• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ElasticSearch系列 - SpringBoot整合ES查询字段不为空的文档 exists

武飞扬头像
我一直在流浪
帮助5

1. ElasticSearch exists 查询是什么

在某些场景下,我们希望找到某个字段不为空的文档,则可以用exists搜索。字段不为空的条件有:

值存在且不是 null;

值不是空数组;

值是数组,但不是 [null]

例如,查询在字段中至少有一个非空值的文档:

GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}

这些文档都将匹配上面的查询:

{ "user": "jane" }
{ "user": "" } ①
{ "user": "-" } ②
{ "user": ["jane"] }
{ "user": ["jane", null ] } ③

① 空字符串是 non-null (非空值)。

② 即使通过 standard analyzer 标准分析器也不会发出警告,原始字段也是非空的。

③ 至少需要一个 non-null 非空值。

这些文档将不会被上面的查询匹配到:

{ "user": null }
{ "user": [] } ①
{ "user": [null] } ②
{ "foo":  "bar" } ③

① 这个字段没有任何值。

② 至少需要一个 non-null 非空值。

③ user 字段完全丢失。

2. ElasticSearch exists 查询字段值存在且不是 null 的文档

在某些场景下,我们希望找到某个字段不为空的文档,则可以用exists搜索。字段不为空的条件有:

值存在且不是 null;

值不是空数组;

值是数组,但不是 [null]

① 索引文档

PUT /my_index
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT /my_index/_doc/1
{
  "tag":null
}

PUT /my_index/_doc/2
{
  "tag":""
}

PUT /my_index/_doc/3
{
  "tag":"C"
}

② 查询 tag 字段值存在且不是 null 的文档

GET /my_index/_search
{
  "query": {
    "exists": {"field": "tag"}
  }
}
{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "tag" : ""
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "tag" : "C"
        }
      }
    ]
  }
}

3. ElasticSearch exists 查询字段值不是空数组的文档

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT /my_index/_doc/2
{
  "tag":[]
}

PUT /my_index/_doc/4
{
  "tag":["A"]
}

② 查询 tag 字段值不是空数组的文档

GET /my_index/_search
{
  "query": {
    "exists": {"field": "tag"}
  }
}
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "tag" : [
            "A"
          ]
        }
      }
    ]
  }
}

4. ElasticSearch exists 查询字段值是数组但不是 [null] 的文档

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT /my_index/_doc/3
{
  "tag":[null]
}

PUT /my_index/_doc/4
{
  "tag":["A"]
}

② 查询 tag 字段值是数组但不是 [null] 的文档

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "tag" : [
            "A"
          ]
        }
      }
    ]
  }
}

5. ElasticSearch exists 查询文档中是否存在指定的字段

exist查询来检查文档中是否存在指定的字段或属性

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT /my_index/_doc/4
{
  "tag":["A"]
}

② 查询存在 tag 字段的文档:

用exists查询来检查文档中是否存在“tag”字段。如果存在,则该文档将被返回。如果不存在,则该文档将被过滤掉。

GET /my_index/_search
{
  "query": {
    "exists": {"field": "tag"}
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "tag" : [
            "A"
          ]
        }
      }
    ]
  }
}

6. SpringBoot 整合ES实现exist查询

@Slf4j
@Service
public class ElasticSearchImpl {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // exist查询
        ExistsQueryBuilder existsQueryBuilder = new ExistsQueryBuilder("tag");
        searchSourceBuilder.query(existsQueryBuilder);

        SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}

ExistsQueryBuilder 源码:

public class ExistsQueryBuilder extends AbstractQueryBuilder<ExistsQueryBuilder> {
    public static final String NAME = "exists";

    public static final ParseField FIELD_FIELD = new ParseField("field");

    private final String fieldName;

    public ExistsQueryBuilder(String fieldName) {
        if (Strings.isEmpty(fieldName)) {
            throw new IllegalArgumentException("field name is null or empty");
        }
        this.fieldName = fieldName;
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhiffc
系列文章
更多 icon
同类精品
更多 icon
继续加载