Shay
Shay

Reputation: 505

Searching for multiple values in a String array in Elastic

I have a field that I am indexing into Elasticsearch that is an array of strings. So, for example, here is what the string array will look like in two records:

Record 1: {"str1", str2", str3", "str4", "str5"}

Record 2: {"str1", str2", str6", "str7", "str8"}

Question 1: I want to be able to query for multiple strings in this array. For e.g. my query has "str1", "str2". "str3" as the search parameter. I want to search for records where the string array has any of these three strings

Question 2: For the scenario above will Record 1 return with a higher score than record 2 (since all three strings are in the array for record 1 but only two are there in record 2).

Is this possible at all? Can you please help with what the query should look like and if the scoring works the way I stated.

Upvotes: 0

Views: 2415

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

You can index them as an array, such as:

{
    "myArrayField":  [ "str1", str2", str3", "str4", "str5" ],
    ...
}

You would then be able to query a number of ways, the simplest for your case being a match query (which is analyzed):

{
    "match" : {
        "myArrayField" : "str1 str2 str3"
    }
}

Or a terms query (which is not analyzed):

{
    "terms" : { 
        "myArrayField" : [ "str1", "str2", "str3" ]
    }
}

And Yes, matches against more query terms will receive a higher score, so Record 1 would be scored higher than Record 2.

Upvotes: 2

Related Questions