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 2843: 极地旅行社 lct splay

    http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...

  2. [POI2015]Logistyka

    [POI2015]Logistyka 题目大意: 一个长度为\(n(n\le10^6)\)的数列\(A_i\),初始全为\(0\).操作共\(m(m\le10^6)\)次,包含以下两种: 将\(A_x ...

  3. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

    从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...

  4. SCC缩点

    int V; //顶点数量 vector<int> G[max_v]; //图的邻接表表示方法 vector<int> rG[max_v]; //把边反向建的图 vector& ...

  5. GIT(2)----入门资料,分支管理,冲突解决

    最近一直使用者GIT发现使用起来确实很不错,最近做些整理总结,发现了一些很不错的资料,收集在这里,以备忘. GIT入门挺简单的,之前有些过一篇文章,关于GIT的,但是都是一些生硬的操作,并没有系统的学 ...

  6. nginx 注册为linux系统服务

    #! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve ...

  7. 网络服务器搭建的那些事(PV QPS Throughput) 转载

    一.前言: 从事后台sever开发的同学,代码开发完成之后,上线之前,总会进行各种黑盒白盒测试,压测.正确性测试... 而测试同学,会给开发同学一份测试报告,需要开发同学进行确认...问题来了,里面好 ...

  8. JVM监控启动参数

    服务端: JVM监控启动参数 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=52001 -Dcom.sun.ma ...

  9. 关于Vue的一些小技巧

    前言 用Vue开发一个网页并不难,但是也经常会遇到一些问题,其实大部分的问题都在文档中有所提及,再不然我们通过谷歌也能成功搜索到问题的答案,为了帮助小伙伴们提前踩坑,在遇到问题的时候,心里大概有个谱知 ...

  10. MySQL运维开发相关的所有工具

    http://www.ruzuojun.com/topic/592.html   Percona Toolkit 安装使用 http://cenalulu.github.io/mysql/mysql- ...