elasticSearch nested exist与missing查询

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: elasticSearch nested查询,简单意义上,你可以理解为,它不会被索引,只是被暂时隐藏起来,而查询的时候,开关就是使用nested query/filter去查询下面我有一个例子,是查询文档中,含有某字段的nested查询,与不含有某字段的nested查询办法。

elasticSearch nested查询,简单意义上,你可以理解为,它不会被索引,只是被暂时隐藏起来,而查询的时候,开关就是使用nested query/filter去查询

下面我有一个例子,是查询文档中,含有某字段的nested查询,与不含有某字段的nested查询办法。

1.查询文档中存在某字段(account.userId)的nested

ES查询语句

核心


{
              "query": {
                "nested": {
                  "path": "account",
                  "query": {
                    "match_all": {}
                  },
                  "filter": {
                    "exists": {
                      "field": "account.userId"
                    }
                  }
                }
              }
}

java查询语句


//构建Nested查询
NestedFilterBuilder nfb = FilterBuilders.nestedFilter("account", QueryBuilders.filteredQuery(
                            QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("account.userId")));
                     

nfb嵌入到原有的查询query中即可

完整查询



{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "and" : {
          "filters" : [ {
            "range" : {
              "oppType" : {
                "from" : 20,
                "to" : null,
                "include_lower" : true,
                "include_upper" : true
              }
            }
          }, {
            "term" : {
              "city" : 1
            }
          }, {
            "range" : {
              "status" : {
                "from" : 0,
                "to" : 12,
                "include_lower" : true,
                "include_upper" : false
              }
            }
          }, {
            "nested" : {
              "query" : {
                "filtered" : {
                  "query" : {
                    "match_all" : { }
                  },
                  "filter" : {
                    "exists" : {
                      "field" : "ajkAccount.ajkUserId"
                    }
                  }
                }
              },
              "path" : "ajkAccount"
            }
          }, {
            "or" : {
              "filters" : [ {
                "term" : {
                  "saleId" : "xxx"
                }
              }, {
                "terms" : {
                  "deptId" : [ "dept001" ]
                }
              } ]
            }
          } ]
        }
      }
    }
  }
}

curl -XPOST 'http://192.168.1.xx:9200/xxxIndex/xxxEntry/_search' '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":{"filters":[{"range":{"oppType":{"from":null,"to":20,"include_lower":true,"include_upper":false}}},{"term":{"city":1}},{"range":{"status":{"from":0,"to":11,"include_lower":true,"include_upper":true}}},{"query":{"nested":{"path":"account","query":{"match_all":{}},"filter":{"exists":{"field":"account.userId"}}}}}]}}}}}'

2.查询文档中不存在某字段(missing account.userId)的nested

ES查询语句

核心


{
          "not":{
            "nested" : {
              "query" : {
                "filtered" : {
                  "query" : {
                    "match_all" : { }
                  },
                  "filter" : {
                    "exists" : {
                      "field" : "account.userId"
                    }
                  }
                }
              },
              "path" : "account"
            }
            }
 }

java代码


//构建Nested查询
NestedFilterBuilder nfb = FilterBuilders.nestedFilter("account", QueryBuilders.filteredQuery(
                            QueryBuilders.matchAllQuery(), FilterBuilders.existsFilter("account.userId")));

//添加Not
FilterBuilders.notFilter(nfb);

完整ES查询


{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "and" : {
          "filters" : [ {
            "range" : {
              "oppType" : {
                "from" : 20,
                "to" : null,
                "include_lower" : true,
                "include_upper" : true
              }
            }
          }, {
            "term" : {
              "city" : 1
            }
          }, {
            "range" : {
              "status" : {
                "from" : 0,
                "to" : 12,
                "include_lower" : true,
                "include_upper" : false
              }
            }
          }, {
            "not" : {
              "filter" : {
                "nested" : {
                  "query" : {
                    "filtered" : {
                      "query" : {
                        "match_all" : { }
                      },
                      "filter" : {
                        "exists" : {
                          "field" : "account.userId"
                        }
                      }
                    }
                  },
                  "path" : "account"
                }
              }
            }
          }, {
            "or" : {
              "filters" : [ {
                "term" : {
                  "saleId" : "xxxxx"
                }
              }, {
                "terms" : {
                  "deptId" : [ "dept01" ]
                }
              } ]
            }
          } ]
        }
      }
    }
  }
}


curl -XPOST 'http://192.168.1.xx:9200/xxxIndex/xxxEntry/_search' '{"query":{"filtered":{"query":{"match_all":{}},"filter":{"and":{"filters":[{"range":{"oppType":{"from":20,"to":null,"include_lower":true,"include_upper":true}}},{"term":{"city":1}},{"range":{"status":{"from":0,"to":12,"include_lower":true,"include_upper":false}}},{"not":{"filter":{"nested":{"query":{"filtered":{"query":{"match_all":{}},"filter":{"exists":{"field":"account.userId"}}}},"path":"account"}}}},{"or":{"filters":[{"term":{"saleId":"xxxxx"}},{"terms":{"deptId":["dept01"]}}]}}]}}}}}'

参考
http://grokbase.com/t/gg/elasticsearch/13a49a2hmq/check-if-field-exists-in-a-nested-object
https://github.com/elastic/elasticsearch/issues/3495 `

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
6月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
254 20
|
11月前
|
存储 JSON 监控
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
大数据-167 ELK Elasticsearch 详细介绍 特点 分片 查询
376 4
|
12月前
|
JSON 自然语言处理 算法
ElasticSearch基础2——DSL查询文档,黑马旅游项目查询功能
DSL查询文档、RestClient查询文档、全文检索查询、精准查询、复合查询、地理坐标查询、分页、排序、高亮、黑马旅游案例
ElasticSearch基础2——DSL查询文档,黑马旅游项目查询功能
|
11月前
|
自然语言处理 搜索推荐 Java
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(一)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图
207 0
|
11月前
|
存储 自然语言处理 搜索推荐
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
SpringBoot 搜索引擎 海量数据 Elasticsearch-7 es上手指南 毫秒级查询 包括 版本选型、操作内容、结果截图(二)
167 0
|
自然语言处理 Java 关系型数据库
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality
ElasticSearch 实现分词全文检索 - 聚合查询 cardinality
362 1
|
存储 自然语言处理 Java
ElasticSearch 实现分词全文检索 - 经纬度定位商家距离查询
ElasticSearch 实现分词全文检索 - 经纬度定位商家距离查询
253 0
|
自然语言处理 Java
ElasticSearch 实现分词全文检索 - 高亮查询
ElasticSearch 实现分词全文检索 - 高亮查询
146 0
|
缓存 自然语言处理 Java
ElasticSearch 实现分词全文检索 - filter查询
ElasticSearch 实现分词全文检索 - filter查询
121 0
|
自然语言处理 Java
ElasticSearch 实现分词全文检索 - 复合查询
ElasticSearch 实现分词全文检索 - 复合查询
121 0