You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将Elasticsearch Bool查询转换为Spring Boot代码(7.17.3版本)

Elasticsearch 7.17.3 Bool查询转Spring Boot代码实现指导

我使用Elasticsearch 7.17.3版本,已经编写了Spring Boot中查询Elasticsearch所有文档的代码,现在需要把指定的Elasticsearch Bool查询语句转换成Spring Boot可运行代码,希望得到具体指导。


原Elasticsearch Bool查询语句

GET feeds/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "comment",
            "query": { 
              "match": {
                "comment.c_text": "This is mateen"
              }
            },"inner_hits": {}
          }
        },
        {
          "term": {
            "title.keyword": {
              "value": "This is mateen"
            }
          }
        },
        {
          "term": {
            "body.keyword": {
              "value": "This is mateen"
            }
          }
        }
      ]
    }
  }
}

索引Mapping定义

PUT feeds
{
  "mappings": {
    "properties": {
      "comment":{
        "type": "nested"
      }
    }
  }
}

现有查询所有文档的Spring Boot代码

public List<feed> searchAllDocuments() throws IOException {
    SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName));
    SearchResponse searchResponse = elasticsearchClient.search(searchRequest, feed.class);
    List<Hit> hits = searchResponse.hits().hits();
    List<feed> feeds = new ArrayList<>();
    feed f = null;
    for (Hit object : hits) {
        f = (feed) object.source();
        feeds.add(f);
    }
    return feeds;
}

转换后的Bool查询Spring Boot代码

public List<feed> searchWithBoolQuery(String searchText) throws IOException {
    // 构建符合需求的Bool查询请求
    SearchRequest searchRequest = SearchRequest.of(s -> s
            .index(indexName)
            .query(q -> q
                    .bool(b -> b
                            // 第一个should条件:嵌套查询comment字段
                            .should(nestedQuery -> nestedQuery
                                    .nested(n -> n
                                            .path("comment")
                                            .query(nq -> nq
                                                    .match(m -> m
                                                            .field("comment.c_text")
                                                            .query(searchText)
                                                    )
                                            )
                                            .innerHits(ih -> ih)
                                    )
                            )
                            // 第二个should条件:精准匹配title.keyword
                            .should(titleTermQuery -> titleTermQuery
                                    .term(t -> t
                                            .field("title.keyword")
                                            .value(searchText)
                                    )
                            )
                            // 第三个should条件:精准匹配body.keyword
                            .should(bodyTermQuery -> bodyTermQuery
                                    .term(t -> t
                                            .field("body.keyword")
                                            .value(searchText)
                                    )
                            )
                    )
            )
    );

    // 执行查询并封装结果
    SearchResponse<feed> searchResponse = elasticsearchClient.search(searchRequest, feed.class);
    List<Hit<feed>> hits = searchResponse.hits().hits();
    List<feed> feeds = new ArrayList<>();
    for (Hit<feed> hit : hits) {
        feed f = hit.source();
        if (f != null) {
            feeds.add(f);
        }
    }
    return feeds;
}

关键说明

  1. 参数化设计:把查询文本"This is mateen"抽成参数searchText,方便后续修改和复用。
  2. 查询结构对应:完全对齐原ES查询的Bool-should逻辑:
    • Nested查询:针对comment嵌套类型,匹配comment.c_text,同时开启inner_hits返回匹配的嵌套子文档。
    • Term查询:分别对title.keyword和body.keyword做精准匹配,和原查询的.keyword字段用法一致。
  3. 代码优化:
    • 使用泛型SearchResponse<feed>和Hit<feed>,避免强制类型转换,提升代码安全性。
    • 增加source()非空判断,防止空指针异常。
  4. 调用方式:直接调用searchWithBoolQuery("This is mateen")即可执行目标查询。

内容的提问来源于stack exchange,提问作者Ali Abbasi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 01:35:37