I was able to get explanations for my more like this query results using elasticsearch Explain API. However, I'm failing to achieve the same in python because apparently explain does not support More like this. Here is what I've tried.
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)
But I get this error.
raise HTTP_EXCEPTIONS.get(status_code, TransportError)( elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', 'request does not support [more_like_this]')
Here is what I'm trying to achieve but in 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();
Elasticsearch explain API supports MLT query. I got explanations using following code in python.
mlt_query = Q("more_like_this", fields=[field], like=like_text, min_term_freq=min_term_freq, min_doc_freq=min_doc_freq)
s = Search(using=self.client, index=self.index_name)
s = s.query(mlt_query)
explanation = s.params(explain=True).execute()
Print the explanation for each hit using hit.meta.explanation