我有一个多层嵌套文档。我想基于所有条件都必须匹配的多个嵌套查询进行查询。
示例
文档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。
答案 0 :(得分:1)
您的查询实际上将查看与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
}
]
}
}
}
]
}
}
希望有帮助
答案 1 :(得分:0)
我看到@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
}
}
]
}
}
}
}
]
}
}