Struct MoreLikeThisQuery

Source
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

Source

pub fn serialize<__S>( __self: &MoreLikeThisQuery, __serializer: __S, ) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Source§

impl MoreLikeThisQuery

Source

pub fn fields<I>(self, fields: I) -> Self
where I: IntoIterator, I::Item: ToString,

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.

Source

pub fn unlike<I>(self, unlike: I) -> Self
where I: IntoIterator, I::Item: Into<Like>,

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.

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

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

Source

pub fn stop_words<T>(self, stop_words: T) -> Self
where T: IntoIterator, T::Item: ToString,

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

Source

pub fn analyzer<T>(self, analyzer: T) -> Self
where T: ToString,

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

Source

pub fn minimum_should_match<T>(self, minimum_should_match: T) -> Self
where 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%”).

Source

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.

Source

pub fn boost_terms<T>(self, boost_terms: T) -> Self
where T: Into<f64>,

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.

Source

pub fn include(self, include: bool) -> Self

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

Source

pub fn boost<T>(self, boost: T) -> Self
where 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.

Source

pub fn name<S>(self, name: S) -> Self
where S: ToString,

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§

Source§

impl Clone for MoreLikeThisQuery

Source§

fn clone(&self) -> MoreLikeThisQuery

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MoreLikeThisQuery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<MoreLikeThisQuery> for Option<Query>

Source§

fn from(q: MoreLikeThisQuery) -> Self

Converts to this type from the input type.
Source§

impl From<MoreLikeThisQuery> for Query

Source§

fn from(q: MoreLikeThisQuery) -> Self

Converts to this type from the input type.
Source§

impl IntoIterator for MoreLikeThisQuery

Source§

type Item = MoreLikeThisQuery

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<MoreLikeThisQuery as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq<MoreLikeThisQuery> for Query

Source§

fn eq(&self, other: &MoreLikeThisQuery) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Query> for MoreLikeThisQuery

Source§

fn eq(&self, other: &Query) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for MoreLikeThisQuery

Source§

fn eq(&self, other: &MoreLikeThisQuery) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for MoreLikeThisQuery

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for MoreLikeThisQuery

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.