我一直在嘗試進行多重搜索(我可以一次發送多個 json 物件),但到目前為止都失敗了。
此請求將發送到 Elasticsearch 服務器,但即使您不知道那是什么,您仍然可以提供幫助。
我想要一個看起來像這樣的請求正文:
{"index": "superheroes"}
{"query": {"term": {"name": {"term": "hulk"}}}}
{"index": "superheroes"}
{"query": {"term": {"name": {"term": "spiderman"}}}}
{"index": "superheroes"}
{"query": {"term": {"name": {"term": "ant man"}}}}
請注意,JSON 物件沒有用陣列包裝。
如果我通過 Postman 等客戶端發送該確切塊,我會從服務器獲得所需的輸出。
這是我在 Rust 中發送該請求的操作:
let mut request_pieces: Vec<serde_json::Value> = Vec::new();
for name in names {
request_pieces.push(json!({"index": "superheroes"}));
request_pieces.push(json!({"query": {"term": {"name": {"term": name}}}}));
}
let search_body: Value = json!(request_pieces);
println!("{:?}", search_body);
當我發送來自服務器的錯誤時,這是??有道理的,因為它在一個陣列中并且服務器并不期望這樣,我發送到服務器的內容如下所示:
Array([
Object({
"index": String(
"superheroes",
),
}),
Object({
"query": Object({
"term": Object({
"name": Object({
"term": String(
"hulk",
),
}),
}),
}),
}),
Object({
"index": String(
"superheroes",
),
}),
Object({
"query": Object({
"term": Object({
"name": Object({
"term": String(
"spiderman",
),
}),
}),
}),
}),
Object({
"index": String(
"superheroes",
),
}),
Object({
"query": Object({
"term": Object({
"name": Object({
"term": String(
"ant man",
),
}),
}),
}),
}),
])
我怎樣才能發送像最頂部的請求?JSON物件在哪里分開。
順便說一句,在其他編程語言中實作這一點的一種方法是將其全部設為字串,但在 Rust 中,這樣做非常具有挑戰性。
uj5u.com熱心網友回復:
當您有一個 json 請求向量時,您可以直接獲取所需的內容,而不是將其編碼為 json 字串:
let mut request_pieces = Vec::new();
for name in names {
request_pieces.push(json!({"index": "superheroes"}).to_string());
request_pieces.push(json!({"query": {"term": {"word": {"term": name}}}}).to_string());
}
let search_body = request_pieces.join("\n");
println!("{}", search_body);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497381.html
