elasticsearch

elasticsearch query nested array of objects


Hi I am trying to get a query to filter based on values in an array of objects, the structure is like this

{
  "_index": "test",
  "_type": "home",
  "_id": "1247816",
  "_score": 1,
  "_source": {
    "TranCust": {
      "CustId": 1247816,
      "sourceNodeName": "SRC"
    },
    "TranList": [
      {
        "TranId": 2431015,
        "batchNr": "211"
      },
      {
        "TranId": 2431016,
        "batchNr": "213"
      }
    ]
  }
}

as an example, i would like to find all documents with a TranId of 2431015, my query looks like this

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

it seems to return no results, is there a better way to try and write this query ?

EDIT, here are the mappings put in

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

Solution

  • ok, so after lots of attempts this is how i got it to work

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