2015-04-21 57 views
0

每次我使用相同的查詢,如以下代碼: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" ] 
      } 
     } 
    ] 
}} 

每次我使用不同的查詢字,但是PARAMS是相同的。

elasticsearch是否支持我們可以定義的這種策略,比如排名策略?每次我只需要輸入查詢。

回答

1

Elasticsearch支持template查詢,你可以註冊一個查詢,並通過查詢字符串作爲PARAM

例子:

  1. 創建.scripts中指數如果不存在
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" 
           ] 
           } 
          } 
         ] 
        } 
        }  
    

    } }

  • 調用搜索模板query_string param
  • POST <server::port>/index>/_search/template {  
        "template": { 
         "id": "<template_name>"  
        },  
        "params": { 
         "query_string": "will smith"  
        }  
    } 
    
    +0

    這是從這個有點不同:http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html#_stored_templates。我試過官方的例子,但無法得到我的查詢結果。你對如何使用url:/ _search而不是/ _search/template有任何想法嗎? – navins

    +0

    如果您打算使用_search端點,您需要將模板查詢更改爲以下內容https://gist.github.com/keety/610613edbbbd8dce8e6b – keety

    +0

    謝謝,它正在工作。 – navins