Struct elastic::client::Client
[−]
[src]
pub struct Client { /* fields omitted */ }A HTTP client for the Elasticsearch REST API.
The Client is a structure that lets you create and send RequestBuilders.
It's mostly a thin wrapper over a reqwest::Client and is re-usable.
Examples
Create a Client for an Elasticsearch node at es_host:9200:
let params = RequestParams::new("http://es_host:9200").url_param("pretty", true); let client = Client::new(params).unwrap();
Methods
impl Client[src]
fn search<'a, TDocument>(
&'a self
) -> RequestBuilder<'a, SearchRequestBuilder<TDocument, DefaultBody>> where
TDocument: DeserializeOwned,
&'a self
) -> RequestBuilder<'a, SearchRequestBuilder<TDocument, DefaultBody>> where
TDocument: DeserializeOwned,
Create a RequestBuilder for a search request.
Examples
Run a simple Query String query for a DocumentType called MyType:
let response = client.search::<MyType>() .index("myindex") .body(json_str!({ query: { query_string: { query: "*" } } })) .send() .unwrap(); // 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::Values as documents when searching:
let response = client.search::<Value>() .index("myindex") .ty(Some("mytype")) .send() .unwrap();
impl Client[src]
fn get_document<'a, TDocument>(
&'a self,
index: Index<'static>,
id: Id<'static>
) -> RequestBuilder<'a, GetRequestBuilder<TDocument>> where
TDocument: DeserializeOwned + DocumentType,
&'a self,
index: Index<'static>,
id: Id<'static>
) -> RequestBuilder<'a, GetRequestBuilder<TDocument>> where
TDocument: DeserializeOwned + DocumentType,
Create a RequestBuilder for a get request.
Examples
Get a DocumentType called MyType with an id of 1:
let response = client.get_document::<MyType>(index("myindex"), id(1)) .send() .unwrap(); if let Some(doc) = response.source { 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.get_document::<Value>(index("myindex"), id(1)) .ty("mytype") .send() .unwrap();
impl Client[src]
fn index_document<'a, TDocument>(
&'a self,
index: Index<'static>,
id: Id<'static>,
doc: TDocument
) -> RequestBuilder<'a, IndexRequestBuilder<TDocument>> where
TDocument: Serialize + DocumentType,
&'a self,
index: Index<'static>,
id: Id<'static>,
doc: TDocument
) -> RequestBuilder<'a, IndexRequestBuilder<TDocument>> where
TDocument: Serialize + DocumentType,
Create a RequestBuilder for an index request.
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.index_document(index("myindex"), id(doc.id), doc) .send() .unwrap();
For more details on document types and mapping, see the types module.
impl Client[src]
fn put_mapping<'a, TDocument>(
&'a self,
index: Index<'static>
) -> RequestBuilder<'a, PutMappingRequestBuilder<TDocument>> where
TDocument: Serialize + DocumentType,
&'a self,
index: Index<'static>
) -> RequestBuilder<'a, PutMappingRequestBuilder<TDocument>> where
TDocument: Serialize + DocumentType,
Create a RequestBuilder for a put mapping request.
Examples
Put the document mapping for a DocumentType called MyType:
client.put_mapping::<MyType>(index("myindex")) .send() .unwrap();
For more details on document types and mapping, see the types module.
impl Client[src]
fn create_index<'a>(
&'a self,
index: Index<'static>
) -> RequestBuilder<'a, CreateIndexRequestBuilder<DefaultBody>>
&'a self,
index: Index<'static>
) -> RequestBuilder<'a, CreateIndexRequestBuilder<DefaultBody>>
Create a RequestBuilder for a create index request.
Examples
Create an index called myindex:
let my_index = index("myindex"); let response = client.create_index(my_index).send().unwrap(); 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(): IndexDocumentMapping::from(MyType::mapping()) } }); let response = client.create_index(my_index) .body(body.to_string()) .send() .unwrap(); assert!(response.acknowledged);
For more details on document types and mapping, see the types module.
impl Client[src]
fn request<'a, TRequest, TBody>(
&'a self,
req: TRequest
) -> RequestBuilder<'a, RawRequestBuilder<TRequest, TBody>> where
TRequest: Into<HttpRequest<'static, TBody>>,
TBody: IntoBody,
&'a self,
req: TRequest
) -> RequestBuilder<'a, RawRequestBuilder<TRequest, TBody>> where
TRequest: Into<HttpRequest<'static, TBody>>,
TBody: IntoBody,
Create a raw RequestBuilder 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().and_then(into_response::<PingResponse>).unwrap(); println!("cluster: {}", ping.name);
impl Client[src]
fn new(params: RequestParams) -> Result<Self>
Create a new client for the given parameters.
The parameters given here are used as the defaults for any
request made by this client, but can be overriden on a
per-request basis.
This method can return a HttpError if the underlying reqwest::Client
fails to create.
Examples
Create a Client with default parameters:
let client = ClientBuilder::new().build().unwrap();
Create a Client for a specific node:
let client = Client::new(RequestParams::new("http://eshost:9200")).unwrap();
See RequestParams for more configuration options.