elasticsearch_dsl/search/queries/params/
rewrite.rs

1use serde::ser::{Serialize, Serializer};
2
3/// Method used to rewrite the query.
4///
5/// > **This parameter is for expert users only. Changing the value of this
6/// > parameter can impact search performance and relevance.**
7///
8/// The `rewrite` parameter determines:
9/// - How Lucene calculates the relevance scores for each matching document
10/// - Whether Lucene changes the original query to a `bool` query or bit set
11/// - If changed to a `bool` query, which `term` query clauses are included
12///
13/// **Performance considerations for the rewrite parameter**
14///
15/// For most uses, we recommend using the `constant_score`,
16/// `constant_score_boolean`, or `top_terms_boost_N` rewrite methods.
17///
18/// Other methods calculate relevance scores. These score calculations
19/// are often expensive and do not improve query results.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Rewrite {
22    /// Uses the constant_score_boolean method for fewer matching terms.
23    /// Otherwise, this method finds all matching terms in sequence and returns
24    /// matching documents using a bit set.
25    ConstantScore,
26
27    /// Assigns each document a relevance score equal to the `boost` parameter.
28    ///
29    /// This method changes the original query to a
30    /// [`bool` query](crate::queries::BoolQuery). This `bool` query contains a
31    /// `should` clause and [`term` query](crate::queries::TermQuery)
32    /// for each matching term.
33    ///
34    /// This method can cause the final `bool` query to exceed the clause limit
35    /// in the
36    /// [`indices.query.bool.max_clause_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#indices-query-bool-max-clause-count)
37    /// setting. If the query exceeds this limit, Elasticsearch returns an error.
38    ConstantScoreBoolean,
39
40    /// Calculates a relevance score for each matching document.
41    ///
42    /// This method changes the original query to a
43    /// [`bool` query](crate::queries::BoolQuery). This `bool` query contains a
44    /// `should` clause and [`term` query](crate::queries::TermQuery)
45    /// for each matching term.
46    ///
47    /// This method can cause the final `bool` query to exceed the clause limit
48    /// in the
49    /// [`indices.query.bool.max_clause_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#indices-query-bool-max-clause-count)
50    /// setting. If the query exceeds this limit, Elasticsearch returns an error.
51    ScoringBoolean,
52
53    /// Calculates a relevance score for each matching document as if all terms
54    /// had the same frequency. This frequency is the maximum frequency of all
55    /// matching terms.
56    ///
57    /// This method changes the original query to a
58    /// [`bool` query](crate::queries::BoolQuery). This `bool` query contains a
59    /// `should` clause and [`term` query](crate::queries::TermQuery)
60    /// for each matching term.
61    ///
62    /// The final `bool` query only includes `term` queries for the top `N`
63    /// scoring terms.
64    ///
65    /// You can use this method to avoid exceeding the clause limit in the
66    /// [`indices.query.bool.max_clause_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#indices-query-bool-max-clause-count)
67    /// setting.
68    TopTermsBlendedFrequencies(u64),
69
70    /// Assigns each matching document a relevance score equal to the boost
71    /// parameter.
72    ///
73    /// This method changes the original query to a
74    /// [`bool` query](crate::queries::BoolQuery). This `bool` query contains a
75    /// `should` clause and [`term` query](crate::queries::TermQuery)
76    /// for each matching term.
77    ///
78    /// The final `bool` query only includes `term` queries for the top `N`
79    /// terms.
80    ///
81    /// You can use this method to avoid exceeding the clause limit in the
82    /// [`indices.query.bool.max_clause_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#indices-query-bool-max-clause-count)
83    /// setting.
84    TopTermsBoost(u64),
85
86    /// Calculates a relevance score for each matching document.
87    ///
88    /// This method changes the original query to a
89    /// [`bool` query](crate::queries::BoolQuery). This `bool` query contains a
90    /// `should` clause and [`term` query](crate::queries::TermQuery)
91    /// for each matching term.
92    ///
93    /// The final `bool` query only includes `term` queries for the top `N`
94    /// scoring terms.
95    ///
96    /// You can use this method to avoid exceeding the clause limit in the
97    /// [`indices.query.bool.max_clause_count`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-settings.html#indices-query-bool-max-clause-count)
98    /// setting.
99    TopTerms(u64),
100}
101
102impl Serialize for Rewrite {
103    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
104    where
105        S: Serializer,
106    {
107        match self {
108            Self::ConstantScore => "constant_score".serialize(serializer),
109            Self::ConstantScoreBoolean => "constant_score_boolean".serialize(serializer),
110            Self::ScoringBoolean => "scoring_boolean".serialize(serializer),
111            Self::TopTermsBlendedFrequencies(n) => {
112                format!("top_terms_blended_freqs_{n}").serialize(serializer)
113            }
114            Self::TopTermsBoost(n) => format!("top_terms_boost_{n}").serialize(serializer),
115            Self::TopTerms(n) => format!("top_terms_{n}").serialize(serializer),
116        }
117    }
118}