Searching on a list of tags that all must match


#1

Hi,

Im pretty new to ElasticSearch and I am trying to build a Search Template in Mustache that takes an array of tags. At this point I am able to pass a params object containing the tags array, however, the problems start when I am trying to build an dynamic Search Template in Mustache.

The Tags array can contain from 1 to 6++ tags, which is the reason for the need of a dynamic Search Template.

I have tried using the Terms property, together with "minimum_should_match" at 100%, but it seems like its deprecated? Im running on ElasticSearch 2.3 at the moment.

My params object looks like this:
{
tags: [ 'tag1', 'tag2', 'tag3' ]
}

And at this point I am stuck with this code for the Tags part of the search template:
{{#tags}}
"filter": {
"terms": {
"tags": ["{{tags}}"]
}
}
{{/tags}}

This specific code now only gets the correct content when the Tags array contains only one tag. If it contains more the one tag, I got an parse error:
[query_parsing_exception] Failed to parse, with { index="recipe-popularity-v1-2018-01-11@15:28:58" }

Kind regards
LarsDolvik


(Simon Willnauer) #2

you can do something like this:

GET _search/template
{
    "source": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}",
    "params": {
        "clauses": [
            { "term": { "tags" : "tag1" } },
            { "term": { "tags" : "tag2" } },
            { "term": { "tags" : "tag3" } }
        ]
   }
}

simon


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.