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 3091: 城市旅行 lct 期望 splay

    https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...

  2. CI Weekly #15 | 据说新版 flow.ci Dashboard 界面很酷

    好久不见 :) 最近工程师们卯足了劲,全新的 flow.ci dashboard 页面 已经与所有用户见面了.更快捷地创建项目,构建列表页面新增分支,Pull Request 界面:侧边栏新增构建任务 ...

  3. 缓存处理后,F5刷新页面,css和js返回200,为什么不是304?

    最近在Apache上做网站的静态资源缓存,但是各种配置之后,发现css和js返回的状态码都是200,为什么不是304? 找来找去在知乎上得到了答案. 来自知乎的一个回答 http://www.zhih ...

  4. Codeforces Round #354 (Div. 2) D. Theseus and labyrinth bfs

    D. Theseus and labyrinth 题目连接: http://www.codeforces.com/contest/676/problem/D Description Theseus h ...

  5. Git_添加远程库

    现在的情景是,你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举 ...

  6. js的栈与堆

    JavaScript中基本数据类型和引用数据类型的区别  这是我引用别人的  觉得很好   1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本 ...

  7. oracle中的术语

    数据库:数据库是实实在在存在的文件,一个数据库文件体系中大致包含(数据文件DBF.控制文件CTL.日志文件LOG) 数据库实例:每个数据库都会有一个数据库实例与之对应,外界环境要通过与数据库实例的交互 ...

  8. HK设备安全补丁升级方案

    1.背景:          当前很多HK行业设备的端口映射到公网上,其中一部分老版本设备是存在安全漏洞的,由于传统行业没有设备平台的概念,无法通过设备提示用户进行升级,导致这些存在漏洞的设备在互联网 ...

  9. 深入浅出VC++串口编程之基于Win32 API

    1.API描述 在WIN32 API中,串口使用文件方式进行访问,其操作的API基本上与文件操作的API一致. 打开串口 Win32 中用于打开串口的API 函数为CreateFile,其原型为: H ...

  10. oracle extract函数

    oracle Extract 函数 //oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分   //语法如下:   EXTRA ...