Skip to content

Commit 44a69b6

Browse files
committed
Add documentation for msearch
This commit adds documentation for the msearch API. Based on feedback from StackOverflow: https://stackoverflow.com/a/73432234/1831
1 parent d52932e commit 44a69b6

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Examples
2+
3+
To make a multi-search request, specify the headers and bodies
4+
for the search requests in the body. The body accepts a
5+
`Vec<T>` where `T` implements the [Body] trait.
6+
7+
```rust,no_run
8+
# use elasticsearch::{Elasticsearch, Error, MsearchParts};
9+
# use serde_json::{json, Value};
10+
# async fn doc() -> Result<(), Box<dyn std::error::Error>> {
11+
let client = Elasticsearch::default();
12+
13+
fn print_hits(hits: &[Value]) {
14+
for hit in hits {
15+
println!(
16+
"id: '{}', source: '{}', score: '{}'",
17+
hit["_id"].as_str().unwrap(),
18+
hit["_source"],
19+
hit["_score"].as_f64().unwrap()
20+
);
21+
}
22+
}
23+
24+
let msearch_response = client
25+
.msearch(MsearchParts::None)
26+
.body::<JsonBody<Value>>(vec![
27+
json!({"index":"cat_food"}).into(),
28+
json!({"query":{"term":{"name":{"term":"Whiskers"}}}}).into(),
29+
json!({"index":"cat_food"}).into(),
30+
json!({"query":{"term":{"name":{"term":"Chicken"}}}}).into(),
31+
json!({"index":"cat_food"}).into(),
32+
json!({"query":{"term":{"name":{"term":"Turkey"}}}}).into(),
33+
])
34+
.send()
35+
.await?;
36+
37+
let json: Value = msearch_response.json().await?;
38+
39+
// iterate over the responses
40+
for response in json["responses"]
41+
.as_array()
42+
.into_iter()
43+
{
44+
print_hits(response["hits"]["hits"].as_array().unwrap());
45+
}
46+
47+
# Ok(())
48+
# }
49+
```

elasticsearch/src/root/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9365,7 +9365,7 @@ impl Elasticsearch {
93659365
pub fn mget<'a, 'b>(&'a self, parts: MgetParts<'b>) -> Mget<'a, 'b, ()> {
93669366
Mget::new(self.transport(), parts)
93679367
}
9368-
#[doc = "[Msearch API](https://www.elastic.co/guide/en/elasticsearch/reference/8.0/search-multi-search.html)\n\nAllows to execute several search operations in one request."]
9368+
#[doc = "[Msearch API](https://www.elastic.co/guide/en/elasticsearch/reference/8.0/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```"]
93699369
pub fn msearch<'a, 'b>(&'a self, parts: MsearchParts<'b>) -> Msearch<'a, 'b, ()> {
93709370
Msearch::new(self.transport(), parts)
93719371
}

0 commit comments

Comments
 (0)