gpt4 book ai didi

elasticsearch - 在Elasticsearch中使用filteredQuery和userquery的QueryBuilder

转载 作者:行者123 更新时间:2023-12-02 23:02:27 25 4
gpt4 key购买 nike

我正在使用这段代码从elasticSearch获取数据:

        QueryBuilder qb;
if (query == null || query.trim().length() <= 0) {
qb = QueryBuilders.matchAllQuery();
} else {
qb = QueryBuilders.queryString(query.replaceAll(" ", " OR ").replaceAll(",", " AND ").replaceAll("!", " NOT "));

}
org.elasticsearch.action.search.SearchResponse searchHits = node.client()
.prepareSearch(name)
.setQuery(qb)
.addHighlightedField("file.filename")
.addHighlightedField("content")
.addHighlightedField("meta.title")
.setHighlighterPreTags("<span class='badge badge-info'>")
.setHighlighterPostTags("</span>")
.addFields("*", "_source")
.execute().actionGet();

一切正常。但是现在我想用这个查询过滤结果。就像我们在filteredQuery中一样。
 https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/and-filter.html

我如何使用queryBuilder添加FilterBuilder?

最佳答案

    QueryBuilder qb;
if (query == null || query.trim().length() <= 0) {
qb = QueryBuilders.matchAllQuery();
} else {
qb = QueryBuilders.queryString(query.replaceAll(" ", " OR ")
.replaceAll(",", " AND ").replaceAll("!", " NOT "));
}

FilterBuilder fb = FilterBuilders.andFilter(.....);
FilteredQueryBuilder fqBuilder = QueryBuilders.filteredQuery(qb, fb);

org.elasticsearch.action.search.SearchResponse searchHits = node
.client()
.prepareSearch(name)
.setQuery(fqBuilder)
.addHighlightedField("file.filename")
.addHighlightedField("content")
.addHighlightedField("meta.title")
.setHighlighterPreTags("<span class='badge badge-info'>")
.setHighlighterPostTags("</span>").addFields("*", "_source")
.execute().actionGet();

关于elasticsearch - 在Elasticsearch中使用filteredQuery和userquery的QueryBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com