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”字段。如果存在,则该文档将被返回。如果不存在,则该文档将被过滤掉。文章来源:https://www.toymoban.com/news/detail-460320.html
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 源码:文章来源地址https://www.toymoban.com/news/detail-460320.html
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;
}
}
到了这里,关于ElasticSearch系列 - SpringBoot整合ES:查询字段不为空的文档 exists的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!