Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice for converting search results to Rust structs? #79

Closed
Jasperav opened this issue Mar 29, 2020 · 1 comment
Closed

Best practice for converting search results to Rust structs? #79

Jasperav opened this issue Mar 29, 2020 · 1 comment

Comments

@Jasperav
Copy link

Jasperav commented Mar 29, 2020

When using the search api, the queried objects are in an array inside "hits" and another "hits". Furthermore, the actual objects are again nested inside a "_source" object. So to extract the data from a search, I have this code:

let value = response.read_body::<Value>().unwrap();

let mut hits = value["hits"]["hits"]
    .as_array()
    .unwrap()
    .clone();

if hits.is_empty() {
    return Ok(vec![])
}

Ok(hits
    .into_iter()
    .map(|e| e["_source"].clone())
    .map(|e| serde_json::from_value(e).unwrap())
    .collect::<BResult<_>>()?)

This looks like code that can be somewhere in this library (else everyone needs to write something like this). Is there a standard way of doing what I am doing?

@russcam
Copy link
Contributor

russcam commented Mar 31, 2020

The example you show is the way I would recommend for now.

What would be really useful here is a SearchResponse<T> struct that the response from the search API is deserialized into. This is something that we would like to provide eventually, but it is a sizeable undertaking, more so for more complex responses like search! I'm going to close this issue in order to discuss on #75 as it is related to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants