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

Elasticsearch获取字段不为空/字段为空或字段存在/字段不存在的数据

  •  0
  • rad11  · 技术社区  · 9 年前

    我正在使用弹性搜索弹簧数据,我想添加过滤器,我可以按字段存在/字段不存在或字段值为空/不为空进行搜索。我该怎么做?

    我知道我应该用这样的东西:

    {
      "query": {
        "filtered": {
          "filter": {
            "bool": {
              "must": [
                {
                  "exists": {
                    "field": "price"
                  }
                },
                ...     <-- your other constraints, if any
              ]
            }
          }
        }
      }
    }
    

    但是如何在弹性搜索弹簧数据中实现它?

    Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(???????));
    
    1 回复  |  直到 9 年前
        1
  •  5
  •   Rahul    9 年前

    您可能希望使用 org.elasticsearch.index.query.FilterBuilders 构建您的 exists 滤器

    FilterBuilders.boolFilter().must(FilterBuilders.existsFilter("price"))