Technology Sharing

Elasticsearch search templates: Reuse and share queries

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

In the daily development and operation of Elasticsearch (ES), we often encounter scenarios where similar queries need to be executed frequently. These queries may involve multiple fields, filter conditions, and aggregate analysis due to the complexity of business logic. In order to optimize these operations and improve development efficiency, Elasticsearch provides the function of search templates. This article will introduce how to use Elasticsearch's search templates to reuse and share queries.

1. Why use search templates?

In daily development, although most queries may be relatively simple, in some complex business scenarios (such as finance, medicine, etc.), a search may require the input or return of dozens of fields, and involve complex filtering conditions and aggregate analysis. At this time, manually writing each query is not only inefficient, but also prone to errors. Search templates are like stored procedures or functions in MySQL. They only need to be defined once, and the search function can be completed in the code by calling the template and passing parameters, which greatly improves development efficiency and query flexibility.

2. Entry Case of Search Template

2.1 Standard form

The basic form of a search template is to define a JSON object containing query conditions and pass_search/template API call. For example:

GET /index_name/_search/template
{
  "source": {
    "query": {
      "match": {
        "remark": "{{kw}}"
      }
    },
    "size": "{{size}}"
  },
  "params": {
    "kw": "真正的数值",
    "size": 100
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

In this example,{{kw}}and{{size}}Is a variable in the template, throughparamsObjects pass specific values.

2.2 toJSON format

toJSONThe characteristics of the form aresourceUse a string to define, and escape characters are required. For example:

GET cars/_search/template
{
  "source": "{ "query": { "match": {{#toJson}}parameter{{/toJson}} }}",
  "params": {
    "parameter": {
      "remark": "真正的数值"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.3 Join method parameter passing

usejoinThe method can concatenate the elements in the array into a string for query conditions. For example:

GET index_name/_search/template
{
  "source": {
    "query": {
      "match": {
        "remark": "{{#join delimiter=' '}}kw{{/join delimiter=' '}}"
      }
    }
  },
  "params": {
    "kw": ["大众", "标致", "奔驰", "宝马"]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2.4 Default Value Form

You can set default values ​​for parameters in the template. If the parameter is not passed when calling, the default value is used. For example:

GET index_name/_search/template
{
  "source": {
    "query": {
      "range": {
        "price": {
          "gte": "{{start}}",
          "lte": "{{end}}{{^end}}200000{{/end}}"
        }
      }
    }
  },
  "params": {
    "start": 100000
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

3. Remember template and implement repeated calls

3.1 Elasticsearch saves template

In order to reuse the template, we can save it in Elasticsearch. By specifying the name of the template, it is convenient to call it repeatedly later. For example:

POST _scripts/my_test_template
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "field_name": "{{kw}}"
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3.2 Calling template

When calling the template, passidTo specify a specific template and pass the corresponding parameters. For example:

GET index_name/_search/template
{
  "id": "my_test_template",
  "params": {
    "kw": "自定义template的参数值"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.3 Query defined template

The defined template information can be queried through GET request, for example:

GET _scripts/my_test_template
  • 1

3.4 Delete the defined template

If you need to delete a defined template, you can use the DELETE request, for example:

DELETE _scripts/my_test_template
  • 1

4. Advanced Application of Search Templates

4.1 Verification Template

Elasticsearch provides_render/template API to validate templates.