logo
pub struct MoreLikeThisQuery { /* private fields */ }
Expand description

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.

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.

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.

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.

To create a more_like_this query with like as a string on title field:

Query::more_like_this(["test"])
    .fields(["title"]);

To create a more_like_this query with string and document id fields on title and description with optional fields:

Query::more_like_this([Like::from(Document::new("123")), Like::from("test")])
    .fields(["title", "description"])
    .min_term_freq(1)
    .max_query_terms(12)
    .boost(1.2)
    .name("more_like_this");

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html

Implementations

A list of fields to fetch and analyze the text from. Defaults to the index.query.default_field index setting, which has a default value of *. The * value matches all fields eligible for term-level queries, excluding metadata fields.

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.

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.

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

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

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

The minimum word length below which the terms will be ignored. Defaults to 0.

The maximum word length above which the terms will be ignored. Defaults to unbounded (0).

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

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

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.

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.

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

Floating point number used to decrease or increase the relevance scores of a query. Defaults to 1.0.

You can use the boost parameter to adjust relevance scores for searches containing two or more queries.

Boost values are relative to the default value of 1.0. A boost value between 0 and 1.0 decreases the relevance score. A value greater than 1.0 increases the relevance score.

You can use named queries to track which queries matched returned documents. If named queries are used, the response includes a matched_queries property for each hit.

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#named-queries

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.