-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for msearch (#204)
This commit adds documentation for the msearch API. Based on feedback from StackOverflow: https://stackoverflow.com/a/73432234/1831
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Examples | ||
|
||
To make a multi-search request, specify the headers and bodies | ||
for the search requests in the body. The body accepts a | ||
`Vec<T>` where `T` implements the [Body] trait. | ||
|
||
```rust,no_run | ||
# use elasticsearch::{Elasticsearch, Error, MsearchParts}; | ||
# use serde_json::{json, Value}; | ||
# async fn doc() -> Result<(), Box<dyn std::error::Error>> { | ||
let client = Elasticsearch::default(); | ||
fn print_hits(hits: &[Value]) { | ||
for hit in hits { | ||
println!( | ||
"id: '{}', source: '{}', score: '{}'", | ||
hit["_id"].as_str().unwrap(), | ||
hit["_source"], | ||
hit["_score"].as_f64().unwrap() | ||
); | ||
} | ||
} | ||
let msearch_response = client | ||
.msearch(MsearchParts::None) | ||
.body::<JsonBody<Value>>(vec![ | ||
json!({"index":"cat_food"}).into(), | ||
json!({"query":{"term":{"name":{"term":"Whiskers"}}}}).into(), | ||
json!({"index":"cat_food"}).into(), | ||
json!({"query":{"term":{"name":{"term":"Chicken"}}}}).into(), | ||
json!({"index":"cat_food"}).into(), | ||
json!({"query":{"term":{"name":{"term":"Turkey"}}}}).into(), | ||
]) | ||
.send() | ||
.await?; | ||
let json: Value = msearch_response.json().await?; | ||
// iterate over the responses | ||
for response in json["responses"] | ||
.as_array() | ||
.into_iter() | ||
{ | ||
print_hits(response["hits"]["hits"].as_array().unwrap()); | ||
} | ||
# Ok(()) | ||
# } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters