1

If I deliver the empty params (see below) I would like to get all documents from Elasticsearch. How can I achieve this?

One solution is that I could write all the existing categories into an array. But I have more than 100 categories and this will not be a good solution.

Can someone please help me? Is it possible to ignore the terms if the array is empty?

POST _scripts/test{"script": {
"lang": "mustache",
"source": {
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "terms": {
          "category": [
            "{{#category}}",
            "{{.}}",
            "{{/category}}"
          ]}}}}}}}

If I execute the below query the results will be empty:

GET poi/_search/template{
"id": "test", 
"params": {
    "category" : [""]
}}
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Mark Müller
  • 13
  • 1
  • 3

1 Answers1

1

The best way to achieve this would be to proceed like this with a JSON array:

POST _scripts/test
{
  "script": {
    "lang": "mustache",
    "source": """
    {
      "query": {
        "bool": {
          "must": {
            "match_all": {}
          },
          "filter": [
            {{#category}}
            {
              "terms": {
                "category": {{#toJson}}category.values{{/toJson}}
              }
            }
            {{/category}}
          ]
        }
      }
    }
    """
  }
}

Then you can execute this search template like this:

GET poi/_search/template
{
  "id": "test", 
  "params": {
    "category" : {
      "values": ["cat1", "cat2", "cat3"]
    }
  }
}

And if you don't want to specify any categories, like this:

GET poi/_search/template
{
  "id": "test", 
  "params": {
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
  • Hi thanks for the fast reply :) ... "filter": { {{/category}}... {{/category}} ... } I tried to execute this query in kibana devtools and in postman and got an error message "bad string". I have no idea where I can execute this query. I tried it with "" but it didnt work. Could you pls tell how to execute this query? – Mark Müller Jun 02 '20 at 07:31
  • You need to run `POST _scripts/test` in Kibana Dev Tools to install the search template and then you can run the `GET poi/_search/template` anywhere (Kibana or Postman). I modified something, can you check my updated answer and try again? – Val Jun 02 '20 at 07:32
  • I cant install the search tempplate {{#category}} {{/category}} this cause this error message: { "error" : { "root_cause" : [ { "type" : "x_content_parse_exception", "reason" : "[10:21] [stored script source] failed to parse field [source]" }... | Bad string syntax error – Mark Müller Jun 02 '20 at 07:47
  • You're right, see my updated answer, mustache templates need to be specified differently, i.e. with triple quotes. – Val Jun 02 '20 at 07:56
  • sry, was my mistake thank you very much this is working :) – Mark Müller Jun 02 '20 at 08:10
  • sry, unfortunately the last query to get all documents is not working. I got this error message:{ "error" : { "root_cause" : [ { "type" : "illegal_argument_exception", "reason" : "query malformed, empty clause found at [9:11]" } ], "type" : "illegal_argument_exception", "reason" : "query malformed, empty clause found at [9:11]" }, "status" : 400 } – Mark Müller Jun 02 '20 at 09:47
  • Makes sense. Please check my updated answer and try again – Val Jun 02 '20 at 09:49
  • Thx you very much :). – Mark Müller Jun 02 '20 at 09:53