elasticsearch10-查询文档处理

请添加图片描述
个人名片:

博主:酒徒ᝰ.
个人简介沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志三人行,必有我师焉。

请添加图片描述
本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》,SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式

【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 点击观看

目录

  • 三.RestClient查询文档
    • 1. 快速入门
    • 2. match查询
    • 3. 精确查询
    • 4. 布尔查询
    • 5. 排序、分页
    • 6. 高亮

三.RestClient查询文档

Elasticsearch中的RestClient是用于发送HTTP请求的客户端。
RestClient可用于创建索引、获取索引、删除索引、插入/更新文档、删除文档、搜索文档等操作。

RestClient的使用效果:

  • 可以在Java应用程序中实现Elasticsearch的各种功能,例如创建索引、获取索引、删除索引、插入/更新文档、删除文档、搜索文档等。
  • 可以实现高可用性、高扩展性的应用程序,通过RestClient可以方便地实现集群管理和负载均衡。
  • 可以实现更快速的数据检索和查询,提高查询效率,减少查询时间。
  • 可以实现更安全的数据检索和查询,通过身份验证和权限控制等机制,保护数据的机密性和完整性。
  • 可以实现更灵活的数据检索和查询,支持多种查询方式和过滤器,可以根据需求定制化查询结果。

1. 快速入门

初始化:

@SpringBootTest
public class DSLSearchTest {

    @Autowired
    private RestHighLevelClient client;

    @BeforeEach
    void setup(){
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.179.128:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

查询结果解析:

private void handleResponse(SearchResponse response) {
    SearchHits hits = response.getHits();
    long total = hits.getTotalHits().value;
    System.out.println("数据总条数:" + total + "条");
    SearchHit[] hitsHits = hits.getHits();
    for (SearchHit hit : hitsHits) {
        String json = hit.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);

        Map<String, HighlightField> highlightFields = hit.getHighlightFields();
        if (!CollectionUtils.isEmpty(highlightFields)) {
            HighlightField highlightField = highlightFields.get("name");
            if (highlightField != null) {
                String name = highlightField.getFragments()[0].string();
                hotelDoc.setName(name);
            }
        }
        System.out.println(hotelDoc);
    }
}

2. match查询

  1. 查询全部
@Test
void testMathAll() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.matchAllQuery());
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);

    handleResponse(response);
}
  1. 单字段查询
@Test
void testMath() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.matchQuery("all", "如家"));
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}
  1. 多字段查询
@Test
void testMultiMath() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.multiMatchQuery("如家", "brand", "name", "business"));
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}

3. 精确查询

  1. 精确查询
@Test
void testTerm() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.termQuery("city", "上海"));
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}
  1. 范围查询
@Test
void testRange() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(150));
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}

4. 布尔查询

@Test
void testBool() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.must(QueryBuilders.termQuery("city", "上海"))
            .should(QueryBuilders.termQuery("brand", "皇冠假日"))
            .should(QueryBuilders.termQuery("brand", "华美达"))
            .mustNot(QueryBuilders.rangeQuery("price").lte(500))
            .filter(QueryBuilders.rangeQuery("score").gte(45));
    request.source().query(boolQuery);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}

5. 排序、分页

@Test
void testPageAndSort() throws IOException {
    int page = 1, size = 5;

    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.matchAllQuery())
            .from((page - 1) * size).size(size)
            .sort("price", SortOrder.DESC);
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}

6. 高亮

@Test
void testHighLight() throws IOException {
    SearchRequest request = new SearchRequest("hotel");
    request.source().query(QueryBuilders.matchQuery("all", "如家"))
            .highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
    SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    handleResponse(response);
}

你可能感兴趣的:(#,elasticsearch,elasticsearch,搜索引擎)