【全文检索_07】JestClient 操作 Elasticsearch

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 相关依赖

<!-- 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 创建索引

/**
 * @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 删除索引

/**
 * @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

/**
 * @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

/**
 * @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 实体类

/**
 * @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 新增/修改文档

/**
 * @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 批量新增

/**
 * @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 查询
/**
 * @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()));
    }
}

在这里插入图片描述

☞ 根据条件查询
/**
 * @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()));
    }
}

在这里插入图片描述

☞ 区间搜索
/**
 * @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 删除文档

/**
 * @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 分页

/**
 * @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 高亮

/**
 * @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));
            }
        }
    }
}

在这里插入图片描述



  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Elasticsearch(简称ES)是一个开源的分布式搜索和分析引擎,它基于Apache Lucene库构建而成。它被设计用于快速、可扩展和分布式的全文检索,以及实时数据分析。ES具有强大的搜索能力和高效的分布式架构,能够处理大规模数据的存储、搜索和分析。 ES的核心概念是索引(Index)、文档(Document)和类型(Type)。索引是包含一系列文档的逻辑容器,每个文档都是一个JSON格式的数据对象,可以被索引和搜索。类型是文档的逻辑分类,用于区分不同类型的文档。ES提供了丰富的搜索功能,包括全文搜索、过滤器、聚合等,通过使用查询语言来实现灵活的搜索需求。 ES的分布式特性使得它非常适合处理大规模数据。它使用分片(Shard)和复制(Replica)机制来实现数据的分布和冗余备份,提高了系统的可用性和扩展性。每个索引可以被划分为多个分片,每个分片可以被部署在不同的节点上,从而实现数据的并行处理和负载均衡。 除了全文检索外,ES还提供了丰富的数据分析功能。它支持实时的数据聚合和可视化,可以用于实时监控、日志分析、业务指标分析等场景。ES还可以与其他工具和框架集成,如Kibana(可视化工具)、Logstash(日志收集工具)等,形成完整的日志分析和监控系统。 总而言之,Elasticsearch是一个强大的全文检索和分析引擎,具有高效的搜索能力、可扩展的分布式架构和丰富的数据分析功能。它在各种场景下都能发挥重要作用,包括企业搜索、电商商品搜索、日志分析等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值