Skip to main content

opensearch_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/opensearch/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
38    /// [`PinnedQueryValues::Ids`]
39    pub fn ids<I>(ids: I) -> Self
40    where
41        I: IntoIterator,
42        I::Item: ToString,
43    {
44        Self::Ids(ids.into_iter().map(|x| x.to_string()).collect())
45    }
46
47    /// Creates an instance of [`PinnedQueryValues`] with
48    /// [`PinnedQueryValues::Docs`]
49    pub fn docs<I>(docs: I) -> Self
50    where
51        I: IntoIterator<Item = PinnedDocument>,
52    {
53        Self::Docs(docs.into_iter().collect())
54    }
55}