Elasticsearch编程操作

本文详细介绍了如何使用Elasticsearch进行编程操作,包括工程搭建、依赖配置、索引管理、文档操作及各种查询技巧等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Elasticsearch编程操作

1 目标

2 讲解

2.1 工程搭建

(1)搭建工程

我们首先搭建一个新的工程,坐标如下


<groupId>com.itheima</groupId>
<artifactId>elasticsearch-day2-demo1</artifactId>
<version>1.0-SNAPSHOT</version>

(2)pom.xml依赖


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>elasticsearch-day2-demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.21</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

(3)创建测试类com.itheima.test.ElasticsearchTest

2.2 数据操作

2.2.1 创建/删除索引

(1)创建索引

(2)删除索引

2.2.2 创建映射

这里的映射表示创建索引类型结构,如果不创建映射,Elasticsearch会默认根据创建的文档中的数据用来创建映射。

查看之前的blog的索引

要想指定对每个字段是否使用分词器,需要手动创建映射。

使用代码创建映射,代码如下:

查看最新的blog3的索引

2.2.3 创建索引数据

(1)通过XContentBuilder对象创建

代码如下:


/***
 * 创建索引数据
 */
@Test
public void testPutIndexDataDemo4() throws Exception{
    //创建TransportClient,并设置不使用集群
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
    //设置IP和端口
    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));
    //通过XContentBuilder构建数据
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    builder.field("id","1");
    builder.field("title","ElasticSearch是一个基于Lucene的搜索服务器,深圳黑马训练营!");
    builder.field("content","它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。");
    builder.endObject();
    //使用TransportClient对象增加数据
    client.prepareIndex("blog3","article","1").setSource(builder).get();
    //关闭资源
    client.close();
}

查看数据如下:

(2)使用Jackson转换实体

a.创建Article

创建com.itheima.domain.Article,代码如下:


public class Article implements Serializable {

    private Integer id;
    private String title;
    private String content;
    
    //..get..set..toString
}

b.引入坐标

Jackson的包,可以将Java对象转换成Json字符串;也可以将Json的字符串,转化成Java对象,引入如下坐标:


<!--jackson JSON转换包-->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.1</version>
</dependency>

c.代码实现

1.2.2.4 修改索引数据

(1)使用prepareUpdate修改

(2)直接使用update()修改

1.2.2.5 删除索引数据

(1)通过prepareDelete 删除

(2)直接使用delete() 删除

1.2.2.6 批量增加数据

数据如下:

1.2.3 查询

1.2.3.1 字符串查询

1.2.3.2 词条查询

1.2.3.3 jackson转换JavaBean

1.2.3.4 多种查询方式实现

1.2.3.5 组合查询

布尔查询


     must(QueryBuilders) : AND,求交集
     mustNot(QueryBuilders): NOT
     should(QueryBuilders):OR ,求并集

代码实现如下:

这里发现:

.must(QueryBuilders.*wildcardQuery*(“content”, “elastics*ch”)) // 模糊查询

能搜索到到结果。

而使用

.must(QueryBuilders.*wildcardQuery*(“content”, “ElasticS*ch”)) // 模糊查询

不能搜索到结果。为什么呢?

分词查询如下:

<http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=ElasticSearch是一个全文检索的框架>

因为IK分词器,在建立索引的时候将英文都变成了小写,这样方便我们在搜索的时候可以实现“不区分大小写”的搜索,只要我们在搜索的条件中添加.toLowerCase()的方法即可。

例如:

.must(QueryBuilders.*wildcardQuery*(“content”, “ELASTics*ch”.toLowerCase()))

将输入的值都先变成小写,再来搜索结果

1.2.3.6 使用DSL表达式

在定义json:放置到Elasticsearch的HEAD插件(PostMan工具)中(DSL表达式),使用restful风格编程,传递消息体

使用head插件查看索引库的信息,进行查询:

JSON数据如下:


{
  "query" : {
    "bool" : {
      "must" : {
        "term" : {
          "title" : "elasticsearch"
        }
      },
      "must" : {
        "range" : {
          "id" : {
            "from" : 5,
            "to" : 55
          }
        }
      }
    }
  }
}

1.2.3.7 分页排序

1.2.4 高亮查询

1.2.4.1 什么是高亮

在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮

百度搜索关键字“传智播客”

高亮源码分析:

1.2.4.2 高亮显示的html分析

通过开发者工具查看高亮数据的html代码实现:

ElasticSearch可以对查询出的内容中关键字部分进行标签和样式的设置,但是你需要告诉ElasticSearch使用什么标签对高亮关键字进行包裹呢?

经过测试:使用<em>高亮内容</em>

1.2.4.3 高亮实现代码

输出结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值