How to work on the filter in the elasticsearch template?

576 Views Asked by At

I have similar kind of data set to work on with Elasticsearch.

  {
    "id" :1,
    "title" : "Toy Story 1",
    "year" : 1995 ,
    "category" : "Adventure"
    },
    {
    "id" :2,
    "title" : "Jumanji",
    "year" : 1995,
    "category" : "Adventure"
    },
    {
    "id" :3,
    "title" : "Grumpier Old Men",
    "year" : 1996,
    "category" : "Comedy"
    },
    {
    "id" :4,
    "title" : "Toy Story 2",
    "year" : 1996,
    "category" : "Action"
    },
    {
    "id" :5,
    "title" : "Toy Story 3"
    "year" : 1997
    "category" : "Comedy"
    },
    {
    "id" :6,
    "title" : "Toy Story 4"
    "year" : 2019
    "category" : "Comedy"
    }

Here is the mapping for my index.

{
    "movies": {
        "mappings": {
            "properties": {
                "category": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "year": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

I am using the search-template for retrieving the data. I have created a template and querying the data by passing the id of the template.

You can refer the below link for the same. Search-template

I am using postman for creating the template and querying the data using the template.

I created the template as below :

POST _scripts/test

{
    "script": {
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "title": "{{query_string}}"
                }
            }
        }
    }
}

POST _search/template

{
    "id": "test", 
    "params": {
        "query_string": "toy"
    }
}

This is working fine. I am getting the result as expected.

Now I am stuck with the filtering option in the template.

I want to filter on the category. I want add the category in the template and pass the values as ["Comedy", "Adventure"]

How can we add the filters in the template and pass the array values to it?

1

There are 1 best solutions below

16
On BEST ANSWER

You can do it this way. First modify your script template like this:

POST _scripts/test
{
  "script": {
    "lang": "mustache",
    "source": """
      {
        "query": {
          "bool": {
            "must": [
              {
                "match": {
                  "title": "{{query_string}}"
                }
              }
            ],
            "filter": [
              {
                "terms": {
                  "category.keyword": {{#toJson}}categories{{/toJson}}
                }
              }
            ]
          }
        }
      }
    """
  }
}

Note that the above can only be executed inside Kibana Dev Tools (because of the triple quotes (""").

If you're executing this somewhere else, you need to JSON-encode the query first:

POST _scripts/test
{
  "script": {
    "lang": "mustache",
    "source": "{\n      \"query\": {\n        \"bool\": {\n          \"must\": [\n            {\n              \"match\": {\n                \"title\": \"{{query_string}}\"\n              }\n            }\n          ],\n          \"filter\": [\n            {\n              \"terms\": {\n                \"category.keyword\": {{#toJson}}categories{{\/toJson}}\n              }\n            }\n          ]\n        }\n      }\n    }"
  }
}

Then you can call it like this:

POST _search/template
{
  "id": "test",
  "params": {
    "query_string": "toy",
    "categories": [
      "Comedy",
      "Adventure"
    ]
  }
}

It will yield this query:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "toy"
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "category": [
              "Comedy",
              "Adventure"
            ]
          }
        }
      ]
    }
  }
}