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§
Source§impl MoreLikeThisQuery
impl MoreLikeThisQuery
pub fn serialize<__S>(
__self: &MoreLikeThisQuery,
__serializer: __S,
) -> Result<__S::Ok, __S::Error>where
__S: Serializer,
Source§impl MoreLikeThisQuery
impl MoreLikeThisQuery
Sourcepub fn fields<I>(self, fields: I) -> Self
pub fn fields<I>(self, fields: I) -> Self
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.
Sourcepub fn unlike<I>(self, unlike: I) -> Self
pub fn unlike<I>(self, unlike: I) -> Self
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.
Sourcepub fn max_query_terms(self, max_query_terms: i64) -> Self
pub fn max_query_terms(self, max_query_terms: i64) -> Self
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.
Sourcepub fn min_term_freq(self, min_term_freq: i64) -> Self
pub fn min_term_freq(self, min_term_freq: i64) -> Self
The minimum term frequency below which the terms will be ignored from the input document. Defaults to 2.
Sourcepub fn min_doc_freq(self, min_doc_freq: i64) -> Self
pub fn min_doc_freq(self, min_doc_freq: i64) -> Self
The minimum document frequency below which the terms will be ignored from the input document. Defaults to 5.
Sourcepub fn max_doc_freq(self, max_doc_freq: i64) -> Self
pub fn max_doc_freq(self, max_doc_freq: i64) -> Self
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).
Sourcepub fn min_word_length(self, min_word_length: i64) -> Self
pub fn min_word_length(self, min_word_length: i64) -> Self
The minimum word length below which the terms will be ignored. Defaults to 0.
Sourcepub fn max_word_length(self, max_word_length: i64) -> Self
pub fn max_word_length(self, max_word_length: i64) -> Self
The maximum word length above which the terms will be ignored. Defaults to unbounded (0).
Sourcepub fn stop_words<T>(self, stop_words: T) -> Self
pub fn stop_words<T>(self, stop_words: T) -> Self
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”.
Sourcepub fn analyzer<T>(self, analyzer: T) -> Selfwhere
T: ToString,
pub fn analyzer<T>(self, analyzer: T) -> Selfwhere
T: ToString,
The analyzer that is used to analyze the free form text.
Defaults to the analyzer associated with the first field in fields
.
Sourcepub fn minimum_should_match<T>(self, minimum_should_match: T) -> Selfwhere
T: ToString,
pub fn minimum_should_match<T>(self, minimum_should_match: T) -> Selfwhere
T: ToString,
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%”).
Sourcepub fn fail_on_unsupported_field(self, fail_on_unsupported_field: bool) -> Self
pub fn fail_on_unsupported_field(self, fail_on_unsupported_field: bool) -> Self
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.
Sourcepub fn boost_terms<T>(self, boost_terms: T) -> Self
pub fn boost_terms<T>(self, boost_terms: T) -> Self
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.
Sourcepub fn include(self, include: bool) -> Self
pub fn include(self, include: bool) -> Self
Specifies whether the input documents should also be included in the search results returned. Defaults to false
.
Sourcepub fn boost<T>(self, boost: T) -> Selfwhere
T: AsPrimitive<f32>,
pub fn boost<T>(self, boost: T) -> Selfwhere
T: AsPrimitive<f32>,
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.
Trait Implementations§
Source§impl Clone for MoreLikeThisQuery
impl Clone for MoreLikeThisQuery
Source§fn clone(&self) -> MoreLikeThisQuery
fn clone(&self) -> MoreLikeThisQuery
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more