mokuroku icon indicating copy to clipboard operation
mokuroku copied to clipboard

How to get original document whilst iterating through query results?

Open SaadAttieh opened this issue 4 years ago • 1 comments

I couldn't see an example of how to do this so I guessed it would just be calling dbase.get on the doc_id in the query result.

//adapted from tags example
    let results = dbase.query_by_key("tags", b"cat").unwrap();
    for result in results {
        let doc_id = str::from_utf8(&result.doc_id).unwrap().to_owned();
        println!("query result key: {:}", doc_id);
        println!("tag value: {}", str::from_utf8(&result.value).unwrap().to_owned());
        println!("get actual value  {:?}", dbase.get::<Asset,_>(result.doc_id).ok().unwrap());
    }

But I get the mutable borrow error:

error[E0502]: cannot borrow `dbase` as immutable because it is also borrowed as mutable
   --> src/main.rs:123:44
    |
118 |     let results = dbase.query_by_key("tags", b"cat").unwrap();
    |                   ----- mutable borrow occurs here
119 |     for result in results {
    |                   ------- mutable borrow later used here
...
123 |         println!("get actual value  {:?}", dbase.get::<Asset,_>(result.doc_id).ok().unwrap());
    |                                            ^^^^^ immutable borrow occurs here

SaadAttieh avatar Nov 29 '20 18:11 SaadAttieh

You are quite right that this use case was not considered when designing the API. For my purposes, I only needed the index values, and querying the full documents would come later as needed. It would require a different kind of iterator that returns the primary value along with the index values.

nlfiedler avatar Dec 01 '20 04:12 nlfiedler