代码之家  ›  专栏  ›  技术社区  ›  fmdaboville

Elasticsearch Java Jest客户端查询生成器范围与布尔查询

  •  3
  • fmdaboville  · 技术社区  · 6 年前

    我需要使用Jest客户端进行Elasticsearch查询,以便将一些术语和日期与范围查询相匹配。因此,我需要使用Jest对范围查询执行布尔查询 QueryBuilder 有这样的请求:

    {
    "query": {
        "range": {
            "gte": "begindate",
            "lte": "enddate"
        },
        "bool": {
            "must": [
                {
                    "terms": {
                        "field1": [
                            55,
                            99
                        ]
                    }
                },
                {
                    "terms": {
                        "field2": [
                            450
                        ]
                    }
                },
                {
                    "terms": {
                        "field3": [
                            11
                        ]
                    }
                }
            ]
        }
    }
    

    }

    为此,我使用 queryBuilder 像这样:

    Builder search = null;
    Search buildedSearch = null;
    SearchResult result = null;
    
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder qb = QueryBuilders.boolQuery();
    
    if (field1 != null) {
        qb.must(QueryBuilders.termsQuery("field1", field1));
    }
    
    if (field2 != null) {
        qb.must(QueryBuilders.termsQuery("field2", field2));
    }
    
    if (field3 != null) {
        qb.must(QueryBuilders.termsQuery("field3", field3));
    }
    
    String query = searchSourceBuilder.query(qb).toString();
    
    if (field != null) {
        search = new Search.Builder(query).addIndex(index).addType(type);
        if (beginIndex != -1) {
            search.setParameter(Parameters.FROM, beginIndex);
        }
    
        if (endIndex != -1) {
            search.setParameter(Parameters.SIZE, endIndex);
        }
    
        buildedSearch = search.build();
    }
    
    try {
        result = client.execute(buildedSearch);
    } catch (IOException e) {
        LOGGER.info("Can't found result");
    }
    

    如何在搜索生成器上添加范围查询,因为我的对象 qb 已经有一个布尔查询,我无法向其中添加范围查询。

    2 回复  |  直到 4 年前
        1
  •  7
  •   spottedmahn    4 年前

    试试这个。

    QueryBuilder rangeQuery = QueryBuilders
    .rangeQuery("field")
    .from("2016-01-01||/D")
    .to("2017-01-01||/D")
    .includeLower(true)
    .includeUpper(false);
    
    
    QueryBuilder termsQuery = QueryBuilders.boolQuery()
    .must(QueryBuilders.termsQuery("field1","12"))
    .must(QueryBuilders.termQuery("field2", "abc"))
    .must(QueryBuilders.termQuery("field3", "def"));
    
    
    QueryBuilder qb = QueryBuilders
    .boolQuery()
    .should(rangeQuery)
    .should(termsQuery);
    
        2
  •  2
  •   aleroot    6 年前

    您需要一个如下所示的范围查询:

    QueryBuilder rangeQ = QueryBuilders
                        .rangeQuery("begindate")
                        .from(5)
                        .to(10);
    

    然后将这两个查询与一个should组合起来:

    QueryBuilder qb = QueryBuilders
                        .boolQuery()
                        .should(rangeQ)
                        .should(boolQ);
    

    看看这篇帖子: How to construct a boolquery dynamically in java api? 了解更多信息。