gpt4 book ai didi

elasticsearch - 如何使用Elasticsearch Java RestHighLevelClient API创建 bool 查询

转载 作者:行者123 更新时间:2023-12-03 00:38:50 24 4
gpt4 key购买 nike

如何在下面使用bool创建RestHighLevelClient查询?

我的尝试未返回任何内容:

BoolQueryBuilder query = boolQuery()
.must(queryStringQuery(queryString).analyzeWildcard(true).defaultField("*"))
.must(rangeQuery("timestamp").gte(fromTs).lte(toTs).format("epoch_millis")
);

我想要
"query":{  
"bool":{
"must":[
{
"query_string":{
"query":"foo:evlis AND bar:beatles",
"analyze_wildcard":true,
"default_field":"*"
}
},
{
"range":{
"timestamp":{
"gte":1549360021398,
"lte":1549446421398,
"format":"epoch_millis"
}
}
}
],
"filter":[

],
"should":[

],
"must_not":[

]
}
}

更新
我已启用 org.elasticsearch.client上的调试日志记录,并可以看到此调试输出。 当我单击调试日志中的链接时,可以看到结果。
2019-02-07 19:20:11.351 DEBUG 17524 --- [/O dispatcher 1] org.elasticsearch.client.RestClient      : request 
[POST http://10.134.39.147:31429/journey/_doc/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512]
returned [HTTP/1.1 200 OK]

但是 searchResponse.getHits()为空,找到0次???
    private void printResult(SearchResponse response) {
System.out.println("=== HTTP Request ===");
System.out.println("status: " + response.status());
System.out.println("took: " + response.getTook());
System.out.println("timed_out: " + response.isTimedOut());

System.out.println("\n=== Hits ===");
final SearchHits hits = response.getHits();
System.out.println("total_hits: " + hits.getHits().length);

List<Journey> journeys = new ArrayList<>();
for (SearchHit hit : hits.getHits()) {
System.out.println("\n=== Documents ===");
System.out.println("index: " + hit.getIndex());
System.out.println("type: " + hit.getType());
System.out.println("id: " + hit.getId());
System.out.println("source: " + hit.getSourceAsString());
}
}

输出量
=== HTTP Request ===
status: OK
took: 19ms
timed_out: false

=== Hits ===
total_hits: 0

最佳答案

脚步:

  • 创建一个新对象SearchSourceBuilder sourceBuilder
  • 创建BoolQueryBuilder的新对象boolQueryBuilder
  • 使用QueryBuilders将boolQueryBuilder添加到queryStringQuery中。
  • 使用QueryBuilders将boolQueryBuilder添加到rangeQuery中。
  • 将boolQueryBuilder添加为sourceBuilder的查询。

  • 因此,您的查询翻译为:

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    boolQueryBuilder.must(
    QueryBuilders.queryStringQuery("foo:evlis AND bar:beatles").defaultField("*")
    .analyzeWildcard(true));
    boolQueryBuilder.must(
    QueryBuilders.rangeQuery("timestamp").gte(1549360021398l).lte(1549446421398l)
    .format("epoch_millis"));
    sourceBuilder.query(boolQueryBuilder);

    关于elasticsearch - 如何使用Elasticsearch Java RestHighLevelClient API创建 bool 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54578222/

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