前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【全文检索_07】JestClient 操作 Elasticsearch

【全文检索_07】JestClient 操作 Elasticsearch

作者头像
Demo_Null
发布2021-02-04 10:21:45
2.1K0
发布2021-02-04 10:21:45
举报
文章被收录于专栏:Java 学习Java 学习

1.1 简介

1.1.1 概述

  Elasticsearch Java API 有四类 client 连接方式:TransportClient、 RestClient 、Jest、 Spring Data Elasticsearch。其中 TransportClient、RestClient 是 Elasticsearch 原生的 api,TransportClient 会在 8.0 版本中删除,替代的是 HighLevelRestClient,它使用 HTTP 请求而不是 Java 序列化请求。Spring Data Elasticsearch 是 Spring 集成的 Elasticsearch 开发包。本章介绍如何使用 JestClient 操作 Elasticsearch。

1.1.2 相关依赖

代码语言:javascript
复制
<!-- Elasticsearch HTTP 客户端 JestClient -->
<dependency>
    <groupId>io.searchbox</groupId>
    <artifactId>jest</artifactId>
    <version>5.3.3</version>
</dependency>
<!-- 与使用版本保持一致 -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.10.2</version>
</dependency>

1.2 索引操作

1.2.1 创建索引

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 创建索引
 */
@SpringBootTest
public class ES {

    @Test
    public void create() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 创建索引
        CreateIndex createIndex = new CreateIndex.Builder("my_index").build();
        JestResult result = jestClient.execute(createIndex);

        // 5. 输出创建结果
        System.out.println(result.getJsonString());
    }
}
在这里插入图片描述
在这里插入图片描述

1.2.2 删除索引

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 删除索引
 */
@SpringBootTest
public class ES {

    @Test
    public void delete() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 删除索引
        DeleteIndex deleteIndex = new DeleteIndex.Builder("my_index").build();
        JestResult result = jestClient.execute(deleteIndex);

        // 5. 输出删除结果
        System.out.println(result.getJsonString());
    }
}
在这里插入图片描述
在这里插入图片描述

1.2.3 设置 Mapping

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 设置 mapping
 */
@SpringBootTest
public class ES {

    @Test
    public void setMapping() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 创建 json 格式的 mapping
        /**
         * {
         *     "mappings":{
         *         "properties":{
         *             "field1":{
         *                 "type":"keyword"
         *             },
         *             "field2":{
         *                 "type":"byte"
         *             }
         *         }
         *     }
         * }
         */
        Map<String, Object> map = new HashMap<String, Object>() {{
            this.put("mappings", new HashMap<String, Object>() {{
                this.put("properties", new HashMap<String, Object>() {{
                    this.put("name", new HashMap<String, String>() {{
                        this.put("type", "keyword");
                    }});
                    this.put("age", new HashMap<String, String>() {{
                        this.put("type", "integer");
                    }});
                }});
            }});
        }};
        String mapping = JSONObject.toJSONString(map);

        // 5. 创建索引
        // PutMapping putMapping = new PutMapping.Builder(indexName, indexType, indexMapping).build();
        CreateIndex createIndex = new CreateIndex.Builder("my_index").settings(mapping).build();
        JestResult result = jestClient.execute(createIndex);

        // 6. 输出创建结果
        System.out.println(result.getJsonString());
    }
}
在这里插入图片描述
在这里插入图片描述

1.2.4 获取 Mapping

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 获取 mapping
 */
@SpringBootTest
public class ES {

    @Test
    public void getMapping() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 获取 mapping
        GetMapping getMapping = new GetMapping.Builder().addIndex("my_index").build();
        JestResult result = jestClient.execute(getMapping);

        // 6. 输出获取结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述

1.3 文档操作

1.3.1 实体类

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc //TODO
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookDocument {

    @JestId // 自动生成 id,
    private String id;
    private String name;
    private String author;
    private String desc;
}

1.3.2 新增/修改文档

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 添加文档
 */
@SpringBootTest
public class ES {

    @Test
    public void create() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 准备数据
        BookDocument bookDocument = new BookDocument("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界");

        // 4. 首先会判断索引是否存在,不存在则根据文档创建索引,然后判断 id 是否存在,存在就是更新文档
        Index index = new Index.Builder(bookDocument).index("my_index").type("_doc").build();
        JestResult result = jestClient.execute(index);

        // 5. 输出创建结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述

1.3.3 批量新增

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc //TODO
 */
@SpringBootTest
public class ES {

    @Test
    public void create() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 批量创建文档
        Bulk.Builder bulk = new Bulk.Builder().defaultIndex("my_index").defaultType("_doc");
        List<BookDocument> list = new ArrayList<BookDocument>() {{
            this.add(new BookDocument("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界"));
            this.add(new BookDocument("002", "完美世界", "辰东", "遮天前传"));
            this.add(new BookDocument("003", "盘龙", "我吃西红柿", "神奇的戒指"));
            this.add(new BookDocument("004", "诛仙", "萧鼎", "天地不仁,以万物为刍狗"));
            this.add(new BookDocument("005", "大主宰", "天蚕土豆", "大千世界,位面交汇"));
        }};
        for (BookDocument bookDocument : list) {
            // 如未设置唯一 id 值,则唯一 id 会默认生成,索引操作为添加操作
            Index index = new Index.Builder(bookDocument).build();
            bulk.addAction(index);
        }
        BulkResult result = jestClient.execute(bulk.build());

        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述

1.3.4 查询文档

☞ 根据 id 查询
代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 根据 id 查询文档
 */
@SpringBootTest
public class ES {

    @Test
    public void search() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 查询文档
        Get get = new Get.Builder("my_index", "001").type("_doc").build();
        JestResult result = jestClient.execute(get);

        // 5. 输出查询结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述
☞ 根据条件查询
代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 条件查询
 */
@SpringBootTest
public class ES {

    @Test
    public void search() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 查询文档
        // 4.1 构造查询条件
        // 4.1.1 单 field 不分词查询
        // TermQueryBuilder termQueryBuilder = new TermQueryBuilder(fieldName, value);
        // 4.1.2 单 field 多词不分词查询
        // TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("", "value1", "value2");
        // 4.1.3 单 field 分词查询
        // MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, value);
        // 4.1.4 多 field 分词查询
        MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("斗气", "desc");
        // 转化为 ES 查询,默认分词后 and 连接,可使用 defaultOperator(Operator.AND) 修改
        SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);
        // 4.2 构造查询
        Search search = new Search.Builder(query.toString()).build();
        // 4.3 执行查询
        JestResult result = jestClient.execute(search);

        // 5. 输出查询结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述
☞ 区间搜索
代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc //TODO
 */
@SpringBootTest
public class ES {

    @Test
    public void search() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4.1 构建区间搜索条件
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");
        // 4.2 解析为 ES 查询
        SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder);
        // 4.3 构建查询
        Search search = new Search.Builder(query.toString()).build();
        // 4.4 执行查询
        JestResult result = jestClient.execute(search);

        // 5. 输出查询结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述

1.3.5 删除文档

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 根据 id 删除文档
 */
@SpringBootTest
public class ES {

    @Test
    public void delete() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4. 删除文档
        Delete delete = new Delete.Builder("001").index("my_index").type("_doc").build();
        JestResult result = jestClient.execute(delete);

        // 5. 输出删除结果
        System.out.println(result.getJsonString());
    }
}
在这里插入图片描述
在这里插入图片描述

1.4 其他操作

1.4.1 分页

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc 分页
 */
@SpringBootTest
public class ES {

    @Test
    public void search() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4.1 构建区间搜索条件
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");
        // 4.2 解析为 ES 查询并添加分页
        SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder).from(0).size(1);
        // 4.3 构建查询
        Search search = new Search.Builder(query.toString()).build();
        // 4.4 执行查询
        JestResult result = jestClient.execute(search);

        // 5. 输出查询结果
        System.out.println(formatJson(result.getJsonString()));
    }
}
在这里插入图片描述
在这里插入图片描述

1.4.2 高亮

代码语言:javascript
复制
/**
 * @author Demo_Null
 * @version 1.0
 * @date 2021/2/1
 * @desc //TODO
 */
@SpringBootTest
public class ES {

    @Test
    public void search() throws IOException {
        // 1. 创建 ES 连接池
        JestClientFactory jestClientFactory = new JestClientFactory();

        // 2. 配置 ES 信息
        HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();
        jestClientFactory.setHttpClientConfig(config);

        // 3. 获取 ES 连接
        JestClient jestClient = jestClientFactory.getObject();

        // 4.1 构建搜索条件
        MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("天世界", "author", "desc");
        // 4.2 转化为 ES 搜索
        SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);
        // 4.3 配置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("author");
        highlightBuilder.field("desc");
        highlightBuilder.preTags("<em>").postTags("</em>");
        highlightBuilder.fragmentSize(500);
        query.highlighter(highlightBuilder);
        // 4.4 构建查询
        Search search = new Search.Builder(query.toString()).build();
        // 4.5 执行查询, 使用 SearchResult 接收
        SearchResult result = jestClient.execute(search);

        // 5.1 遍历查询结果
        List<SearchResult.Hit<BookDocument, Void>> hits = result.getHits(BookDocument.class);
        for (SearchResult.Hit<BookDocument, Void> hit : hits) {
            // 5.2 获取高亮集合
            Map<String, List<String>> highlight = hit.highlight;
            for (String s : highlight.keySet()) {
                // 5.3 输出高亮结果
                System.out.println("高亮显示:" + highlight.get(s).get(0));
            }
        }
    }
}
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-02-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.1 简介
    • 1.1.1 概述
      • 1.1.2 相关依赖
      • 1.2 索引操作
        • 1.2.1 创建索引
          • 1.2.2 删除索引
            • 1.2.3 设置 Mapping
              • 1.2.4 获取 Mapping
              • 1.3 文档操作
                • 1.3.1 实体类
                  • 1.3.2 新增/修改文档
                    • 1.3.3 批量新增
                      • 1.3.4 查询文档
                        • ☞ 根据 id 查询
                        • ☞ 根据条件查询
                        • ☞ 区间搜索
                      • 1.3.5 删除文档
                      • 1.4 其他操作
                        • 1.4.1 分页
                          • 1.4.2 高亮
                          相关产品与服务
                          Elasticsearch Service
                          腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档