查询弹性搜索中某些文档中缺少的字段

时间:2016-03-16 21:27:21

标签: java elasticsearch

我特别需要在哪里查询弹力搜索字段说F1。 所有文件中都没有F1。我的搜索应该是缺少F1文档和F1有一些价值。 缺少F1或F1 =' A1'

我不确定弹性搜索是否可行。这是我的查询,我知道这是不正确的查询。希望有人可以纠正它或Java程序。

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": {
                  "terms": {
                    "F1": [
                      "Some Value"
                    ]
                  }
                }
              }
            },
            {
              "missing": {
                "field": "F1"
              }
            }
          ]
        }
      }
    }
  }
}

这是构建查询的java代码。

  public QueryBuilder getQueryBuilder() {
        QueryBuilder qb;
        //what do we want to return if there is no request? Nothing or all.
        if (filters == null || filters.isEmpty()) {
            qb = QueryBuilders.matchAllQuery();
        } //build our filtered query using given filters in our request
        else {
            Set<String> keys = filters.keySet();
            BoolFilterBuilder boolFilterBuilder = FilterBuilders.boolFilter();
            for (String key : keys) {
                Set<Object> values = filters.get(key);
                if (values == null || values.toString().isEmpty()) {
                    continue; //Ignore nothing to do.
                }
                if (key.equalsIgnoreCase(Constants.MISSING_FILTER)) {
                    Iterator i = values.iterator();
                    while (i.hasNext()) {
                        boolFilterBuilder.must(FilterBuilders.missingFilter((String) i.next()));
                    }
                } else {
                    boolFilterBuilder.must(buildShouldQuery(key, values));
                }
            }
            qb = QueryBuilders.filteredQuery(createSearchQueryBuilder(), boolFilterBuilder);
        }
        return qb;
    }

1 个答案:

答案 0 :(得分:1)

尝试使用此功能。

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "bool": {
                    "should": [
                        { "terms": { "F1": ["Some Value"] },
                        { "missing": { "field": "F1" } }
                    ]
                }
            }
         }
     }
}