elasticsearch查询嵌套的对象数组

use*_*435 5 elasticsearch

嗨我想尝试根据对象数组中的值进行筛选,结构就像这样

{
  "_index": "test",
  "_type": "home",
  "_id": "1247816",
  "_score": 1,
  "_source": {
    "TranCust": {
      "CustId": 1247816,
      "sourceNodeName": "SRC"
    },
    "TranList": [
      {
        "TranId": 2431015,
        "batchNr": "211"
      },
      {
        "TranId": 2431016,
        "batchNr": "213"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

作为一个例子,我想找到TranId为2431015的所有文档,我的查询看起来像这样

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "TranList",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "TranId": "2431015"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它似乎没有返回任何结果,是否有更好的方法来尝试编写此查询?

编辑,这里是插入的映射

{
 "mappings": {
    "home": {
        "properties": {
            "TranCust": {
                "type": "object"
                }
            },
            "TranList": {
                "type": "nested"
            }
        }
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

use*_*435 8

好吧,经过多次尝试,这就是我如何让它发挥作用

{"query": {
"bool": {
  "must": [
    {
      "nested": {
        "path": "TranList", 
        "query": {
          "bool": {
            "must": [ 
              { "match": { "TranList.TranId ": "2431015" }}
            ]
    }}}}
  ]
}}
}
Run Code Online (Sandbox Code Playgroud)


tug*_*cem 8

不确定您的 ES 版本是什么,但以下内容应该适用于 ES 6.x+ 版本。您实际上并不需要用bool:must

{
    "query": {
        "nested" : {
            "path" : "TranList",
            "query" : {
                "bool" : {
                    "must" : [
                        { "match" : {"TranList.TranId" : "2431015"} }
                    ]    
                }
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)