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.
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.
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
}
}
In this example,{{kw}}
and{{size}}
Is a variable in the template, throughparams
Objects pass specific values.
toJSON
The characteristics of the form aresource
Use a string to define, and escape characters are required. For example:
GET cars/_search/template
{
"source": "{ "query": { "match": {{#toJson}}parameter{{/toJson}} }}",
"params": {
"parameter": {
"remark": "真正的数值"
}
}
}
usejoin
The 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": ["大众", "标致", "奔驰", "宝马"]
}
}
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
}
}
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}}"
}
}
}
}
}
When calling the template, passid
To specify a specific template and pass the corresponding parameters. For example:
GET index_name/_search/template
{
"id": "my_test_template",
"params": {
"kw": "自定义template的参数值"
}
}
The defined template information can be queried through GET request, for example:
GET _scripts/my_test_template
If you need to delete a defined template, you can use the DELETE request, for example:
DELETE _scripts/my_test_template
Elasticsearch provides_render/template
API to validate templates.