0

We have an index of Users e.g.

{
   "name": "Eli",
   "cars": [
        { "model": "Honda", "color": "Red" },
        { "model": "Honda", "color": "Blue" },
        { "model": "Toyota", "color": "Red" }
   ]
}

{
   "name": "Don",
   "cars": [
        { "model": "Honda", "color": "Blue" },
        { "model": "Honda", "color": "Black" },
        { "model": "Toyota", "color": "Red" }
   ]
}

We are trying to retrieve all the users with a Red Honda, but we couldn't find a way to do it in ElasticSearch

1 Answer 1

1

Since i dont know which elasticsearch version you are using, i'm referencing to the current.

What you are looking at is the following:

https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html and https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html and https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html

with the nested mapping you can create a query like the following:

{
 "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "cars",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "cars.model": "honda"
                    }
                  },
                  {
                    "term": {
                      "cars.color": "red"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

link to example: https://www.found.no/play/gist/91c5a6c8c9fe81928b1cc497f8740a3f (click run)

Be aware, this is only working when you are working with nested objects! The mapping must know this.

Sign up to request clarification or add additional context in comments.

Comments

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.