Elasticsearch-dsl-py: How to achieve more like this functionality?

Created on 30 Mar 2017  路  4Comments  路  Source: elastic/elasticsearch-dsl-py

Please, explain how to achieve more-like-this functionality.

This what works via the REST (POST to http://localhost:9200/_search):

{
        "query": {
            "more_like_this" : {
                "fields" : ["content", "summary"],
                "like" : [
                {
                    "_index" : "article",
                    "_type": "article_document",
                    "_id" : "118"
                }
                ],
                "min_term_freq" : 1,
                "max_query_terms" : 12
            }
        }
    }

Most helpful comment

s = Search()
s = s.query(MoreLikeThis(like={'_id': 118, '_index': 'article', '_type': 'article_document'}, fields=['content', 'summary']))
print(s.to_dict())

This gives you

{'query': {'more_like_this': {'like': {'_id': 118, '_index': 'article', '_type': 'article_document'}, 'fields': ['content', 'summary']}}}

Which is the query you have there.

All 4 comments

from elasticsearch_dsl.query import MoreLikeThis
from elasticsearch_dsl Search

my_text = 'I want to find something similar'

s = Search()
s = s.query(MoreLikeThis(like=my_text, fields=['content', 'summary']))
# You can also exclude fields from the result to make the response quicker in the normal way
# s = s.source(exclude=["sentences", "text"])
response = s.execute().to_dict()

@eamonnmag:

Awesome! Thanks. Do you perhaps know how to do it based on the id of the document (+index, +type), like in my example?

s = Search()
s = s.query(MoreLikeThis(like={'_id': 118, '_index': 'article', '_type': 'article_document'}, fields=['content', 'summary']))
print(s.to_dict())

This gives you

{'query': {'more_like_this': {'like': {'_id': 118, '_index': 'article', '_type': 'article_document'}, 'fields': ['content', 'summary']}}}

Which is the query you have there.

@eamonnmag:

Awesome! Thanks for you help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

primoz-k picture primoz-k  路  4Comments

njoannin picture njoannin  路  3Comments

mortada picture mortada  路  3Comments

gabrielpjordao picture gabrielpjordao  路  3Comments

berinhard picture berinhard  路  3Comments