Struct elastic::client::Client
[−]
[src]
pub struct Client<TSender> { /* fields omitted */ }
A HTTP client for the Elasticsearch REST API.
The Client
is a structure that lets you create and send request builders.
Client
is generic over a Sender
, but rather than use Client
directly, use one of:
Examples
Create a synchronous Client
and send a ping request:
let client = SyncClientBuilder::new().build()?; let response = client.request(PingRequest::new()) .send()? .into_response::<PingResponse>()?;
Create an asynchronous Client
and send a ping request:
let mut core = Core::new()?; let client = AsyncClientBuilder::new().build(&core.handle())?; let response_future = client.request(PingRequest::new()) .send() .and_then(|res| res.into_response::<PingResponse>()); core.run(response_future)?;
Methods
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn request<TRequest, TBody>(
&self,
req: TRequest
) -> RawRequestBuilder<TSender, TRequest, TBody> where
TRequest: Into<HttpRequest<'static, TBody>>,
TBody: Into<TSender::Body>,
[src]
&self,
req: TRequest
) -> RawRequestBuilder<TSender, TRequest, TBody> where
TRequest: Into<HttpRequest<'static, TBody>>,
TBody: Into<TSender::Body>,
Create a RawRequestBuilder
with this Client
that can be configured before sending.
The request
method accepts any type that can be converted into a HttpRequest<'static>
,
which includes the endpoint types in the endpoints
module.
Examples
Send a cluster ping and read the returned metadata:
// `PingRequest` implements `Into<HttpRequest>` let req = PingRequest::new(); // Turn the `PingRequest` into a `RequestBuilder` let builder = client.request(req); // Send the `RequestBuilder` and parse as a `PingResponse` let ping = builder.send()?.into_response::<PingResponse>()?; println!("cluster: {}", ping.name());
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn search<TDocument>(
&self
) -> SearchRequestBuilder<TSender, TDocument, DefaultBody> where
TDocument: DeserializeOwned,
[src]
&self
) -> SearchRequestBuilder<TSender, TDocument, DefaultBody> where
TDocument: DeserializeOwned,
Create a SearchRequestBuilder
with this Client
that can be configured before sending.
For more details, see:
Examples
Run a simple Query String query for a DocumentType
called MyType
:
let query = "a query string"; let response = client.search::<MyType>() .index("myindex") .body(json!({ "query": { "query_string": { "query": query } } })) .send()?; // Iterate through the hits (of type `MyType`) for hit in response.hits() { println!("{:?}", hit); }
For more details on document types and mapping, see the types
module.
It's also possible to use serde_json::Value
s as documents when searching:
let response = client.search::<Value>() .index("myindex") .ty(Some("mytype")) .send()?;
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn document_get<TDocument>(
&self,
index: Index<'static>,
id: Id<'static>
) -> GetRequestBuilder<TSender, TDocument> where
TDocument: DeserializeOwned + DocumentType,
[src]
&self,
index: Index<'static>,
id: Id<'static>
) -> GetRequestBuilder<TSender, TDocument> where
TDocument: DeserializeOwned + DocumentType,
Create a GetRequestBuilder
with this Client
that can be configured before sending.
For more details, see:
Examples
Get a DocumentType
called MyType
with an id of 1
:
let response = client.document_get::<MyType>(index("myindex"), id(1)) .send()?; if let Some(doc) = response.into_document() { println!("id: {}", doc.id); }
For more details on document types, see the types
module.
Get the same document as a serde_json::Value
:
let response = client.document_get::<Value>(index("myindex"), id(1)) .ty("mytype") .send()?;
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn document_index<TDocument>(
&self,
index: Index<'static>,
id: Id<'static>,
doc: TDocument
) -> IndexRequestBuilder<TSender, TDocument> where
TDocument: Serialize + DocumentType,
[src]
&self,
index: Index<'static>,
id: Id<'static>,
doc: TDocument
) -> IndexRequestBuilder<TSender, TDocument> where
TDocument: Serialize + DocumentType,
Create a IndexRequestBuilder
with this Client
that can be configured before sending.
For more details, see:
Examples
Index a DocumentType
called MyType
with an id of 1
:
let doc = MyType { id: 1, title: String::from("A title"), timestamp: Date::now() }; let response = client.document_index(index("myindex"), id(doc.id), doc) .send()?; assert!(response.created());
For more details on document types and mapping, see the types
module.
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn document_put_mapping<TDocument>(
&self,
index: Index<'static>
) -> PutMappingRequestBuilder<TSender, TDocument> where
TDocument: Serialize + DocumentType,
[src]
&self,
index: Index<'static>
) -> PutMappingRequestBuilder<TSender, TDocument> where
TDocument: Serialize + DocumentType,
Create a PutMappingRequestBuilder
with this Client
that can be configured before sending.
For more details, see:
Examples
Put the document mapping for a DocumentType
called MyType
:
let response = client.document_put_mapping::<MyType>(index("myindex")) .send()?; assert!(response.acknowledged());
For more details on document types and mapping, see the types
module.
impl<TSender> Client<TSender> where
TSender: Sender,
[src]
TSender: Sender,
fn index_create(
&self,
index: Index<'static>
) -> IndexCreateRequestBuilder<TSender, DefaultBody>
[src]
&self,
index: Index<'static>
) -> IndexCreateRequestBuilder<TSender, DefaultBody>
Create a IndexCreateRequestBuilder
with this Client
that can be configured before sending.
For more details, see:
Examples
Create an index called myindex
:
let my_index = index("myindex"); let response = client.index_create(my_index).send()?; assert!(response.acknowledged());
Create an index with settings and document mappings for a DocumentType
called MyType
:
let my_index = index("myindex"); let body = json!({ "settings": { "index": { "number_of_shards": 3, "number_of_replicas": 2 } }, "mappings": { MyType::name(): MyType::index_mapping() } }); let response = client.index_create(my_index) .body(body.to_string()) .send()?; assert!(response.acknowledged());
For more details on document types and mapping, see the types
module.