0

我使用elasticsearch弹簧数据,我想添加过滤器,我可以按字段搜索存在/字段不存在或字段值为空/不为空。我应该怎么做?Elasticsearch获取数据,其中字段非空/字段为空或字段存在/字段不存在

我知道即时应该使用这样的事情:

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

卜如何elasticsearch春天数据实现它?

Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(???????)); 

回答

4

你可能喜欢用org.elasticsearch.index.query.FilterBuilders建立你exists过滤

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