2016-06-01 139 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