exists允许你过滤文档,只查找那些在特定字段有值的文档,无论其值是多少。demo如下:

1、索引中的数据如下:

ElasticSearch查询之exists使用

2、查询语句如下

GET account/account/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "sex"
          }
        }
      ]
    }
  }
}

如下可以看到,sex字段为null的文档也没有查询出来

ElasticSearch查询之exists使用

3、java代码实现该查询

/**
 * exists过滤器
 */
@ResponseBody
@RequestMapping("/searchAccountBool")
public List<Account> searchAccountBool(){
    BoolQueryBuilder accountBoolquery = QueryBuilders.boolQuery();
    ExistsQueryBuilder sexQueryBuilder = QueryBuilders.existsQuery("sex");
    accountBoolquery.must(sexQueryBuilder);
    System.out.println(accountBoolquery);
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(accountBoolquery).build();
    List<Account> accountList = elasticsearchTemplate.queryForList(searchQuery, Account.class);
    System.out.println(accountList);
    return accountList;
}

4、postman测试

ElasticSearch查询之exists使用

相关文章:

  • 2021-07-06
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
猜你喜欢
  • 2023-01-12
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
相关资源
相似解决方案