我能够使用elasticsearch Explain API为我的more like this查询结果获得解释。然而,我在python中无法实现同样的功能,因为显然explain不支持More like this。这是我尝试过的。
def execute_mlt_query(self, like_text, field, min_term_freq=1, min_doc_freq=1):
s = Search(using=self.client, index=self.index_name)
# Build the MLT query dynamically
mlt_query = Q("more_like_this", fields=[field], like=like_text, min_term_freq=min_term_freq, min_doc_freq=min_doc_freq)
s = s.query(mlt_query)
result = s.execute()
return self.get_docs_from_hits(result,mlt_query)
def get_docs_from_hits(self, result, mlt_query):
for hit in result:
self.similar_documents.append({
"doc_id": hit.meta.id,
"score": hit.meta.score,
"label": hit.to_dict()['yes'],
})
explanation = self.client.explain(self.client, index=self.index_name, doc_type="_doc", id=hit.meta.id, body=mlt_query.to_dict())
print(explanation)
但我得到了这个错误。
**raise HTTP_EXCEPTIONS.get(status_code,TransportError)(elasticsearch.exceptions.RequestError:请求错误(400,'parsing_exception','请求不支持[more_like_this]')
这是我试图实现的,但在Python中,
import org.apache.lucene.search.Explanation;
import org.elasticsearch.action.explain.ExplainRequest;
import org.elasticsearch.action.explain.ExplainResponse;
ExplainRequest request = new ExplainRequest(ESQuery.index_name, "_doc", hit.getId());
request.query(QueryBuilders.moreLikeThisQuery(fields,texts,null).minTermFreq(1));
ExplainResponse response = client.explain(request, RequestOptions.DEFAULT);
Explanation explanation = response.getExplanation();
String exp_str = explanation.toString();
1条答案
按热度按时间c0vxltue1#
Elasticsearch解释API支持MLT查询。我用下面的Python代码得到了解释。
1.首先动态构建MLT查询
1.对Search对象使用explain方法
使用Meta打印每个命中的解释