15

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"
            }
        }
    }
 }
}
2
  • Are you sure your objects are stores as nested type, rather than object? Commented Nov 9, 2015 at 9:35
  • i have added the mappings i put in to the question Commented Nov 9, 2015 at 9:38

3 Answers 3

15

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"
            }
          }]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this can be more simplfied , check this out - { "query": { "nested": { "path": "TranList", "query": { "match": { "TranList.TranId": "2431015" } } } } }
9

Not sure what was your ES version, but the following should ideally work for ES 6.x+ versions. You don't actually need to wrap your nested query with bool:must

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

Comments

3
{
  "query": {
    "query_string": {
      "default_field": "TranList.TranId",
      "query": "2431015"
    }
  }
}

1 Comment

Could you elaborate on how does your answer solve the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.