2016-06-01 72 views
0

我對彈性搜索非常新穎,我們正在從Solr遷移到彈性搜索。作爲將現有Solr查詢轉換爲彈性搜索DSL查詢的遷移工作的一部分。Elasticsearch(版本2.3)功能分數查詢過濾式查詢

這是我使用功能評分功能部分完成的DSL查詢。

{ 
    "query": { 
    "function_score": { 
     "query": { 
     "filtered": { 
      "match": { 
      "name": "barack obama" 
      }, 
      "filter": { 
      "range": { 
       "relevance": { 
       "gte": 6 
       } 
      }, 
      "bool": { 
       "must_not": [ 
       { 
        "terms": { 
        "classIds": [ 
         199, 
         220 
        ], 
        "execution": "and" 
        } 
       } 
       ], 
       "must": [ 
       { 
        "term": { 
        "classIds": 10597 
        } 
       } 
       ] 
      } 
      } 
     } 
     }, 
     "boost_mode": "replace", 
     "functions": [ 
     { 
      "script_score": { 
      "script": { 
       "lang": "groovy", 
       "file": "calculate-score", 
       "params": { 
       "relevance_boost": 1, 
       "class_penalize": 0.25 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

這個查詢在彈性搜索集羣上運行時返回錯誤。請幫我弄清楚這個問題。

這裏計算得分是groovy腳本和它的工作正常,我測試了簡單的查詢。

以下是錯誤響應:

{ 
    "error": { 
    "root_cause": [ 
     { 
     "type": "query_parsing_exception", 
     "reason": "[filtered] query does not support [match]", 
     "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0", 
     "line": 6, 
     "col": 11 
     } 
    ], 
    "type": "search_phase_execution_exception", 
    "reason": "all shards failed", 
    "phase": "query", 
    "grouped": true, 
    "failed_shards": [ 
     { 
     "shard": 0, 
     "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0", 
     "node": "NOAwAtVwQS25egu7AIaHEg", 
     "reason": { 
      "type": "query_parsing_exception", 
      "reason": "[filtered] query does not support [match]", 
      "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0", 
      "line": 6, 
      "col": 11 
     } 
     } 
    ] 
    }, 
    "status": 400 
} 

這裏是Solr的查詢我想轉換到彈性搜索:

SOLR QUERY (UNIQUE_NODE_CORE): q={!boost b="product(pow(field(relevance),1.0000),if(exists(query({!v='all_class_ids:226'})),0.25,1),if(exists(query({!v='all_class_ids:14106'})),0.25,1),if(exists(query({!v='all_class_ids:656'})),0.25,1))"} 
raw_name:"barack obama" 
&rows=1 
&start=0 
&sort=score desc,relevance desc 
-&fq=class_id:"10597" 
-fq=relevance:[6 TO *] 
-&fq=-all_class_ids:"14127" 
-&fq=-all_class_ids:"14106" 
-&fq=-all_class_ids:"226" 
&fl=ontology_id,url_friendly_name,name,score,raw_notable_for,property_207578 

只是需要幫助運行與功能評分過濾查詢。

回答

3

偉大的工作,你幾乎在那裏,你只是錯過了filtered查詢中的query節以包裝match查詢。同樣,range過濾器可以插入到bool/must中。我知道,真是一口。

{ 
    "query": { 
    "function_score": { 
     "query": { 
     "filtered": { 
      "query": { 
      "match": { 
       "name": "barack obama" 
      } 
      }, 
      "filter": { 
      "bool": { 
       "must_not": [ 
       { 
        "terms": { 
        "classIds": [ 
         199, 
         220 
        ], 
        "execution": "and" 
        } 
       } 
       ], 
       "must": [ 
       { 
        "range": { 
        "relevance": { 
         "gte": 6 
        } 
        } 
       }, 
       { 
        "term": { 
        "classIds": 10597 
        } 
       } 
       ] 
      } 
      } 
     } 
     }, 
     "boost_mode": "replace", 
     "functions": [ 
     { 
      "script_score": { 
      "script": { 
       "lang": "groovy", 
       "file": "calculate-score", 
       "params": { 
       "relevance_boost": 1, 
       "class_penalize": 0.25 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

注意,由於ES 2.0 filtered查詢已被棄用,你可以用一個bool/must/filter這樣的查詢重寫一遍:

{ 
    "query": { 
    "function_score": { 
     "query": { 
     "bool": { 
      "must": { 
      "match": { 
       "name": "barack obama" 
      } 
      }, 
      "filter": [ 
      { 
       "range": { 
       "relevance": { 
        "gte": 6 
       } 
       } 
      }, 
      { 
       "term": { 
       "classIds": 10597 
       } 
      } 
      ], 
      "must_not": [ 
      { 
       "terms": { 
       "classIds": [ 
        199, 
        220 
       ], 
       "execution": "and" 
       } 
      } 
      ] 
     } 
     }, 
     "boost_mode": "replace", 
     "functions": [ 
     { 
      "script_score": { 
      "script": { 
       "lang": "groovy", 
       "file": "calculate-score", 
       "params": { 
       "relevance_boost": 1, 
       "class_penalize": 0.25 
       } 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

這工作就像一個魅力!謝謝@Val! – geek

+0

太棒了,很高興幫助! – Val

+0

嘿,你可以看看這個問題對我來說,當試圖使用groovy函數得分時遇到性能問題:https://stackoverflow.com/questions/37871163/elasticsearch-query-with-function-score-is-running-more-低於10倍,慢 – geek