elasticsearch 多级嵌套查询,elasticsearch,kibana,elasticsearch,Kibana" /> elasticsearch 多级嵌套查询,elasticsearch,kibana,elasticsearch,Kibana" />

elasticsearch 多级嵌套查询

elasticsearch 多级嵌套查询,elasticsearch,kibana,elasticsearch,Kibana,我有一个多层嵌套文档。我想基于多个嵌套查询进行查询,所有条件都必须匹配 范例 文件1 { "publishing_rule": { "publishing_orders": [{ "transporters": [{ "fteid": "81" }], "active": false },

我有一个多层嵌套文档。我想基于多个嵌套查询进行查询,所有条件都必须匹配

范例

文件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中满足了这些标准。这就是为什么你有那两个

这个查询应该有效

{
“查询”:{
“嵌套”:{
“路径”:“发布规则。发布订单”,
“查询”:{
“布尔”:{
“必须”:[
{
“匹配”:{
“发布规则。发布订单。活动”:true
}
},
{
“嵌套”:{
“路径”:“发布规则。发布订单。运输商”,
“查询”:{
“布尔”:{
“必须”:[
{
“匹配”:{
“发布规则。发布订单。运输商。fteid:“81”
}
}
]
}
}
}
}
]
}
}
}
}
}
请注意,我使用一个
嵌套的
作为入口点,然后使用内部的
嵌套的
,这是为了在文档中同时启用两个过滤器进行搜索

更新

映射

GET/myindex/\u映射
{
“映射”:{
“_doc”:{
“财产”:{
“发布规则”:{
“财产”:{
“发布订单”:{
“类型”:“嵌套”,
“财产”:{
“活动”:{
“类型”:“布尔”
},
“运输者”:{
“类型”:“嵌套”,
“财产”:{
“fteid”:{
“类型”:“文本”,
“字段”:{
“关键字”:{
“类型”:“关键字”,
“忽略上面的内容”:256
}
}
}
}
}
}
}
}
}
}
}
}
ES结果

文件2

{
    "publishing_rule": {
        "publishing_orders": [{
                "transporters": [{
                    "fteid": "81"
                }],
                "active": true
            },
            {
                "transporters": [{
                    "fteid": "82"
                }],
                "active": false
            }
        ]
    }
}
{
“take”:6,
“超时”:false,
“_碎片”:{
“总数”:5,
“成功”:5,
“失败”:0
},
“点击次数”:{
“总数”:1,
“最高分数”:1.89712,
“点击次数”:[
{
“_索引”:“我的索引”,
“\u类型”:“\u单据”,
“\u id”:“AWzu\uu bgqsCjtPMt7kG”,
“_分数”:1.89712,
“_来源”:{
“发布规则”:{
“发布订单”:[
{
“运输者”:[
{
“fteid”:“81”//匹配
}
],
“活动”:真//匹配
},
{
“运输者”:[
{
“fteid”:“82”
}
],
“活动”:真
}
]
}
}
}
]
}
}

希望能有所帮助

我发现@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
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

上面的查询也给出了结果中的两个文档。这是我的字段映射{“映射”:{“文档”:{“属性”:{“发布规则.发布顺序”:{“类型”:“嵌套”,“属性”:{“传输者”:{“类型”:“嵌套”} } } }@OnkarKore很有趣。我使用了您的映射并得到了文档2作为结果。我已经更新了我的答案,以包括我的映射和搜索结果。在我的示例中,
81
实际上是
COM-5fbd027b-c154-46b7-b887-0b9c37438c81
,它被标记化,无法进行精确匹配。因此我无法得到正确的响应。我改变了将
文本
映射到
关键字
。您的答案非常有效。谢谢。