Java Elasticsearch-具有存在条件的Boolquery

时间:2019-02-20 11:28:26

标签: java api elasticsearch post

我有以下代码,我需要在某些字段中获取非空结果,并在其他字段中检查值。 (我正在使用“ RestHighLevelClient”)

但是我得到了这个错误:

  

原因:ElasticsearchStatusException [Elasticsearch异常[type = search_phase_execution_exception,原因=所有分片均失败]]

     

....

     

禁止显示:org.elasticsearch.client.ResponseException:方法[POST],   主机[http://192.168.8.101:9200],URI   [/ _search?typed_keys = true&ignore_unavailable = false&expand_wildcards = open&allow_no_indices = true&scroll = 1m&sear   .....

     

{“错误”:{“ root_cause”:[{“ type”:“ query_parsing_exception”,“原因”:“ [存在]   查询不支持[boost]

代码:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.must(QueryBuilders.existsQuery("usuarioIntegracao"));
boolQuery.must(QueryBuilders.termsQuery("tabela", "Arquivo", "Mensagem"));
boolQuery.must(QueryBuilders.termQuery("statusTexto", "Erro"));
boolQuery.must(QueryBuilders.rangeQuery("dataEntrada")
                            .from("now-1d/d")
                            .timeZone("-03:00"));

searchSourceBuilder.query(boolQuery);
searchSourceBuilder.size(9999);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

searchRequest.source(searchSourceBuilder);

SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);

我在Postman上成功搜索了相同的内容

{
    "size" : 1000,

    "sort" : [
        {"dataEntrada" : {"order" : "desc"}}
    ],

    "query" : {
        "bool":{
          "must":[
            {"terms":{ "tabela" : ["Arquivo", "Mensagem"] }},

            {"term":{ "statusTexto" : "Erro" }},

            {"exists" : { "field" : "usuarioIntegracao" }},

            {"range" : {
                "dataEntrada" : {
                    "gte" : "now-1d/d",
                    "lt" :  "now/d",
                    "timeZone" : "-03:00"
                }
            }}
          ],
          "should":{},
          "must_not":{

          }
       }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的问题是语法。 试试这个:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
    .must(QueryBuilders.existsQuery("usuarioIntegracao"))
    .must(QueryBuilders.termsQuery("tabela", "Arquivo", "Mensagem"))
    .must(QueryBuilders.termQuery("statusTexto", "Erro"))
    .must(QueryBuilders.rangeQuery("dataEntrada").from("now-1d/d").timeZone("-03:00"));

searchSourceBuilder.query(boolQuery);