WARNING: Version 1.6 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
More Like This Query
editMore Like This Query
editThe More Like This Query (MLT Query) finds documents that are "like" a given
set of documents. In order to do so, MLT selects a set of representative terms
of these input documents, forms a query using these terms, executes the query
and returns the results. The user controls the input documents, how the terms
should be selected and how the query is formed. more_like_this
can be
shortened to mlt
.
The simplest use case consists of asking for documents that are similar to a provided piece of text. Here, we are asking for all movies that have some text similar to "Once upon a time" in their "title" and in their "description" fields, limiting the number of selected terms to 12.
{ "more_like_this" : { "fields" : ["title", "description"], "like_text" : "Once upon a time", "min_term_freq" : 1, "max_query_terms" : 12 } }
Another use case consists of asking for similar documents to ones already existing in the index. In this case, the syntax to specify a document is similar to the one used in the Multi GET API.
{ "more_like_this" : { "fields" : ["title", "description"], "docs" : [ { "_index" : "imdb", "_type" : "movies", "_id" : "1" }, { "_index" : "imdb", "_type" : "movies", "_id" : "2" }], "min_term_freq" : 1, "max_query_terms" : 12 } }
Finally, users can also provide documents not necessarily present in the index using a syntax is similar to artificial documents.
{ "more_like_this" : { "fields" : ["name.first", "name.last"], "docs" : [ { "_index" : "marvel", "_type" : "quotes", "doc" : { "name": { "first": "Ben", "last": "Grimm" }, "tweet": "You got no idea what I'd... what I'd give to be invisible." } } }, { "_index" : "marvel", "_type" : "quotes", "_id" : "2" } ], "min_term_freq" : 1, "max_query_terms" : 12 } }
How it Works
editSuppose we wanted to find all documents similar to a given input document.
Obviously, the input document itself should be its best match for that type of
query. And the reason would be mostly, according to
Lucene scoring formula,
due to the terms with the highest tf-idf. Therefore, the terms of the input
document that have the highest tf-idf are good representatives of that
document, and could be used within a disjunctive query (or OR
) to retrieve
similar documents. The MLT query simply extracts the text from the input
document, analyzes it, usually using the same analyzer as the field, then
selects the top K terms with highest tf-idf to form a disjunctive query of
these terms.
The fields on which to perform MLT must be indexed and of type
string
. Additionally, when using like
with documents, either _source
must be enabled or the fields must be stored
or have term_vector
enabled.
In order to speed up analysis, it could help to store term vectors at index
time, but at the expense of disk usage.
For example, if we wish to perform MLT on the "title" and "tags.raw" fields,
we can explicitly store their term_vector
at index time. We can still
perform MLT on the "description" and "tags" fields, as _source
is enabled by
default, but there will be no speed up on analysis for these fields.
curl -s -XPUT 'http://localhost:9200/imdb/' -d '{ "mappings": { "movies": { "properties": { "title": { "type": "string", "term_vector": "yes" }, "description": { "type": "string" }, "tags": { "type": "string", "fields" : { "raw": { "type" : "string", "index" : "not_analyzed", "term_vector" : "yes" } } } } } } }
Parameters
editThe only required parameters are either docs
, ids
or like_text
, all
other parameters have sensible defaults. There are three types of parameters:
one to specify the document input, the other one for term selection and for
query formation.
Document Input Parameters
edit
|
The list of documents to find documents like it. The syntax to specify
documents is similar to the one used by the Multi GET API.
The text is fetched from |
|
A list of document ids, shortcut to |
|
The text to find documents like it. required if |
|
A list of the fields to run the more like this query against. Defaults to the
|
Term Selection Parameters
edit
|
The maximum number of query terms that will be selected. Increasing this value
gives greater accuracy at the expense of query execution speed. Defaults to
|
|
The minimum term frequency below which the terms will be ignored from the
input document. Defaults to |
|
The minimum document frequency below which the terms will be ignored from the
input document. Defaults to |
|
The maximum document frequency above which the terms will be ignored from the
input document. This could be useful in order to ignore highly frequent words
such as stop words. Defaults to unbounded ( |
|
The minimum word length below which the terms will be ignored. Defaults to |
|
The maximum word length above which the terms will be ignored. Defaults to unbounded ( |
|
An array of stop words. Any word in this set is considered "uninteresting" and ignored. If the analyzer allows for stop words, you might want to tell MLT to explicitly ignore them, as for the purposes of document similarity it seems reasonable to assume that "a stop word is never interesting". |
|
The analyzer that is used to analyze the free form text. Defaults to the
analyzer associated with the first field in |
Query Formation Parameters
edit
|
After the disjunctive query has been formed, this parameter controls the
number of terms that must match. The syntax is the same as the
minimum should match. (Defaults to |
|
[1.5.0]
Deprecated in 1.5.0. Replaced by |
|
Each term in the formed query could be further boosted by their tf-idf score.
This sets the boost factor to use when using this feature. Defaults to
deactivated ( |
|
Specifies whether the input documents should also be included in the search
results returned. Defaults to |
|
Sets the boost value of the whole query. Defaults to |