elasticsearch_dsl/search/suggesters/suggest_context_query.rs
1use crate::util::ShouldSkip;
2
3/// The completion suggester considers all documents in the index, but it is often desirable to
4/// serve suggestions filtered and/or boosted by some criteria. For example, you want to suggest
5/// song titles filtered by certain artists or you want to boost song titles based on their genre.
6///
7/// To achieve suggestion filtering and/or boosting, you can add context mappings while configuring
8/// a completion field. You can define multiple context mappings for a completion field. Every
9/// context mapping has a unique name and a type. There are two types: `category` and `geo`.
10/// Context mappings are configured under the contexts parameter in the field mapping.
11#[derive(Debug, Clone, PartialEq, Serialize)]
12pub struct SuggestContextQuery {
13 context: String,
14
15 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
16 boost: Option<f32>,
17
18 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
19 prefix: Option<bool>,
20}
21
22impl SuggestContextQuery {
23 /// Creates an instance of [SuggestContextQuery]
24 ///
25 /// - `context` - The value of the category to filter/boost on
26 pub fn new<T>(context: T) -> Self
27 where
28 T: ToString,
29 {
30 Self {
31 context: context.to_string(),
32 boost: None,
33 prefix: None,
34 }
35 }
36
37 /// The factor by which the score of the suggestion should be boosted, the score is computed by
38 /// multiplying the boost with the suggestion weight, defaults to `1`
39 pub fn boost<T>(mut self, boost: T) -> Self
40 where
41 T: num_traits::AsPrimitive<f32>,
42 {
43 self.boost = Some(boost.as_());
44 self
45 }
46
47 /// Whether the category value should be treated as a prefix or not. For example, if set to
48 /// `true`, you can filter category of _type1_, _type2_ and so on, by specifying a category
49 /// prefix of type. Defaults to `false`
50 pub fn prefix(mut self, prefix: bool) -> Self {
51 self.prefix = Some(prefix);
52 self
53 }
54}
55
56impl IntoIterator for SuggestContextQuery {
57 type Item = Self;
58
59 type IntoIter = std::option::IntoIter<Self::Item>;
60
61 fn into_iter(self) -> Self::IntoIter {
62 Some(self).into_iter()
63 }
64}