ElasticSearch 支持是否定义了固定排名策略?



每次我使用相同的查询时,例如下面的代码:

{
"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是否支持我们可以定义的这种策略,比如排名策略?每次我只需要输入查询。

Elasticsearch 支持模板查询,您可以在其中注册查询并将查询字符串作为参数传递

例:

  1. 创建 .scripts 索引(如果不存在)
PUT <server::port>/.scripts
  1. 为上述情况注册查询模板
 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"
                 ]
              }
           }
        ]
     }
  }    

} }

  1. 使用query_string参数调用搜索模板
 POST <server::port>/index>/_search/template {    
    "template": {
        "id": "<template_name>"    
    },    
    "params": {
        "query_string": "will smith"    
    }      
 }

最新更新