More like this with MLT

Overview

ElasticUtils exposes ElasticSearch More Like This API with the MLT class.

For example:

mlt = MLT(2034, index='addon_index', doctype='addon')

This creates an MLT that will return documents that are like document 2034 of type addon in the addon_index.

You can specify an S and the MLT will derive the index, doctype, ES object, and also use the search specified by the S in the body of the More Like This request. This allows you to get documents like the one specified that also meet query and filter criteria. For example:

s = S().filter(product='firefox')
mlt = MLT(2034, s=s)

You can specify which fields to use with the fields argument. If you don’t, then ElasticSearch will use all the fields.

You can specify additional parameters. See the documentation on the moreLikeThis query.

See also

http://www.elasticsearch.org/guide/reference/api/more-like-this.html
ElasticSearch guide on More Like This API
http://www.elasticsearch.org/guide/reference/query-dsl/mlt-query.html
ElasticSearch guide on the moreLikeThis query which specifies the additional parameters you can use.

API

class elasticutils.MLT(id_, s=None, fields=None, index=None, doctype=None, es=None, **query_params)

Represents a lazy ElasticSearch More Like This API request.

This is lazy in the sense that it doesn’t evaluate and execute the ElasticSearch request unless you force it to by iterating over it or getting the length of the search results.

For example:

>>> mlt = MLT(2034, index='addons_index', doctype='addon')
>>> num_related_documents = len(mlt)
>>> num_related_documents = list(mlt)
__init__(id_, s=None, fields=None, index=None, doctype=None, es=None, **query_params)

When the MLT is evaluated, it generates a list of dict results.

Parameters:
  • id – The id of the document we want to find more like.
  • s – An instance of an S. The query is passed in the body of the more like this request.
  • fields – A list of fields to use for more like this.
  • index – The index to use. Falls back to the first index listed in s.
  • doctype – The doctype to use. Falls back to the first doctype listed in s.
  • es – The ES object to use. If you don’t provide one, then it will create one for you.
  • query_params – Any additional query parameters for the more like this call.

Note

You must specify either an s or the index and doctype arguments. Omitting them will result in a ValueError.

get_es()

Returns an ES

  • If there’s an s, then it returns that ES.
  • If the es was provided in the constructor, then it returns that ES.
  • Otherwise, it creates a new ES and returns that.

Override this if that behavior isn’t correct for you.

raw()

Build query and passes to ElasticSearch, then returns the raw format returned.

Project Versions

Table Of Contents

Previous topic

Searching with S

Next topic

Getting an ES

This Page