Official Rust client for OpenSearch
OpenSearch
is an official Rust client for OpenSearch, providing an efficient asynchronous
client for all stable OpenSearch APIs that's easy to use.
Versions and Compatibility
Rust client | OpenSearch |
---|---|
1.x | 1.x |
A major version of the client is compatible with the same major version of OpenSearch. Since OpenSearch is developed following Semantic Versioning principles, Any minor/patch version of the client can be used against any minor/patch version of OpenSearch within the same major version lineage. For example,
- A
1.5.0
client can be used against1.0.0
OpenSearch - A
1.4.0
client can be used against1.5.1
OpenSearch
In the former case, a 1.5.0 client may contain additional API functions that are not available in 1.0.0 OpenSearch. In this case, these APIs cannot be used, but for any APIs available in OpenSearch, the respective API functions on the client will be compatible.
In the latter case, a 1.4.0 client won't contain API functions for APIs that are introduced in OpenSearch 1.5.0+, but for all other APIs available in OpenSearch, the respective API functions on the client will be compatible.
No compatibility assurances are given between different major versions of the client and OpenSearch. Major differences likely exist between major versions of OpenSearch, particularly around request and response object formats, but also around API urls and behaviour.
Features
The following are a list of Cargo features that can be enabled or disabled:
- native-tls (enabled by default): Enables TLS functionality provided by
native-tls
. - rustls-tls: Enables TLS functionality provided by
rustls
. - beta-apis: Enables beta APIs. Beta APIs are on track to become stable and permanent features. Use them with caution because it is possible that breaking changes are made to these APIs in a minor version.
- experimental-apis: Enables experimental APIs. Experimental APIs are just that - an experiment. An experimental
API might have breaking changes in any future version, or it might even be removed entirely. This feature also
enables
beta-apis
.
Getting started
Add the opensearch
crate and version to Cargo.toml. Choose the version that is compatible with
the version of OpenSearch you're using
[]
= "1.0.0"
The following optional dependencies may also be useful to create requests and read responses
= "~1"
= "~1"
Async support with tokio
The client uses reqwest
to make HTTP calls, which internally uses
the tokio
runtime for async support. As such, you may require
to take a dependency on tokio
in order to use the client. For example, in Cargo.toml, you may
need the following dependency
= { = "*", = ["full"] }
and to attribute async main function with #[tokio::main]
async
and attribute test functions with #[tokio::test]
async
Create a client
To create a client to make API calls to OpenSearch running on http://localhost:9200
# use OpenSearch;
let client = default;
Alternatively, you can create a client to make API calls against OpenSearch running on a specific [url::Url]
# use ;
#
More control over how a Transport is built can be achieved using TransportBuilder to build a transport, and passing it to [OpenSearch::new] create a new instance of [OpenSearch]
# use ;
# use Url;
#
Making API calls
The client exposes all stable OpenSearch APIs, either on the root [OpenSearch] client,
or on a namespace client that groups related APIs, such as Cat, which groups the
Cat related APIs. All API functions are async
and can be await
ed.
The following makes an API call to the cat indices API
# use ;
# use Url;
# use ;
#
# async
For APIs that contain parts of the Url path to be provided by the consumer, the Url path
variants are modelled as an enum
, such as CatIndicesParts in the above example, which models
the variants of the CatIndices API.
Indexing
Indexing a single document can be achieved with the index API
# use ;
# use Url;
# use ;
#
# async
For indexing multiple documents, the bulk API is a better option, allowing multiple operations to be sent in one API call
# use ;
# use Url;
# use ;
#
# async
Searching
The following makes an API call to tweets/_search
with the json body
{"query":{"match":{"message":"OpenSearch"}}}
# use ;
# use Url;
# use ;
#
# async
Request bodies
For APIs that expect JSON, the body
associated function of the API constrains the input
to a type that implements [serde::Serialize] trait. An example of this was the indexing a single
document example above.
Some APIs expect newline delimited JSON
(NDJSON) however, so the body
associated for these APIs constrain the input to a vector of
types that implement Body trait. An example of this was the bulk indexing multiple documents
above.
The Body trait represents the body of an API call, allowing for different body implementations. As well as those to represent JSON and NDJSON, a few other types also have implementations for Body, such as byte slice. Whilst these can't be passed to the API functions directly, [OpenSearch::send] can be used
# use ;
# use Url;
# use ;
#
# async