elasticSearch nested exist与missing查询
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-objecthttps://github.com/elastic/elasticsearch/issues/3495 `