elasticsearch elasticsearch查询嵌套对象数组,elasticsearch,elasticsearch" /> elasticsearch elasticsearch查询嵌套对象数组,elasticsearch,elasticsearch" />

elasticsearch elasticsearch查询嵌套对象数组

elasticsearch elasticsearch查询嵌套对象数组,elasticsearch,elasticsearch,嗨,我正在尝试获取一个查询,以便根据对象数组中的值进行筛选,其结构如下 { "_index": "test", "_type": "home", "_id": "1247816", "_score": 1, "_source": { "TranCust": { "CustId": 1247816, "sourceNodeName": "SRC" }, "TranList": [ { "TranId":

嗨,我正在尝试获取一个查询,以便根据对象数组中的值进行筛选,其结构如下

{
  "_index": "test",
  "_type": "home",
  "_id": "1247816",
  "_score": 1,
  "_source": {
    "TranCust": {
      "CustId": 1247816,
      "sourceNodeName": "SRC"
    },
    "TranList": [
      {
        "TranId": 2431015,
        "batchNr": "211"
      },
      {
        "TranId": 2431016,
        "batchNr": "213"
      }
    ]
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "TranList",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "TranId": "2431015"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
例如,我想查找TranId为2431015的所有文档,我的查询如下

{
  "_index": "test",
  "_type": "home",
  "_id": "1247816",
  "_score": 1,
  "_source": {
    "TranCust": {
      "CustId": 1247816,
      "sourceNodeName": "SRC"
    },
    "TranList": [
      {
        "TranId": 2431015,
        "batchNr": "211"
      },
      {
        "TranId": 2431016,
        "batchNr": "213"
      }
    ]
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "TranList",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "TranId": "2431015"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
它似乎没有返回任何结果,有没有更好的方法来尝试和编写此查询

编辑, 以下是输入的映射

{
 "mappings": {
    "home": {
        "properties": {
            "TranCust": {
                "type": "object"
                }
            },
            "TranList": {
                "type": "nested"
            }
        }
    }
 }
}

好的,经过多次尝试,这就是我如何让它工作的

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

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

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


你确定你的对象存储为
嵌套类型
,而不是
对象
?我已经在问题中添加了映射。你能详细说明你的答案是如何解决问题的吗?这对我很有效。