How to match exactly one item in an array


(AlloVince) #1

Hi guys

I have a question when matching a field with array.

Here is my data:

Item1:

{
 "names": ["foo-123-sth-more", "bar-456-sth-more"]
}

Item2:

{
 "names": ["foo-456-sth-more", "bar-123-sth-more"]
}

My query is

{
    "query": {
        "match" : {
            "names" : {
                "query" : "foo 456",
                "operator" : "and"
            }
        }
    }
}

Both item1 and item2 will matched, but the expected result is only item2 match, every element in array should be consider as an independent text to handle.

Is there any one could tell me how to handle this case? thank you.


(Jaspreet Singh) #2

You should use nested datatypes. It is just the way arrays are indexed that shows the behaviour are you noticing.
Check this out - https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html