elasticsearch 嵌套文档的弹性查询
我有一个嵌套文档,如下所示:
elasticsearch 嵌套文档的弹性查询,
elasticsearch,
elasticsearch,我有一个嵌套文档,如下所示: "someField": "hello", "users": [ { "name": "John", "surname": "Doe", "age": 2 } ] 据此,上述各项应匹配: GET /_search { "query": { "exists" : { "field" : "users" } } } 鉴于以下情况不应出现: "someField": "hello", "users": []
"someField": "hello",
"users": [
{
"name": "John",
"surname": "Doe",
"age": 2
}
]
据此,上述各项应匹配:
GET /_search
{
"query": {
"exists" : { "field" : "users" }
}
}
鉴于以下情况不应出现:
"someField": "hello",
"users": []
但不幸的是,两者并不匹配。有什么想法吗?Elasticsearch博客上提到的例子指的是字符串和字符串类型数组,而不是嵌套类型 以下查询应适用于您:
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "users"
}
}
]
}
}
}
}
}
此外,您还可以参考以了解更多信息,其中讨论了此使用模式。使用以下索引映射:
{
"index_name": {
"mappings": {
"object_name": {
"dynamic": "strict",
"properties": {
"nested_field_name": {
"type": "nested",
"properties": {
"some_property": {
"type": "keyword"
}
}
}
}
}
}
}
}
我需要使用此查询:
GET /index_name/_search
{
"query": {
"nested": {
"path": "nested_field_name",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "nested_field_name.some_property"
}
}
]
}
}
}
}
}
Elasticsearch 5.4.3版这对我很有用
GET /type/_search?pretty=true
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "outcome",
"query": {
"exists": {
"field": "outcome.outcomeName"
}
}
}
}
]
}
}
}
任何人都知道为什么嵌套域DSL必须设计得如此复杂。