每次我使用相同的查询,例如下面的代码:
{
"bool": {
"should": [
{
"multi_match" : {
"query": "Will Smith",
"type": "cross_fields",
"fields": [ "first", "last" ],
"minimum_should_match": "50%"
}
},
{
"multi_match" : {
"query": "Will Smith",
"type": "cross_fields",
"fields": [ "*.edge" ]
}
}
]
}}
每次我使用不同的查询词,但参数是相同的。
elasticsearch是否支持我们可以定义的这种策略,比如排名策略?每次我只需要输入查询。
答案 0 :(得分:1)
Elasticsearch支持template查询,您可以在其中注册查询并将查询字符串作为参数传递
示例:
PUT <server::port>/.scripts
PUT <server::port>/_search/template/<template_name> { "template": { "query": { "bool": { "should": [ { "multi_match": { "query": "{{query_string}}", "type": "cross_fields", "fields": [ "first", "last" ], "minimum_should_match": "50%" } }, { "multi_match": { "query": "{query_string}", "type": "cross_fields", "fields": [ "*.edge" ] } } ] } }
} }
POST <server::port>/index>/_search/template { "template": { "id": "<template_name>" }, "params": { "query_string": "will smith" } }