多级嵌套查询
Multi Level Nested Queries
我有一个多层嵌套文档。我想基于多个嵌套查询进行查询,所有条件都必须匹配。
例子
文档 1
{
"publishing_rule": {
"publishing_orders": [{
"transporters": [{
"fteid": "81"
}],
"active": false
},
{
"transporters": [{
"fteid": "82"
}],
"active": true
}
]
}
}
文档 2
{
"publishing_rule": {
"publishing_orders": [{
"transporters": [{
"fteid": "81"
}],
"active": true
},
{
"transporters": [{
"fteid": "82"
}],
"active": false
}
]
}
}
我想获取符合以下条件的所有文档
publishing_rule.publishing_orders.active = true
AND
publishing_rule.publishing_orders.transporters.fteid = '81'
active
和 transporters.fteid
应该是同一对象的一部分。
我尝试创建以下映射
{
"mappings": {
"_doc": {
"properties": {
"publishing_rule.publishing_orders": {
"type": "nested",
"properties": {
"transporters": {
"type": "nested"
}
}
}
}
}
}
}
并在下面的查询中使用
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
}
]
}
}
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
}
}
但我没有得到预期的结果。返回两个文档的查询。
我希望结果中只有文档 2。
您的查询实际上会查找匹配 active = true
或 fteid = 81
但不会同时匹配两者的任何文档。文档 1 和文档 2 满足了这些条件。这就是您获得这两个的原因。
这个查询应该有效
{
"query": {
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
}
}
}
}
请注意,我使用单个 nested
作为入口点,然后使用内部 nested
,这是为了让两个过滤器一起在文档中进行搜索。
更新
映射
GET /myindex/_mapping
{
"mappings": {
"_doc": {
"properties": {
"publishing_rule": {
"properties": {
"publishing_orders": {
"type": "nested",
"properties": {
"active": {
"type": "boolean"
},
"transporters": {
"type": "nested",
"properties": {
"fteid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
}
}
ES 结果
文件 2
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.89712,
"hits": [
{
"_index": "myindex",
"_type": "_doc",
"_id": "AWzu__bgqsCjtPMt7kG_",
"_score": 1.89712,
"_source": {
"publishing_rule": {
"publishing_orders": [
{
"transporters": [
{
"fteid": "81" // matched
}
],
"active": true // matched
},
{
"transporters": [
{
"fteid": "82"
}
],
"active": true
}
]
}
}
}
]
}
}
希望对您有所帮助
我看到@deerawan 的回答足以解决您提出的问题。由于您不接受他的回答,我相信您正在寻找的是在结果中单独获取嵌套文档。我修改了@deerawan 的查询以单独包含与查询匹配的嵌套文档
{
"_source": false,
"query": {
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
},
"inner_hits": {}
}
}
}
这应该会给您以下响应
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_index": "nest",
"_type": "doc",
"_id": "1234",
"_score": 1.3862944,
"inner_hits": {
"publishing_rule.publishing_orders": {
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_nested": {
"field": "publishing_rule.publishing_orders",
"offset": 0
},
"_score": 1.3862944,
"_source": {
"transporters": [
{
"fteid": "81"
}
],
"active": true
}
}
]
}
}
}
}
]
}
}
我有一个多层嵌套文档。我想基于多个嵌套查询进行查询,所有条件都必须匹配。
例子
文档 1
{
"publishing_rule": {
"publishing_orders": [{
"transporters": [{
"fteid": "81"
}],
"active": false
},
{
"transporters": [{
"fteid": "82"
}],
"active": true
}
]
}
}
文档 2
{
"publishing_rule": {
"publishing_orders": [{
"transporters": [{
"fteid": "81"
}],
"active": true
},
{
"transporters": [{
"fteid": "82"
}],
"active": false
}
]
}
}
我想获取符合以下条件的所有文档
publishing_rule.publishing_orders.active = true
AND
publishing_rule.publishing_orders.transporters.fteid = '81'
active
和 transporters.fteid
应该是同一对象的一部分。
我尝试创建以下映射
{
"mappings": {
"_doc": {
"properties": {
"publishing_rule.publishing_orders": {
"type": "nested",
"properties": {
"transporters": {
"type": "nested"
}
}
}
}
}
}
}
并在下面的查询中使用
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
}
]
}
}
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
}
}
但我没有得到预期的结果。返回两个文档的查询。
我希望结果中只有文档 2。
您的查询实际上会查找匹配 active = true
或 fteid = 81
但不会同时匹配两者的任何文档。文档 1 和文档 2 满足了这些条件。这就是您获得这两个的原因。
这个查询应该有效
{
"query": {
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
}
}
}
}
请注意,我使用单个 nested
作为入口点,然后使用内部 nested
,这是为了让两个过滤器一起在文档中进行搜索。
更新
映射
GET /myindex/_mapping
{
"mappings": {
"_doc": {
"properties": {
"publishing_rule": {
"properties": {
"publishing_orders": {
"type": "nested",
"properties": {
"active": {
"type": "boolean"
},
"transporters": {
"type": "nested",
"properties": {
"fteid": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
}
}
}
ES 结果
文件 2
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.89712,
"hits": [
{
"_index": "myindex",
"_type": "_doc",
"_id": "AWzu__bgqsCjtPMt7kG_",
"_score": 1.89712,
"_source": {
"publishing_rule": {
"publishing_orders": [
{
"transporters": [
{
"fteid": "81" // matched
}
],
"active": true // matched
},
{
"transporters": [
{
"fteid": "82"
}
],
"active": true
}
]
}
}
}
]
}
}
希望对您有所帮助
我看到@deerawan 的回答足以解决您提出的问题。由于您不接受他的回答,我相信您正在寻找的是在结果中单独获取嵌套文档。我修改了@deerawan 的查询以单独包含与查询匹配的嵌套文档
{
"_source": false,
"query": {
"nested": {
"path": "publishing_rule.publishing_orders",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.active": true
}
},
{
"nested": {
"path": "publishing_rule.publishing_orders.transporters",
"query": {
"bool": {
"must": [
{
"match": {
"publishing_rule.publishing_orders.transporters.fteid": "81"
}
}
]
}
}
}
}
]
}
},
"inner_hits": {}
}
}
}
这应该会给您以下响应
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_index": "nest",
"_type": "doc",
"_id": "1234",
"_score": 1.3862944,
"inner_hits": {
"publishing_rule.publishing_orders": {
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_nested": {
"field": "publishing_rule.publishing_orders",
"offset": 0
},
"_score": 1.3862944,
"_source": {
"transporters": [
{
"fteid": "81"
}
],
"active": true
}
}
]
}
}
}
}
]
}
}