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 `

elasticSearch nested exist与missing查询的更多相关文章

  1. ElasticSearch 学习记录之ES查询添加排序字段和使用missing或existing字段查询

    ES添加排序 在默认的情况下,ES 是根据文档的得分score来进行文档额排序的.但是自己可以根据自己的针对一些字段进行排序.就像下面的查询脚本一样.下面的这个查询是根据productid这个值进行排 ...

  2. 干货 | Elasticsearch Nested类型深入详解(转)

    https://blog.csdn.net/laoyang360/article/details/82950393 0.概要在Elasticsearch实战场景中,我们或多或少会遇到嵌套文档的组合形式 ...

  3. 干货 | Elasticsearch Nested类型深入详解

    在Elasticsearch实战场景中,我们或多或少会遇到嵌套文档的组合形式,反映在ES中称为父子文档. 父子文档的实现,至少包含以下两种方式: 1)父子文档 父子文档在5.X版本中通过parent- ...

  4. 第三百六十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询

    第三百六十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询 bool查询说明 filter:[],字段的过滤,不参与打分must:[] ...

  5. 使用ElasticSearch完成百万级数据查询附近的人功能

    上一篇文章介绍了ElasticSearch使用Repository和ElasticSearchTemplate完成构建复杂查询条件,简单介绍了ElasticSearch使用地理位置的功能. 这一篇我们 ...

  6. 解决 Elasticsearch 超过 10000 条无法查询的问题

    解决 Elasticsearch 超过 10000 条无法查询的问题 问题描述 分页查询场景,当查询记录数超过 10000 条时,会报错. 使用 Kibana 的 Dev Tools 工具查询 从第 ...

  7. elasticsearch nested查询

    项目里可能会遇到多级嵌套的情况,实际上最多两级,三级及以上,我测试不通过. 一级索引时,我插入数据,会自动创建索引映射:然二级时,索引映射必须手动创建. 映射: PUT test999 { " ...

  8. Elasticsearch: nested对象

    在处理大量数据时,关系数据库存在很多问题. 无论是速度,高效处理,有效并行化,可扩展性还是成本,当数据量开始增长时,关系数据库都会失败.该关系数据库的另一个挑战是必须预先定义关系和模式.Elastic ...

  9. Elasticsearch结构化搜索与查询

    Elasticsearch 的功能之一就是搜索,搜索主要分为两种类型,结构化搜索和全文搜索.结构化搜索是指有关查询那些具有内在结构数据的过程.比如日期.时间和数字都是结构化的:它们有精确的格式,我们可 ...

随机推荐

  1. BZOJ.5407.girls(容斥 三元环)

    题目链接 CF 原题 \(Description\) 有n个点,其中有m条边连接两个点.每一个没有连边的三元组\((i,j,k)(i<j<k)\)对答案的贡献为\(A*i+B*j+C*k\ ...

  2. [51Nod 1773] A国的贸易

    [51Nod 1773] A国的贸易 题目描述 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们有着神奇的贸易规则. ...

  3. weighttp 使用

    Weighttp 地址 http://redmine.lighttpd.net/projects/weighttp/wiki Weighttp的介绍:weighttp  is a lightweigh ...

  4. 某gov的逻辑漏洞

    首先找一个号 在企业信息里面查看到大量的企业名称和组织机构代码 随后去找回密码那 可以看到是直接显示了用户名和密码 随后去登录 可以看到大量的工程信息个企业注册信息 ​

  5. [AGC016E]Poor Turkeys

    [AGC016E]Poor Turkeys 题目大意: 有\(n(n\le400)\)只火鸡,编号为\(1\)到\(n\),有\(m(m\le10^5)\)个人,每人指定了两只火鸡\(x\)和\(y\ ...

  6. Wannafly挑战赛22游记

    Wannafly挑战赛22游记 幸运的人都是相似的,不幸的人各有各的不幸. --题记 A-计数器 题目大意: 有一个计数器,计数器的初始值为\(0\),每次操作你可以把计数器的值加上\(a_1,a_2 ...

  7. 连接本地websocket服务延迟的问题

    今天用C#编写了一个Chrome Remote Debugger的客户端程序,发现使用rest和websocket程序时第一次连接的时候特别慢,大概每次都要消耗一秒左右,而用chrome直接连接却没有 ...

  8. Android File Hierarchy : System Structure Architecture Layout

    Most of the Android user are using their Android phone just for calls, SMS, browsing and basic apps, ...

  9. Run native executable in Android App

    Run native executable in Android App Demo † Here's demo application called "Run Native Exe" ...

  10. hint.css使用说明

    GitHub:http://liu12fei08fei.github.io/html/1hint.html hint.css使用说明 用途 快速实现tooltips提示样式 相关资源 官方网站GitH ...