2022-10-20 18:00:07 +02:00
|
|
|
use std::fmt::Write;
|
2024-07-30 10:27:57 +02:00
|
|
|
use std::marker::PhantomData;
|
2022-10-20 18:00:07 +02:00
|
|
|
use std::panic::{catch_unwind, resume_unwind, UnwindSafe};
|
|
|
|
use std::time::Duration;
|
2021-02-18 22:10:50 +01:00
|
|
|
|
2021-02-18 19:50:52 +01:00
|
|
|
use actix_web::http::StatusCode;
|
2021-03-10 14:47:04 +01:00
|
|
|
use tokio::time::sleep;
|
2022-10-09 19:43:51 +02:00
|
|
|
use urlencoding::encode as urlencode;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
use super::encoder::Encoder;
|
2022-10-20 18:00:07 +02:00
|
|
|
use super::service::Service;
|
2024-11-04 17:51:40 +01:00
|
|
|
use super::{Owned, Shared, Value};
|
2023-09-11 16:50:53 +02:00
|
|
|
use crate::json;
|
2022-10-09 19:43:51 +02:00
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
pub struct Index<'a, State = Owned> {
|
2021-02-18 19:50:52 +01:00
|
|
|
pub uid: String,
|
|
|
|
pub service: &'a Service,
|
2024-07-30 10:27:57 +02:00
|
|
|
pub(super) encoder: Encoder,
|
|
|
|
pub(super) marker: PhantomData<State>,
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
impl<'a> Index<'a, Owned> {
|
|
|
|
pub fn to_shared(&self) -> Index<'a, Shared> {
|
|
|
|
Index {
|
|
|
|
uid: self.uid.clone(),
|
|
|
|
service: self.service,
|
|
|
|
encoder: self.encoder,
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 19:55:00 +01:00
|
|
|
pub async fn load_test_set(&self) -> u64 {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/documents", urlencode(self.uid.as_ref()));
|
2022-12-21 11:27:15 +08:00
|
|
|
let (response, code) = self
|
|
|
|
.service
|
|
|
|
.post_str(
|
|
|
|
url,
|
|
|
|
include_str!("../assets/test_set.json"),
|
2023-02-16 13:59:58 +01:00
|
|
|
vec![("content-type", "application/json")],
|
2022-12-21 11:27:15 +08:00
|
|
|
)
|
|
|
|
.await;
|
|
|
|
assert_eq!(code, 202);
|
|
|
|
let update_id = response["taskUid"].as_i64().unwrap();
|
|
|
|
self.wait_task(update_id as u64).await;
|
|
|
|
update_id as u64
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn load_test_set_ndjson(&self) -> u64 {
|
|
|
|
let url = format!("/indexes/{}/documents", urlencode(self.uid.as_ref()));
|
|
|
|
let (response, code) = self
|
|
|
|
.service
|
|
|
|
.post_str(
|
|
|
|
url,
|
|
|
|
include_str!("../assets/test_set.ndjson"),
|
2023-02-16 13:59:58 +01:00
|
|
|
vec![("content-type", "application/x-ndjson")],
|
2022-12-21 11:27:15 +08:00
|
|
|
)
|
|
|
|
.await;
|
2021-03-17 13:54:17 +01:00
|
|
|
assert_eq!(code, 202);
|
2022-05-17 11:17:32 +02:00
|
|
|
let update_id = response["taskUid"].as_i64().unwrap();
|
2021-12-02 16:03:26 +01:00
|
|
|
self.wait_task(update_id as u64).await;
|
2021-02-19 19:55:00 +01:00
|
|
|
update_id as u64
|
|
|
|
}
|
|
|
|
|
2021-05-31 16:03:39 +02:00
|
|
|
pub async fn create(&self, primary_key: Option<&str>) -> (Value, StatusCode) {
|
2024-07-30 10:27:57 +02:00
|
|
|
self._create(primary_key).await
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
2021-02-18 20:28:10 +01:00
|
|
|
|
2023-01-24 13:20:20 +01:00
|
|
|
pub async fn update_raw(&self, body: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.patch_encoded(url, body, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2021-02-18 20:28:10 +01:00
|
|
|
pub async fn update(&self, primary_key: Option<&str>) -> (Value, StatusCode) {
|
|
|
|
let body = json!({
|
|
|
|
"primaryKey": primary_key,
|
|
|
|
});
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}", urlencode(self.uid.as_ref()));
|
2021-02-18 20:28:10 +01:00
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
self.service.patch_encoded(url, body, self.encoder).await
|
2021-02-18 20:28:10 +01:00
|
|
|
}
|
2021-02-18 20:44:33 +01:00
|
|
|
|
|
|
|
pub async fn delete(&self) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}", urlencode(self.uid.as_ref()));
|
2021-02-18 20:44:33 +01:00
|
|
|
self.service.delete(url).await
|
|
|
|
}
|
2021-02-18 22:10:50 +01:00
|
|
|
|
|
|
|
pub async fn add_documents(
|
|
|
|
&self,
|
|
|
|
documents: Value,
|
|
|
|
primary_key: Option<&str>,
|
|
|
|
) -> (Value, StatusCode) {
|
2024-07-30 10:27:57 +02:00
|
|
|
self._add_documents(documents, primary_key).await
|
2021-02-18 22:10:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 13:59:58 +01:00
|
|
|
pub async fn raw_add_documents(
|
|
|
|
&self,
|
|
|
|
payload: &str,
|
2024-02-22 15:51:47 +01:00
|
|
|
headers: Vec<(&str, &str)>,
|
2023-02-16 13:59:58 +01:00
|
|
|
query_parameter: &str,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), query_parameter);
|
2024-02-22 15:51:47 +01:00
|
|
|
self.service.post_str(url, payload, headers).await
|
2023-02-16 13:59:58 +01:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:11:10 +01:00
|
|
|
pub async fn update_documents(
|
|
|
|
&self,
|
|
|
|
documents: Value,
|
|
|
|
primary_key: Option<&str>,
|
|
|
|
) -> (Value, StatusCode) {
|
2021-02-19 19:55:00 +01:00
|
|
|
let url = match primary_key {
|
2022-10-20 18:00:07 +02:00
|
|
|
Some(key) => {
|
|
|
|
format!("/indexes/{}/documents?primaryKey={}", urlencode(self.uid.as_ref()), key)
|
|
|
|
}
|
2022-10-09 19:43:51 +02:00
|
|
|
None => format!("/indexes/{}/documents", urlencode(self.uid.as_ref())),
|
2021-02-19 19:55:00 +01:00
|
|
|
};
|
2022-10-09 19:43:51 +02:00
|
|
|
self.service.put_encoded(url, documents, self.encoder).await
|
2021-02-19 19:55:00 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 13:59:58 +01:00
|
|
|
pub async fn raw_update_documents(
|
|
|
|
&self,
|
|
|
|
payload: &str,
|
|
|
|
content_type: Option<&str>,
|
|
|
|
query_parameter: &str,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), query_parameter);
|
|
|
|
|
|
|
|
if let Some(content_type) = content_type {
|
|
|
|
self.service.put_str(url, payload, vec![("Content-Type", content_type)]).await
|
|
|
|
} else {
|
|
|
|
self.service.put_str(url, payload, Vec::new()).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
pub async fn list_tasks(&self) -> (Value, StatusCode) {
|
2022-11-28 16:27:41 +01:00
|
|
|
let url = format!("/tasks?indexUids={}", self.uid);
|
2021-02-18 22:10:50 +01:00
|
|
|
self.service.get(url).await
|
|
|
|
}
|
2021-02-19 19:55:00 +01:00
|
|
|
|
2021-02-22 16:03:17 +01:00
|
|
|
pub async fn delete_document(&self, id: u64) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/documents/{}", urlencode(self.uid.as_ref()), id);
|
2021-02-22 16:03:17 +01:00
|
|
|
self.service.delete(url).await
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:34:25 +02:00
|
|
|
pub async fn delete_document_by_filter(&self, body: Value) -> (Value, StatusCode) {
|
2023-04-27 13:51:02 +02:00
|
|
|
let url = format!("/indexes/{}/documents/delete", urlencode(self.uid.as_ref()));
|
2023-04-11 19:34:25 +02:00
|
|
|
self.service.post_encoded(url, body, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:03:17 +01:00
|
|
|
pub async fn clear_all_documents(&self) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/documents", urlencode(self.uid.as_ref()));
|
2021-02-22 16:03:17 +01:00
|
|
|
self.service.delete(url).await
|
|
|
|
}
|
2021-02-23 14:13:43 +01:00
|
|
|
|
|
|
|
pub async fn delete_batch(&self, ids: Vec<u64>) -> (Value, StatusCode) {
|
2022-10-20 18:00:07 +02:00
|
|
|
let url = format!("/indexes/{}/documents/delete-batch", urlencode(self.uid.as_ref()));
|
2023-09-11 16:50:53 +02:00
|
|
|
self.service
|
|
|
|
.post_encoded(url, serde_json::to_value(&ids).unwrap().into(), self.encoder)
|
|
|
|
.await
|
2021-02-23 14:13:43 +01:00
|
|
|
}
|
2021-02-24 09:30:51 +01:00
|
|
|
|
2023-01-18 15:28:02 +01:00
|
|
|
pub async fn delete_batch_raw(&self, body: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents/delete-batch", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post_encoded(url, body, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:30:51 +01:00
|
|
|
pub async fn update_settings(&self, settings: Value) -> (Value, StatusCode) {
|
2024-07-30 10:27:57 +02:00
|
|
|
self._update_settings(settings).await
|
2021-02-24 09:30:51 +01:00
|
|
|
}
|
2021-02-24 10:14:36 +01:00
|
|
|
|
2023-01-11 17:04:24 +01:00
|
|
|
pub async fn update_settings_displayed_attributes(
|
|
|
|
&self,
|
|
|
|
settings: Value,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url =
|
|
|
|
format!("/indexes/{}/settings/displayed-attributes", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_searchable_attributes(
|
|
|
|
&self,
|
|
|
|
settings: Value,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url =
|
|
|
|
format!("/indexes/{}/settings/searchable-attributes", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_filterable_attributes(
|
|
|
|
&self,
|
|
|
|
settings: Value,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url =
|
|
|
|
format!("/indexes/{}/settings/filterable-attributes", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_sortable_attributes(
|
|
|
|
&self,
|
|
|
|
settings: Value,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/sortable-attributes", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_ranking_rules(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/ranking-rules", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_stop_words(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/stop-words", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_synonyms(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/synonyms", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_distinct_attribute(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/distinct-attribute", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_typo_tolerance(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/typo-tolerance", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.patch_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_faceting(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/faceting", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.patch_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_settings_pagination(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/pagination", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.patch_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2024-03-19 11:14:28 +01:00
|
|
|
pub async fn update_settings_search_cutoff_ms(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings/search-cutoff-ms", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.put_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:14:36 +01:00
|
|
|
pub async fn delete_settings(&self) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref()));
|
2021-02-24 10:14:36 +01:00
|
|
|
self.service.delete(url).await
|
|
|
|
}
|
2021-04-01 21:54:37 +03:00
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
pub async fn update_distinct_attribute(&self, value: Value) -> (Value, StatusCode) {
|
|
|
|
let url =
|
|
|
|
format!("/indexes/{}/settings/{}", urlencode(self.uid.as_ref()), "distinct-attribute");
|
|
|
|
self.service.put_encoded(url, value, self.encoder).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Index<'a, Shared> {
|
|
|
|
/// You cannot modify the content of a shared index, thus the delete_document_by_filter call
|
|
|
|
/// must fail. If the task successfully enqueue itself, we'll wait for the task to finishes,
|
|
|
|
/// and if it succeed the function will panic.
|
|
|
|
pub async fn delete_document_by_filter_fail(&self, body: Value) -> (Value, StatusCode) {
|
|
|
|
let (mut task, code) = self._delete_document_by_filter(body).await;
|
|
|
|
if code.is_success() {
|
|
|
|
task = self.wait_task(task.uid()).await;
|
|
|
|
if task.is_success() {
|
|
|
|
panic!(
|
|
|
|
"`delete_document_by_filter_fail` succeeded: {}",
|
|
|
|
serde_json::to_string_pretty(&task).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(task, code)
|
|
|
|
}
|
2024-10-02 16:23:21 +02:00
|
|
|
|
|
|
|
pub async fn delete_index_fail(&self) -> (Value, StatusCode) {
|
|
|
|
let (mut task, code) = self._delete().await;
|
|
|
|
if code.is_success() {
|
|
|
|
task = self.wait_task(task.uid()).await;
|
|
|
|
if task.is_success() {
|
|
|
|
panic!(
|
|
|
|
"`delete_index_fail` succeeded: {}",
|
|
|
|
serde_json::to_string_pretty(&task).unwrap()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(task, code)
|
|
|
|
}
|
2024-07-30 10:27:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
impl<State> Index<'_, State> {
|
|
|
|
pub async fn get(&self) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// add_documents is not allowed on shared index but we need to use it to initialize
|
|
|
|
/// a bunch of very common indexes in `common/mod.rs`.
|
|
|
|
pub(super) async fn _add_documents(
|
|
|
|
&self,
|
|
|
|
documents: Value,
|
|
|
|
primary_key: Option<&str>,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let url = match primary_key {
|
|
|
|
Some(key) => {
|
|
|
|
format!("/indexes/{}/documents?primaryKey={}", urlencode(self.uid.as_ref()), key)
|
|
|
|
}
|
|
|
|
None => format!("/indexes/{}/documents", urlencode(self.uid.as_ref())),
|
|
|
|
};
|
|
|
|
self.service.post_encoded(url, documents, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) async fn _update_settings(&self, settings: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.patch_encoded(url, settings, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) async fn _delete_document_by_filter(&self, body: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents/delete", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post_encoded(url, body, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) async fn _create(&self, primary_key: Option<&str>) -> (Value, StatusCode) {
|
|
|
|
let body = json!({
|
|
|
|
"uid": self.uid,
|
|
|
|
"primaryKey": primary_key,
|
|
|
|
});
|
|
|
|
self.service.post_encoded("/indexes", body, self.encoder).await
|
|
|
|
}
|
2024-10-02 16:23:21 +02:00
|
|
|
|
|
|
|
pub(super) async fn _delete(&self) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.delete(url).await
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:27:57 +02:00
|
|
|
pub async fn wait_task(&self, update_id: u64) -> Value {
|
|
|
|
// try several times to get status, or panic to not wait forever
|
|
|
|
let url = format!("/tasks/{}", update_id);
|
|
|
|
for _ in 0..100 {
|
|
|
|
let (response, status_code) = self.service.get(&url).await;
|
|
|
|
assert_eq!(200, status_code, "response: {}", response);
|
|
|
|
|
|
|
|
if response["status"] == "succeeded" || response["status"] == "failed" {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait 0.5 second.
|
|
|
|
sleep(Duration::from_millis(500)).await;
|
|
|
|
}
|
|
|
|
panic!("Timeout waiting for update id");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_task(&self, update_id: u64) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/tasks/{}", update_id);
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn filtered_tasks(
|
|
|
|
&self,
|
|
|
|
types: &[&str],
|
|
|
|
statuses: &[&str],
|
|
|
|
canceled_by: &[&str],
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let mut url = format!("/tasks?indexUids={}", self.uid);
|
|
|
|
if !types.is_empty() {
|
|
|
|
let _ = write!(url, "&types={}", types.join(","));
|
|
|
|
}
|
|
|
|
if !statuses.is_empty() {
|
|
|
|
let _ = write!(url, "&statuses={}", statuses.join(","));
|
|
|
|
}
|
|
|
|
if !canceled_by.is_empty() {
|
|
|
|
let _ = write!(url, "&canceledBy={}", canceled_by.join(","));
|
|
|
|
}
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_document(&self, id: u64, options: Option<Value>) -> (Value, StatusCode) {
|
|
|
|
let mut url = format!("/indexes/{}/documents/{}", urlencode(self.uid.as_ref()), id);
|
|
|
|
if let Some(options) = options {
|
|
|
|
write!(url, "{}", yaup::to_string(&options).unwrap()).unwrap();
|
|
|
|
}
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_document_by_filter(&self, payload: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents/fetch", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post(url, payload).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_all_documents_raw(&self, options: &str) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), options);
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_all_documents(&self, options: GetAllDocumentsOptions) -> (Value, StatusCode) {
|
|
|
|
let url = format!(
|
|
|
|
"/indexes/{}/documents{}",
|
|
|
|
urlencode(self.uid.as_ref()),
|
|
|
|
yaup::to_string(&options).unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn settings(&self) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/settings", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:54:37 +03:00
|
|
|
pub async fn stats(&self) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/stats", urlencode(self.uid.as_ref()));
|
2021-04-01 21:54:37 +03:00
|
|
|
self.service.get(url).await
|
|
|
|
}
|
2021-04-20 12:07:22 +02:00
|
|
|
|
2021-07-06 11:54:37 +02:00
|
|
|
/// Performs both GET and POST search queries
|
|
|
|
pub async fn search(
|
|
|
|
&self,
|
|
|
|
query: Value,
|
|
|
|
test: impl Fn(Value, StatusCode) + UnwindSafe + Clone,
|
|
|
|
) {
|
2023-06-22 14:42:52 +02:00
|
|
|
let post = self.search_post(query.clone()).await;
|
|
|
|
|
2023-01-12 13:55:53 +01:00
|
|
|
let query = yaup::to_string(&query).unwrap();
|
2023-06-22 14:42:52 +02:00
|
|
|
let get = self.search_get(&query).await;
|
|
|
|
|
|
|
|
insta::allow_duplicates! {
|
|
|
|
let (response, code) = post;
|
|
|
|
let t = test.clone();
|
|
|
|
if let Err(e) = catch_unwind(move || t(response, code)) {
|
|
|
|
eprintln!("Error with post search");
|
|
|
|
resume_unwind(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (response, code) = get;
|
|
|
|
if let Err(e) = catch_unwind(move || test(response, code)) {
|
|
|
|
eprintln!("Error with get search");
|
|
|
|
resume_unwind(e);
|
|
|
|
}
|
2021-07-06 11:54:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn search_post(&self, query: Value) -> (Value, StatusCode) {
|
2022-10-09 19:43:51 +02:00
|
|
|
let url = format!("/indexes/{}/search", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post_encoded(url, query, self.encoder).await
|
2021-07-06 11:54:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:55:53 +01:00
|
|
|
pub async fn search_get(&self, query: &str) -> (Value, StatusCode) {
|
2024-06-17 16:21:51 +02:00
|
|
|
let url = format!("/indexes/{}/search{}", urlencode(self.uid.as_ref()), query);
|
2021-07-06 11:54:09 +02:00
|
|
|
self.service.get(url).await
|
2024-05-27 14:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs both GET and POST similar queries
|
|
|
|
pub async fn similar(
|
|
|
|
&self,
|
|
|
|
query: Value,
|
|
|
|
test: impl Fn(Value, StatusCode) + UnwindSafe + Clone,
|
|
|
|
) {
|
|
|
|
let post = self.similar_post(query.clone()).await;
|
|
|
|
|
|
|
|
let query = yaup::to_string(&query).unwrap();
|
|
|
|
let get = self.similar_get(&query).await;
|
|
|
|
|
|
|
|
insta::allow_duplicates! {
|
|
|
|
let (response, code) = post;
|
|
|
|
let t = test.clone();
|
|
|
|
if let Err(e) = catch_unwind(move || t(response, code)) {
|
|
|
|
eprintln!("Error with post search");
|
|
|
|
resume_unwind(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (response, code) = get;
|
|
|
|
if let Err(e) = catch_unwind(move || test(response, code)) {
|
|
|
|
eprintln!("Error with get search");
|
|
|
|
resume_unwind(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn similar_post(&self, query: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/similar", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post_encoded(url, query, self.encoder).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn similar_get(&self, query: &str) -> (Value, StatusCode) {
|
2024-06-17 16:21:51 +02:00
|
|
|
let url = format!("/indexes/{}/similar{}", urlencode(self.uid.as_ref()), query);
|
2024-05-27 14:29:13 +02:00
|
|
|
self.service.get(url).await
|
2021-07-06 11:54:09 +02:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:31:52 +02:00
|
|
|
pub async fn facet_search(&self, query: Value) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/indexes/{}/facet-search", urlencode(self.uid.as_ref()));
|
|
|
|
self.service.post_encoded(url, query, self.encoder).await
|
|
|
|
}
|
|
|
|
|
2022-06-02 12:16:46 +02:00
|
|
|
pub async fn get_distinct_attribute(&self) -> (Value, StatusCode) {
|
2022-10-20 18:00:07 +02:00
|
|
|
let url =
|
|
|
|
format!("/indexes/{}/settings/{}", urlencode(self.uid.as_ref()), "distinct-attribute");
|
2022-06-02 12:16:46 +02:00
|
|
|
self.service.get(url).await
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
2021-02-19 19:55:00 +01:00
|
|
|
|
2024-06-05 23:40:29 +02:00
|
|
|
#[derive(Debug, Default, serde::Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-02-22 14:23:17 +01:00
|
|
|
pub struct GetAllDocumentsOptions {
|
2024-07-02 15:18:30 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-02-22 14:23:17 +01:00
|
|
|
pub limit: Option<usize>,
|
2024-07-02 15:18:30 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2021-02-22 14:23:17 +01:00
|
|
|
pub offset: Option<usize>,
|
2024-07-02 15:18:30 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2024-06-06 16:31:07 +02:00
|
|
|
pub fields: Option<Vec<&'static str>>,
|
2024-07-02 15:18:30 +02:00
|
|
|
pub retrieve_vectors: bool,
|
2021-02-22 14:23:17 +01:00
|
|
|
}
|