gpt4 book ai didi

java - Elasticsearch java api 或过滤器

转载 作者:行者123 更新时间:2023-12-01 10:07:19 25 4
gpt4 key购买 nike

我需要应用“OR”过滤器,这相当于('a'或'b'或'c'或'd'或'e')中的类别,下面提供了等效的ES查询

"query_string": {
"query": "category: (\"a\", \"b\", \"c\", \"d\", \"e\")"
}

我有下面提到的 JAVA ES API 代码,我对在 Java 中添加 OR 过滤器感到惊讶。有人可以帮忙吗?

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.termFilter("category","a")))
.must(QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr)))
.fields(fieldList);

最佳答案

根据您的示例,我假设您使用的是 ES 1.x 并假设 RangeQuery 应该是一个查询。如果我正确理解你的问题,你想做这样的事情:

QueryBuilders.filteredQuery(
QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr),
FilterBuilders.boolFilter()
.should(FilterBuilders.termFilter("category","a"))
.should(FilterBuilders.termFilter("category","b"))
.should(FilterBuilders.termFilter("category","c"))
.should(FilterBuilders.termFilter("category","d"))
.should(FilterBuilders.termFilter("category","e"))
)

但是,您也可以使用 terms filter :

QueryBuilders.filteredQuery(
QueryBuilders.rangeQuery("@timestamp").from(fromHr).to(toHr),
FilterBuilders.termsFilter("category", "a", "b", "c", "d", "e", "f" ))

关于java - Elasticsearch java api 或过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362353/

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