manticore_openapi/apis/
search_api.rs

1/*
2 * Manticore Search Client
3 *
4 * Сlient for Manticore Search.
5 *
6 * The version of the OpenAPI document: 3.3.1
7 * Contact: info@manticoresearch.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11use super::{configuration, Error};
12use crate::{apis::ResponseContent, models};
13use reqwest;
14use serde::{Deserialize, Serialize};
15
16/// struct for typed errors of method [`percolate`]
17#[derive(Debug, Clone, Serialize, Deserialize)]
18#[serde(untagged)]
19pub enum PercolateError {
20    DefaultResponse(models::ErrorResponse),
21    UnknownValue(serde_json::Value),
22}
23
24/// struct for typed errors of method [`search`]
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum SearchError {
28    DefaultResponse(models::ErrorResponse),
29    UnknownValue(serde_json::Value),
30}
31
32/// Performs a percolate search.  This method must be used only on percolate indexes.  Expects two parameters: the index name and an object with array of documents to be tested. An example of the documents object:    ```   {     \"query\":     {       \"percolate\":       {         \"document\":         {           \"content\":\"sample content\"         }       }     }   }   ```  Responds with an object with matched stored queries:     ```   {     'timed_out':false,     'hits':     {       'total':2,       'max_score':1,       'hits':       [         {           '_index':'idx_pq_1',           '_type':'doc',           '_id':'2',           '_score':'1',           '_source':           {             'query':             {               'match':{'title':'some'}             }           }         },         {           '_index':'idx_pq_1',           '_type':'doc',           '_id':'5',           '_score':'1',           '_source':           {             'query':             {               'ql':'some | none'             }           }         }       ]     }   }   ```
33pub async fn percolate(
34    configuration: &configuration::Configuration,
35    index: &str,
36    percolate_request: models::PercolateRequest,
37) -> Result<models::SearchResponse, Error<PercolateError>> {
38    let local_var_configuration = configuration;
39
40    let local_var_client = &local_var_configuration.client;
41
42    let local_var_uri_str = format!(
43        "{}/pq/{index}/search",
44        local_var_configuration.base_path,
45        index = crate::apis::urlencode(index)
46    );
47    let mut local_var_req_builder =
48        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
49
50    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
51        local_var_req_builder =
52            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
53    }
54    local_var_req_builder = local_var_req_builder.json(&percolate_request);
55
56    let local_var_req = local_var_req_builder.build()?;
57    let local_var_resp = local_var_client.execute(local_var_req).await?;
58
59    let local_var_status = local_var_resp.status();
60    let local_var_content = local_var_resp.text().await?;
61
62    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
63        serde_json::from_str(&local_var_content).map_err(Error::from)
64    } else {
65        let local_var_entity: Option<PercolateError> =
66            serde_json::from_str(&local_var_content).ok();
67        let local_var_error = ResponseContent {
68            status: local_var_status,
69            content: local_var_content,
70            entity: local_var_entity,
71        };
72        Err(Error::ResponseError(local_var_error))
73    }
74}
75
76///  The method expects an object with the following mandatory properties: * the name of the index to search * the match query object For details, see the documentation on [**SearchRequest**](SearchRequest.md) The method returns an object with the following properties: - took: the time taken to execute the search query. - timed_out: a boolean indicating whether the query timed out. - hits: an object with the following properties:    - total: the total number of hits found.    - hits: an array of hit objects, where each hit object represents a matched document. Each hit object has the following properties:      - _id: the ID of the matched document.      - _score: the score of the matched document.      - _source: the source data of the matched document.  In addition, if profiling is enabled, the response will include an additional array with profiling information attached. Here is an example search response:    ```   {     'took':10,     'timed_out':false,     'hits':     {       'total':2,       'hits':       [         {'_id':'1','_score':1,'_source':{'gid':11}},         {'_id':'2','_score':1,'_source':{'gid':12}}       ]     }   }   ```  For more information about the match query syntax and additional parameters that can be added to request and response, please see the documentation [here](https://manual.manticoresearch.com/Searching/Full_text_matching/Basic_usage#HTTP-JSON).
77pub async fn search(
78    configuration: &configuration::Configuration,
79    search_request: models::SearchRequest,
80) -> Result<models::SearchResponse, Error<SearchError>> {
81    let local_var_configuration = configuration;
82
83    let local_var_client = &local_var_configuration.client;
84
85    let local_var_uri_str = format!("{}/search", local_var_configuration.base_path);
86    let mut local_var_req_builder =
87        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
88
89    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
90        local_var_req_builder =
91            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
92    }
93    local_var_req_builder = local_var_req_builder.json(&search_request);
94    /*println!(
95        "raw request = {}",
96        serde_json::to_string(&search_request).unwrap()
97    );*/
98
99    let local_var_req = local_var_req_builder.build()?;
100    let local_var_resp = local_var_client.execute(local_var_req).await?;
101
102    let local_var_status = local_var_resp.status();
103    let local_var_content = local_var_resp.text().await?;
104
105    //println!("raw response = {}", &local_var_content);
106    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
107        serde_json::from_str(&local_var_content).map_err(Error::from)
108    } else {
109        let local_var_entity: Option<SearchError> = serde_json::from_str(&local_var_content).ok();
110        let local_var_error = ResponseContent {
111            status: local_var_status,
112            content: local_var_content,
113            entity: local_var_entity,
114        };
115        Err(Error::ResponseError(local_var_error))
116    }
117}