zeroentropy_community/resources/
queries.rs

1use crate::client::Client;
2use crate::error::Result;
3use crate::types::{
4    Filter, LatencyMode, TopDocumentsResponse, TopPagesResponse, TopSnippetsResponse,
5};
6use serde::Serialize;
7
8/// Queries resource for searching documents
9pub struct Queries<'a> {
10    client: &'a Client,
11}
12
13impl<'a> Queries<'a> {
14    pub(crate) fn new(client: &'a Client) -> Self {
15        Self { client }
16    }
17
18    /// Search for top documents matching a query
19    ///
20    /// # Arguments
21    /// * `collection_name` - Name of the collection to search
22    /// * `query` - Natural language query
23    /// * `k` - Number of documents to return (1-2048)
24    /// * `filter` - Optional metadata filter
25    /// * `include_metadata` - Whether to include metadata in results
26    /// * `latency_mode` - Latency/quality tradeoff
27    /// * `reranker` - Optional reranker model ID
28    pub async fn top_documents(
29        &self,
30        collection_name: impl Into<String>,
31        query: impl Into<String>,
32        k: u32,
33        filter: Option<Filter>,
34        include_metadata: Option<bool>,
35        latency_mode: Option<LatencyMode>,
36        reranker: Option<String>,
37    ) -> Result<TopDocumentsResponse> {
38        #[derive(Serialize)]
39        struct Request {
40            collection_name: String,
41            query: String,
42            k: u32,
43            #[serde(skip_serializing_if = "Option::is_none")]
44            filter: Option<Filter>,
45            #[serde(skip_serializing_if = "Option::is_none")]
46            include_metadata: Option<bool>,
47            #[serde(skip_serializing_if = "Option::is_none")]
48            latency_mode: Option<LatencyMode>,
49            #[serde(skip_serializing_if = "Option::is_none")]
50            reranker: Option<String>,
51        }
52
53        let body = Request {
54            collection_name: collection_name.into(),
55            query: query.into(),
56            k,
57            filter,
58            include_metadata,
59            latency_mode,
60            reranker,
61        };
62
63        self.client.post("/queries/top-documents", &body).await
64    }
65
66    /// Search for top pages matching a query
67    ///
68    /// # Arguments
69    /// * `collection_name` - Name of the collection to search
70    /// * `query` - Natural language query
71    /// * `k` - Number of pages to return (1-1024)
72    /// * `filter` - Optional metadata filter
73    /// * `include_content` - Whether to include page content
74    /// * `latency_mode` - Latency/quality tradeoff
75    pub async fn top_pages(
76        &self,
77        collection_name: impl Into<String>,
78        query: impl Into<String>,
79        k: u32,
80        filter: Option<Filter>,
81        include_content: Option<bool>,
82        latency_mode: Option<LatencyMode>,
83    ) -> Result<TopPagesResponse> {
84        #[derive(Serialize)]
85        struct Request {
86            collection_name: String,
87            query: String,
88            k: u32,
89            #[serde(skip_serializing_if = "Option::is_none")]
90            filter: Option<Filter>,
91            #[serde(skip_serializing_if = "Option::is_none")]
92            include_content: Option<bool>,
93            #[serde(skip_serializing_if = "Option::is_none")]
94            latency_mode: Option<LatencyMode>,
95        }
96
97        let body = Request {
98            collection_name: collection_name.into(),
99            query: query.into(),
100            k,
101            filter,
102            include_content,
103            latency_mode,
104        };
105
106        self.client.post("/queries/top-pages", &body).await
107    }
108
109    /// Search for top snippets matching a query
110    ///
111    /// # Arguments
112    /// * `collection_name` - Name of the collection to search
113    /// * `query` - Natural language query
114    /// * `k` - Number of snippets to return
115    /// * `filter` - Optional metadata filter
116    /// * `include_document_metadata` - Whether to include document metadata
117    /// * `precise_responses` - Longer snippets (around 2000 chars vs 200 chars)
118    /// * `reranker` - Optional reranker model ID
119    pub async fn top_snippets(
120        &self,
121        collection_name: impl Into<String>,
122        query: impl Into<String>,
123        k: u32,
124        filter: Option<Filter>,
125        include_document_metadata: Option<bool>,
126        precise_responses: Option<bool>,
127        reranker: Option<String>,
128    ) -> Result<TopSnippetsResponse> {
129        #[derive(Serialize)]
130        struct Request {
131            collection_name: String,
132            query: String,
133            k: u32,
134            #[serde(skip_serializing_if = "Option::is_none")]
135            filter: Option<Filter>,
136            #[serde(skip_serializing_if = "Option::is_none")]
137            include_document_metadata: Option<bool>,
138            #[serde(skip_serializing_if = "Option::is_none")]
139            precise_responses: Option<bool>,
140            #[serde(skip_serializing_if = "Option::is_none")]
141            reranker: Option<String>,
142        }
143
144        let body = Request {
145            collection_name: collection_name.into(),
146            query: query.into(),
147            k,
148            filter,
149            include_document_metadata,
150            precise_responses,
151            reranker,
152        };
153
154        self.client.post("/queries/top-snippets", &body).await
155    }
156}