Skip to content

Commit

Permalink
Add documentation for msearch (#204)
Browse files Browse the repository at this point in the history
This commit adds documentation for the msearch API.
Based on feedback from StackOverflow:

https://stackoverflow.com/a/73432234/1831
  • Loading branch information
russcam authored Aug 21, 2024
1 parent bcc4c0f commit 144f586
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
49 changes: 49 additions & 0 deletions api_generator/docs/functions/Elasticsearch.msearch.md
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(())
# }
```
2 changes: 1 addition & 1 deletion elasticsearch/src/root/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9617,7 +9617,7 @@ impl Elasticsearch {
pub fn mget<'a, 'b>(&'a self, parts: MgetParts<'b>) -> Mget<'a, 'b, ()> {
Mget::new(self.transport(), parts)
}
#[doc = "[Msearch API](https://www.elastic.co/guide/en/elasticsearch/reference/8.15/search-multi-search.html)\n\nAllows to execute several search operations in one request."]
#[doc = "[Msearch API](https://www.elastic.co/guide/en/elasticsearch/reference/8.15/search-multi-search.html)\n\nAllows to execute several search operations in one request.\n\n# Examples\n\nTo make a multi-search request, specify the headers and bodies\nfor the search requests in the body. The body accepts a\n`Vec<T>` where `T` implements the [Body] trait.\n\n```rust,no_run\n# use elasticsearch::{Elasticsearch, Error, MsearchParts};\n# use serde_json::{json, Value};\n# async fn doc() -> Result<(), Box<dyn std::error::Error>> {\nlet client = Elasticsearch::default();\n\nfn print_hits(hits: &[Value]) {\n for hit in hits {\n println!(\n \"id: '{}', source: '{}', score: '{}'\",\n hit[\"_id\"].as_str().unwrap(),\n hit[\"_source\"],\n hit[\"_score\"].as_f64().unwrap()\n );\n }\n}\n\nlet msearch_response = client\n .msearch(MsearchParts::None)\n .body::<JsonBody<Value>>(vec![\n json!({\"index\":\"cat_food\"}).into(),\n json!({\"query\":{\"term\":{\"name\":{\"term\":\"Whiskers\"}}}}).into(),\n json!({\"index\":\"cat_food\"}).into(),\n json!({\"query\":{\"term\":{\"name\":{\"term\":\"Chicken\"}}}}).into(),\n json!({\"index\":\"cat_food\"}).into(),\n json!({\"query\":{\"term\":{\"name\":{\"term\":\"Turkey\"}}}}).into(),\n ])\n .send()\n .await?;\n\nlet json: Value = msearch_response.json().await?;\n\n// iterate over the responses\nfor response in json[\"responses\"]\n .as_array()\n .into_iter()\n{\n print_hits(response[\"hits\"][\"hits\"].as_array().unwrap());\n}\n \n# Ok(())\n# }\n```"]
pub fn msearch<'a, 'b>(&'a self, parts: MsearchParts<'b>) -> Msearch<'a, 'b, ()> {
Msearch::new(self.transport(), parts)
}
Expand Down

0 comments on commit 144f586

Please sign in to comment.