如何使用 Elasticsearch Java RestHighLevelClient API 创建布尔查询

Howto create a bool query with Elasticsearch Java RestHighLevelClient API

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

我的尝试没有return任何结果:

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 上启用了调试日志记录,并且可以看到此调试输出。 当我从调试日志中单击 link 时,我可以看到结果。

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

步骤:

  1. 创建新对象 SearchSourceBuilder sourceBuilder
  2. 创建一个新的 BoolQueryBuilder 对象boolQueryBuilder
  3. 使用 QueryBuilders 添加 boolQueryBuilder 一个 queryStringQuery 的必须项。
  4. 使用 QueryBuilders 添加到 boolQueryBuilderrangeQuery 中。
  5. 添加 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);