More like this query

The More Like This 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.

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.

  1. GET /_search
  2. {
  3. "query": {
  4. "more_like_this" : {
  5. "fields" : ["title", "description"],
  6. "like" : "Once upon a time",
  7. "min_term_freq" : 1,
  8. "max_query_terms" : 12
  9. }
  10. }
  11. }

A more complicated use case consists of mixing texts with documents 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.

  1. GET /_search
  2. {
  3. "query": {
  4. "more_like_this": {
  5. "fields": [ "title", "description" ],
  6. "like": [
  7. {
  8. "_index": "imdb",
  9. "_id": "1"
  10. },
  11. {
  12. "_index": "imdb",
  13. "_id": "2"
  14. },
  15. "and potentially some more text here as well"
  16. ],
  17. "min_term_freq": 1,
  18. "max_query_terms": 12
  19. }
  20. }
  21. }

Finally, users can mix some texts, a chosen set of documents but also provide documents not necessarily present in the index. To provide documents not present in the index, the syntax is similar to artificial documents.

  1. GET /_search
  2. {
  3. "query": {
  4. "more_like_this": {
  5. "fields": [ "name.first", "name.last" ],
  6. "like": [
  7. {
  8. "_index": "marvel",
  9. "doc": {
  10. "name": {
  11. "first": "Ben",
  12. "last": "Grimm"
  13. },
  14. "_doc": "You got no idea what I'd... what I'd give to be invisible."
  15. }
  16. },
  17. {
  18. "_index": "marvel",
  19. "_id": "2"
  20. }
  21. ],
  22. "min_term_freq": 1,
  23. "max_query_terms": 12
  24. }
  25. }
  26. }

How it Works

Suppose 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 at 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 text or keyword` . Additionally, when using like with documents, either _source must be enabled or the fields must be stored or store term_vector. In order to speed up analysis, it could help to store term vectors at index time.

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.

  1. PUT /imdb
  2. {
  3. "mappings": {
  4. "properties": {
  5. "title": {
  6. "type": "text",
  7. "term_vector": "yes"
  8. },
  9. "description": {
  10. "type": "text"
  11. },
  12. "tags": {
  13. "type": "text",
  14. "fields": {
  15. "raw": {
  16. "type": "text",
  17. "analyzer": "keyword",
  18. "term_vector": "yes"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

Parameters

The only required parameter is like, 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

like

The only required parameter of the MLT query is like and follows a versatile syntax, in which the user can specify free form text and/or a single or multiple documents (see examples above). The syntax to specify documents is similar to the one used by the Multi GET API. When specifying documents, the text is fetched from fields unless overridden in each document request. The text is analyzed by the analyzer at the field, but could also be overridden. The syntax to override the analyzer at the field follows a similar syntax to the per_field_analyzer parameter of the Term Vectors API. Additionally, to provide documents not necessarily present in the index, artificial documents are also supported.

unlike

The unlike parameter is used in conjunction with like in order not to select terms found in a chosen set of documents. In other words, we could ask for documents like: “Apple”, but unlike: “cake crumble tree”. The syntax is the same as like.

fields

A list of fields to fetch and analyze the text from.

Term Selection Parameters

max_query_terms

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 25.

min_term_freq

The minimum term frequency below which the terms will be ignored from the input document. Defaults to 2.

min_doc_freq

The minimum document frequency below which the terms will be ignored from the input document. Defaults to 5.

max_doc_freq

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 (Integer.MAX_VALUE, which is 2^31-1 or 2147483647).

min_word_length

The minimum word length below which the terms will be ignored. The old name min_word_len is deprecated. Defaults to 0.

max_word_length

The maximum word length above which the terms will be ignored. The old name max_word_len is deprecated. Defaults to unbounded (0).

stop_words

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”.

analyzer

The analyzer that is used to analyze the free form text. Defaults to the analyzer associated with the first field in fields.

Query Formation Parameters

minimum_should_match

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 “30%”).

fail_on_unsupported_field

Controls whether the query should fail (throw an exception) if any of the specified fields are not of the supported types (text or keyword). Set this to false to ignore the field and continue processing. Defaults to true.

boost_terms

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 (0). Any other positive value activates terms boosting with the given boost factor.

include

Specifies whether the input documents should also be included in the search results returned. Defaults to false.

boost

Sets the boost value of the whole query. Defaults to 1.0.

Alternative

To take more control over the construction of a query for similar documents it is worth considering writing custom client code to assemble selected terms from an example document into a Boolean query with the desired settings. The logic in more_like_this that selects “interesting” words from a piece of text is also accessible via the TermVectors API. For example, using the termvectors API it would be possible to present users with a selection of topical keywords found in a document’s text, allowing them to select words of interest to drill down on, rather than using the more “black-box” approach of matching used by more_like_this.