Skip to main content

OsClient

Struct OsClient 

Source
pub struct OsClient { /* private fields */ }

Implementations§

Source§

impl OsClient

Source

pub fn new(configuration: Arc<Configuration>) -> Self

Source

pub fn from_environment() -> Result<OsClient, Error>

Source

pub fn indices(&self) -> &IndicesApiClient

Source

pub fn cluster(&self) -> &ClusterApiClient

Source

pub fn ingest(&self) -> &IngestApiClient

Source

pub fn ml(&self) -> &MlApiClient

Source

pub fn api_version(&self) -> &'static str

Get the version of this API.

This string is pulled directly from the source OpenAPI document and may be in any format the API selects.

Source

pub async fn bulk_index_document<T: Serialize>( &self, index: &str, id: Option<String>, body: &T, ) -> Result<(), Error>

Sends a bulk index request to OpenSearch with the specified index, id and document body.

§Arguments
  • index - A string slice that holds the name of the index.
  • id - An optional string slice that holds the id of the document.
  • body - A reference to a serializable document body.
§Returns

Returns a Result containing a serde_json::Value representing the response from OpenSearch or an Error if the request fails.

§Example
use opensearch_client::OpenSearchClient;

#[derive(Serialize)]
struct MyDocument {
  title: String,
  content: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = OpenSearchClient::new("http://localhost:9200");
  let document = MyDocument {
    title: "My Title".to_string(),
    content: "My Content".to_string(),
  };
  let response = client
    .bulk_index_document("my_index", Some("my_id".to_string()), &document)
    .await?;
  Ok(())
}
Source

pub async fn bulk_action( &self, action: BulkAction, body: Option<&Value>, ) -> Result<(), Error>

Sends a bulk action to the OpenSearch server.

§Arguments
  • command - A string slice that holds the command to be executed.
  • action - A BulkAction enum that specifies the action to be taken.
  • body - An optional serde_json::Value that holds the request body.
§Returns

A Result containing a serde_json::Value object representing the response from the server, or an Error if the request failed.

§Examples
use opensearch_client::{BulkAction, OpenSearchClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = OpenSearchClient::new("http://localhost:9200")?;
  let action = BulkAction::Index {
    index: "my_index".to_string(),
    id: Some("1".to_string()),
  };
  let response = client.bulk_action("index", action, None).await?;
  Ok(())
}
Source

pub async fn bulk_create_document<T: Serialize>( &self, index: &str, id: &str, body: &T, ) -> Result<(), Error>

Sends a bulk create request to the OpenSearch cluster with the specified index, id and body.

§Arguments
  • index - A string slice that holds the name of the index.
  • id - A string slice that holds the id of the document.
  • body - A generic type T that holds the body of the document to be created.
§Returns

Returns a Result containing a serde_json::Value on success, or an Error on failure.

Source

pub async fn bulk_update_document( &self, index: &str, id: &str, body: &UpdateActionBody, ) -> Result<(), Error>

Asynchronously updates a document in bulk.

§Arguments
  • index - A string slice that holds the name of the index.
  • id - A string slice that holds the ID of the document to update.
  • body - An UpdateAction struct that holds the update action to perform.
§Returns

Returns a Result containing a serde_json::Value on success, or an Error on failure.

Source

pub async fn flush_bulk(&self) -> Result<BulkResponse, Error>

Sends a bulk request to the OpenSearch server and returns a BulkResponse. If the bulker size is 0, it returns an empty BulkResponse. If the bulk request contains errors, it logs the errors and returns the BulkResponse.

§Examples
use opensearch_client::OpenSearchClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = OpenSearchClient::new("http://localhost:9200", "user", "password");
  let response = client.flush_bulk().await?;
  println!("{:?}", response);
  Ok(())
}
Source

pub async fn index_document<T: Serialize>( &self, index: &str, body: &T, id: Option<String>, ) -> Result<IndexResponse, Error>

Indexes a document in the specified index with the given body and optional ID.

§Arguments
  • index - A string slice that holds the name of the index to which the document will be added.
  • body - A reference to a serializable object that represents the document to be added.
  • id - An optional string slice that holds the ID of the document to be added. If not provided, a new ID will be generated.
§Returns

A Result containing an IndexResponse object if the operation was successful, or an Error if an error occurred.

§Examples
use opensearch_client::Client;

#[derive(Serialize)]
struct MyDocument {
  title: String,
  content: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = Client::new("http://localhost:9200")?;

  let document = MyDocument {
    title: "My Title".to_string(),
    content: "My Content".to_string(),
  };

  let response = client.index_document("my_index", &document, None).await?;

  println!("Document ID: {}", response._id);

  Ok(())
}
Source

pub async fn create_document<T: Serialize>( &self, index: &str, id: &str, body: &T, ) -> Result<IndexResponse, Error>

Creates a new document in the specified index with the given ID and body.

§Arguments
  • index - A string slice that holds the name of the index.
  • id - A string slice that holds the ID of the document.
  • body - A generic type T that holds the body of the document. The type T must implement the Serialize trait from the serde crate.
§Returns

Returns a Result containing an IndexResponse on success, or an Error on failure.

§Examples
use opensearch_client::OpenSearchClient;

#[derive(Serialize)]
struct MyDocument {
  title: String,
  content: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = OpenSearchClient::new("http://localhost:9200")?;

  let document = MyDocument {
    title: "My Title".to_string(),
    content: "My Content".to_string(),
  };

  let response = client.create_document("my_index", "1", &document).await?;

  Ok(())
}
Source

pub async fn get_typed<T: DeserializeOwned>( &self, index: &str, id: &str, ) -> Result<GetTypedResult<T>, Error>

Asynchronously retrieves a typed document from the specified index and ID.

§Arguments
  • index - A string slice that holds the name of the index to retrieve the document from.
  • id - A string slice that holds the ID of the document to retrieve.
§Returns

A Result containing the deserialized content of the retrieved document, or an Error if the operation failed.

§Generic Type Parameters
  • T - The type of the document to retrieve. Must implement the DeserializeOwned and std::default::Default traits.
Source

pub async fn update_document( &self, index: &str, id: &str, action: &UpdateActionBody, ) -> Result<IndexResponse, Error>

Updates a document in the specified index with the given ID using the provided update action.

§Arguments
  • index - A string slice that holds the name of the index to update the document in.
  • id - A string slice that holds the ID of the document to update.
  • action - A reference to an UpdateAction enum that specifies the update action to perform.
§Returns

Returns a Result containing an IndexResponse struct if the update was successful, or an Error if an error occurred.

§Example
use opensearch_client::{Client, UpdateAction};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new("http://localhost:9200")?;

    let index = "my_index";
    let id = "1";
    let action = UpdateAction::new().doc(json!({"foo": "bar"}));

    let response = client.update_document(index, id, &action).await?;

    Ok(())
}
Source

pub fn get_bulker( &self, bulk_size: u32, max_concurrent_connections: u32, ) -> (JoinHandle<()>, Bulker)

Source

pub fn bulker(&self) -> BulkerBuilder

Source

pub async fn search_typed<T: DeserializeOwned>( &self, index: &str, search: Search, ) -> Result<TypedSearchResult<T>, Error>

Source

pub async fn search_stream<T: DeserializeOwned + Default>( &self, index: &str, query: &Query, sort: &SortCollection, size: u64, ) -> Result<impl Stream<Item = TypedHit<T>> + 'static, Error>

Searches for documents in the specified index and returns a stream of hits.

§Arguments
  • index - The name of the index to search in.
  • query - The query to execute.
  • sort - The sort criteria to use.
  • size - The maximum number of hits to return.
§Returns

A stream of hits that match the specified search criteria.

§Examples
use opensearch_client::{Client, Query, SortCollection};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let client = Client::new("http://localhost:9200")?;
  let query = Query::match_all();
  let sort = SortCollection::new().add_field("_doc", "asc");
  let stream = client.search_stream("my_index", &query, &sort, 10).await?;
  stream
    .for_each(|hit| {
      println!("{:?}", hit);
      futures::future::ready(())
    })
    .await;
  Ok(())
}
Source

pub fn delete_script<'f1>(&'f1 self) -> OsClientDeleteScriptBuilder<'f1>

Deletes a script.

Source

pub fn termvectors<'f1>(&'f1 self) -> OsClientTermvectorsBuilder<'f1>

Returns information and statistics about terms in the fields of a particular document.

Source

pub fn info<'f1>(&'f1 self) -> OsClientInfoBuilder<'f1>

Returns basic information about the cluster.

Source

pub fn search_shards<'f1>(&'f1 self) -> OsClientSearchShardsBuilder<'f1>

Returns information about the indexes and shards that a search request would be executed against.

Source

pub fn put_script<'f1>(&'f1 self) -> OsClientPutScriptBuilder<'f1>

Creates or updates a script.

Source

pub fn rank_eval<'f1>(&'f1 self) -> OsClientRankEvalBuilder<'f1>

Allows to evaluate the quality of ranked search results over a set of typical search queries.

Source

pub fn explain<'f1>(&'f1 self) -> OsClientExplainBuilder<'f1>

Returns information about why a specific matches (or doesn’t match) a query.

Source

pub fn get_script<'f1>(&'f1 self) -> OsClientGetScriptBuilder<'f1>

Returns a script.

Source

pub fn render_search_template<'f1>( &'f1 self, ) -> OsClientRenderSearchTemplateBuilder<'f1>

Allows to use the Mustache language to pre-render a search definition.

Source

pub fn reindex<'f1>(&'f1 self) -> OsClientReindexBuilder<'f1>

Allows to copy documents from one index to another, optionally filtering the source documents by a query, changing the destination index settings, or fetching the documents from a remote cluster.

Source

pub fn exists_source<'f1>(&'f1 self) -> OsClientExistsSourceBuilder<'f1>

Returns information about whether a document source exists in an index.

Source

pub fn delete_by_query<'f1>(&'f1 self) -> OsClientDeleteByQueryBuilder<'f1>

Deletes documents matching the provided query.

Source

pub fn exists<'f1>(&'f1 self) -> OsClientExistsBuilder<'f1>

Returns information about whether a document exists in an index.

Source

pub fn field_caps<'f1>(&'f1 self) -> OsClientFieldCapsBuilder<'f1>

Returns the information about the capabilities of fields among multiple indexes.

Source

pub fn get<'f1>(&'f1 self) -> OsClientGetBuilder<'f1>

Returns a document.

Source

pub fn delete<'f1>(&'f1 self) -> OsClientDeleteBuilder<'f1>

Removes a document from the index.

Source

pub fn mtermvectors<'f1>(&'f1 self) -> OsClientMtermvectorsBuilder<'f1>

Returns multiple termvectors in one request.

Source

pub fn search_with_index<'f1>(&'f1 self) -> OsClientSearchWithIndexBuilder<'f1>

Returns results matching a query.

Source

pub fn mget<'f1>(&'f1 self) -> OsClientMgetBuilder<'f1>

Allows to get multiple documents in one request.

Source

pub fn get_script_languages<'f1>( &'f1 self, ) -> OsClientGetScriptLanguagesBuilder<'f1>

Returns available script types, languages and contexts.

Source

pub fn clear_scroll<'f1>(&'f1 self) -> OsClientClearScrollBuilder<'f1>

Explicitly clears the search context for a scroll.

Source

pub fn search_template<'f1>(&'f1 self) -> OsClientSearchTemplateBuilder<'f1>

Allows to use the Mustache language to pre-render a search definition.

Source

pub fn count<'f1>(&'f1 self) -> OsClientCountBuilder<'f1>

Returns number of documents matching a query.

Source

pub fn search_shards_with_index<'f1>( &'f1 self, ) -> OsClientSearchShardsWithIndexBuilder<'f1>

Returns information about the indexes and shards that a search request would be executed against.

Source

pub fn reindex_rethrottle<'f1>( &'f1 self, ) -> OsClientReindexRethrottleBuilder<'f1>

Changes the number of requests per second for a particular reindex operation.

Source

pub fn create<'f1>(&'f1 self) -> OsClientCreateBuilder<'f1>

Creates a new document in the index.

Returns a 409 response when a document with a same ID already exists in the index.

Source

pub fn search<'f1>(&'f1 self) -> OsClientSearchBuilder<'f1>

Returns results matching a query.

Source

pub fn get_source<'f1>(&'f1 self) -> OsClientGetSourceBuilder<'f1>

Returns the source of a document.

Source

pub fn bulk<'f1>(&'f1 self) -> OsClientBulkBuilder<'f1>

Allows to perform multiple index/update/delete operations in a single request.

Source

pub fn scroll<'f1>(&'f1 self) -> OsClientScrollBuilder<'f1>

Allows to retrieve a large numbers of results from a single search request.

Source

pub fn msearch<'f1>(&'f1 self) -> OsClientMsearchBuilder<'f1>

Allows to execute several search operations in one request.

Source

pub fn scripts_painless_execute<'f1>( &'f1 self, ) -> OsClientScriptsPainlessExecuteBuilder<'f1>

Allows an arbitrary script to be executed and a result to be returned.

Source

pub fn update_by_query<'f1>(&'f1 self) -> OsClientUpdateByQueryBuilder<'f1>

Performs an update on every document in the index without changing the source, for example to pick up a mapping change.

Source

pub fn update_by_query_raw<'f1>( &'f1 self, ) -> OsClientUpdateByQueryRawBuilder<'f1>

Performs an update on every document in the index without changing the source, for example to pick up a mapping change.

Source

pub fn update_by_query_rethrottle<'f1>( &'f1 self, ) -> OsClientUpdateByQueryRethrottleBuilder<'f1>

Changes the number of requests per second for a particular Update By Query operation.

Source

pub fn get_script_context<'f1>( &'f1 self, ) -> OsClientGetScriptContextBuilder<'f1>

Returns all script contexts.

Source

pub fn delete_by_query_rethrottle<'f1>( &'f1 self, ) -> OsClientDeleteByQueryRethrottleBuilder<'f1>

Changes the number of requests per second for a particular Delete By Query operation.

Source

pub fn search_template_with_index<'f1>( &'f1 self, ) -> OsClientSearchTemplateWithIndexBuilder<'f1>

Allows to use the Mustache language to pre-render a search definition.

Source

pub fn msearch_template<'f1>(&'f1 self) -> OsClientMsearchTemplateBuilder<'f1>

Allows to execute several search template operations in one request.

Source

pub fn ping<'f1>(&'f1 self) -> OsClientPingBuilder<'f1>

Returns whether the cluster is running.

Source

pub fn index<'f1>(&'f1 self) -> OsClientIndexBuilder<'f1>

Creates or updates a document in an index.

Source

pub fn update<'f1>(&'f1 self) -> OsClientUpdateBuilder<'f1>

Updates a document with a script or partial document.

Source

pub fn update_raw<'f1>(&'f1 self) -> OsClientUpdateRawBuilder<'f1>

Updates a document with a script or partial document.

Trait Implementations§

Source§

impl Clone for OsClient

Source§

fn clone(&self) -> OsClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more