0

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.

1 Answer 1

2

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.

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.