0
How can i fetch nested object in elastic search ? 

{
            "_index": "userinfo",
            "_type": "userdetails",
            "_id": "2",
            "_score": 1,
            "_source": {
               "id": "2",
               "name": "Robert Mark",
               "age": 42,
               "email": "[email protected]",
               "userType": {
                  "id": "3",
                  "type": "End User"
               },
               "hobbies": [
                  {
                     "id": "3",
                     "description": "Writing Books"
                  },
                  {
                     "id": "4",
                     "description": "Gardening"
                  }
               ]
            }
         }

This is my json structure i want to fetch all the records where description under hobbies are "Gardening" .

I am new to elastic search please help me for this if any one knows .

"description": "Gardening"

2 Answers 2

1

This is your query:

{
  "query": {
    "nested": {
      "path": "hobbies",
      "query": {
        "match": {
          "hobbies.description": "Gardening"
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

israelst is correct if you precreated a nested mapping type for your hobbies object. By default though, it uses object instead of `nested.

{
  "query" : {
    "match" : {
      "hobbies.description" : "Gardening"
    }
  }
}

Note that the queries are practically the same and, in fact, my example is a subset of the nested version.

1 Comment

My Hobby Object is already mapped as type nested but still your solution is not working @Field(type = FieldType.Nested) private List<Hobby> hobbies; anyway thanks for the answer.

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.