mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
Make the FieldsIdsMap serialization more stable by using a BTreeMap
This commit is contained in:
parent
9133f38138
commit
566a7c3039
9 changed files with 67 additions and 77 deletions
|
@ -1,8 +1,10 @@
|
|||
use std::collections::HashMap;
|
||||
use std::io::{self, BufRead};
|
||||
use std::iter::once;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Context;
|
||||
use heed::EnvOpenOptions;
|
||||
use log::debug;
|
||||
use structopt::StructOpt;
|
||||
|
@ -59,18 +61,22 @@ pub fn run(opt: Opt) -> anyhow::Result<()> {
|
|||
let query = result?;
|
||||
let result = index.search(&rtxn).query(query).execute().unwrap();
|
||||
|
||||
let headers = match index.headers(&rtxn)? {
|
||||
Some(headers) => headers,
|
||||
None => return Ok(()),
|
||||
};
|
||||
let mut stdout = io::stdout();
|
||||
let fields_ids_map = index.fields_ids_map(&rtxn)?.unwrap_or_default();
|
||||
let documents = index.documents(&rtxn, result.documents_ids.iter().cloned())?;
|
||||
|
||||
let mut wtr = csv::Writer::from_writer(io::stdout());
|
||||
wtr.write_record(&headers)?;
|
||||
for (_id, record) in documents {
|
||||
wtr.write_record(record.iter().map(|(_, v)| v))?;
|
||||
let document: anyhow::Result<HashMap<_, _>> = record.iter()
|
||||
.map(|(k, v)| {
|
||||
let key = fields_ids_map.name(k).context("field id not found")?;
|
||||
let val = std::str::from_utf8(v)?;
|
||||
Ok((key, val))
|
||||
})
|
||||
.collect();
|
||||
|
||||
let document = document?;
|
||||
serde_json::to_writer(&mut stdout, &document)?;
|
||||
}
|
||||
wtr.flush()?;
|
||||
|
||||
debug!("Took {:.02?} to find {} documents", before.elapsed(), result.documents_ids.len());
|
||||
}
|
||||
|
|
|
@ -382,22 +382,22 @@ pub fn run(opt: Opt) -> anyhow::Result<()> {
|
|||
let SearchResult { found_words, documents_ids } = search.execute().unwrap();
|
||||
|
||||
let mut documents = Vec::new();
|
||||
if let Some(headers) = index.headers(&rtxn).unwrap() {
|
||||
for (_id, record) in index.documents(&rtxn, documents_ids).unwrap() {
|
||||
let mut record = record.iter()
|
||||
.map(|(key_id, value)| {
|
||||
let key = headers[key_id as usize].to_owned();
|
||||
let value = std::str::from_utf8(value).unwrap().to_owned();
|
||||
(key, value)
|
||||
})
|
||||
.collect();
|
||||
let fields_ids_map = index.fields_ids_map(&rtxn).unwrap().unwrap_or_default();
|
||||
|
||||
if !disable_highlighting {
|
||||
highlight_record(&mut record, &found_words);
|
||||
}
|
||||
for (_id, record) in index.documents(&rtxn, documents_ids).unwrap() {
|
||||
let mut record = record.iter()
|
||||
.map(|(key_id, value)| {
|
||||
let key = fields_ids_map.name(key_id).unwrap().to_owned();
|
||||
let value = std::str::from_utf8(value).unwrap().to_owned();
|
||||
(key, value)
|
||||
})
|
||||
.collect();
|
||||
|
||||
documents.push(record);
|
||||
if !disable_highlighting {
|
||||
highlight_record(&mut record, &found_words);
|
||||
}
|
||||
|
||||
documents.push(record);
|
||||
}
|
||||
|
||||
Response::builder()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue