elasticsearch_dsl/search/queries/params/
pinned_query.rs

1use crate::Set;
2
3/// Ids or documents to filter by
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum PinnedQueryValues {
7    /// [Document IDs](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html)
8    /// listed in the order they are to appear in results.
9    Ids(Set<String>),
10
11    /// Documents listed in the order they are to appear in results.
12    Docs(Set<PinnedDocument>),
13}
14
15/// Pinned document
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
17pub struct PinnedDocument {
18    _index: String,
19    _id: String,
20}
21
22impl PinnedDocument {
23    /// Creates an instance of [`PinnedDocument`]
24    pub fn new<IX, ID>(index: IX, id: ID) -> Self
25    where
26        IX: ToString,
27        ID: ToString,
28    {
29        Self {
30            _index: index.to_string(),
31            _id: id.to_string(),
32        }
33    }
34}
35
36impl PinnedQueryValues {
37    /// Creates an instance of [`PinnedQueryValues`] with [`PinnedQueryValues::Ids`]
38    pub fn ids<I>(ids: I) -> Self
39    where
40        I: IntoIterator,
41        I::Item: ToString,
42    {
43        Self::Ids(ids.into_iter().map(|x| x.to_string()).collect())
44    }
45
46    /// Creates an instance of [`PinnedQueryValues`] with [`PinnedQueryValues::Docs`]
47    pub fn docs<I>(docs: I) -> Self
48    where
49        I: IntoIterator<Item = PinnedDocument>,
50    {
51        Self::Docs(docs.into_iter().collect())
52    }
53}