0

I have the following objects in elasticsearch players index.

GET /players/_search

"hits": [
     {
        "_index": "players",
        "_type": "1",
        "_id": "rRI5A8mVRUKKTA08bK90Vw",
        "_score": 0.2712221,
        "_source": {
           "id": "1",
           "first_name": "Lebron",
           "last_name": "James",
           "numbers": [
              {
                 "number": "23",
                 "team": {
                    "city": "Cliveland",
                    "name": "Cavaliers"
                 }
              },
              {
                 "number": "6",
                 "team": {
                    "city": "Maimi",
                    "name": "Heat"
                 }
              }
           ]
        }
     }
  ]

Now I want to search players who played in "maimi" and had number "23" (there is no such player in this index). I tried

GET /players/_search
{
    "query": { 
        "bool" : {
            "must" : [
                {
                    "term" : {
                        "numbers.number" : "23"
                    }
                },
                {   "term" : {
                        "numbers.team.city" : "maimi" 
                    }
                }
            ]
        }
    }
}

But elasticsearch still found LeBron. How should I do the correct request to elasticsearch?

1 Answer 1

2

It's because of the structure of your data and how it's indexed : arrays inner objects are flattened internally.

Actually, your document is indexed in ElasticSearch like this :

numbers.number:     [23,6]
numbers.team.city:  [Cliveland,Maimi]
numbers.team.name:  [Cavaliers,Heat]

The association between inner objects are lost though and searching for Cliveland Heat number 6 would match this document too.

You should use a nested object mapping to solve this : the inner objects will be kept as they are.

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.