Home > Blockchain >  Msearch Elasticsearch API - Rust
Msearch Elasticsearch API - Rust

Time:07-12

By this point, I feel like I am the only other person on earth that is using multi-search on Rust... other than the person who wrote it.

There is zero documentation on this other than this hyper-confusing one https://docs.rs/elasticsearch/7.14.0-alpha.1/elasticsearch/struct.Msearch.html

I figured I had to pass MsearchParts parts as an argument for the client.msearch(here goes msearch_parts), and luckily for me, there a piece of documentation for how that is supposed to be, but such documentation is so poorly done that I have no clue of what to do because I did not write the API.

I have no clue of how to pass my JSON

{"index":"cat_food"}
{"query":{"term":{"name":{"term":"Whiskers"}}}}
{"index":"cat_food"}
{"query":{"term":{"name":{"term":"Chicken"}}}}
{"index":"cat_food"}
{"query":{"term":{"name":{"term":"Turkey"}}}}
"NOT IN THE CODE: extra EMPTY line required by elasticsearch multi-searches"

and get a 200^ response.

As a side note, my JSON is well formatted into a string that can be sent in a normal reqwest the issue is more on how to turn that JSON string into MsearchParts.

CodePudding user response:

Per the doc of MsearchParts, it looks an array of &str needs to be used to construct the Index (or IndexType) variant of enum MsearchParts. So, please give the following way a try and see if it works.

Let parts = MsearchParts::Index([
    r#"{"index":"cat_food"}"#, 
    r#"{"query":{"term":{"name":{"term":"Whiskers"}}}}"#, 
    r#"{"index":"cat_food"}"#, 
    r#"{"query":{"term":{"name":{"term":"Chicken"}}}}"#, 
    r#"{"index":"cat_food"}"#, 
    r#"{"query":{"term":{"name":{"term":"Turkey"}}}}"#
]);

CodePudding user response:

After hours of investigating I took some other approach, using a body vector and the msearch API. I think the json doesnt go to the msearchparts but to a vector of bodies. (see https://docs.rs/elasticsearch/7.14.0-alpha.1/elasticsearch/#request-bodies)

It runs, but the response gives me an error 400. And I dont know why. I assume its the missing empty (json) bodies, as required in the elastic console.

What do you think?

let mut body: Vec<JsonBody<_>> = Vec::with_capacity(4);
body.push(json!(
    {"query": {
        "match": {"title":"bee"}
    }}
).into());

body.push(json!(
    {"query": {
        "multi_match": {
            "query": "tree",
            "fields": ["title", "info"]
            }
    },"from":0, "size":2}
).into());

let search_response = client
    .msearch(MsearchParts::Index(&["nature_index"]))
    .body(body)
    .send()
    .await?;
  • Related