2023-02-21 09:44:03 +01:00
|
|
|
use heed::RoTxn;
|
|
|
|
use roaring::RoaringBitmap;
|
|
|
|
|
|
|
|
use super::db_cache::DatabaseCache;
|
2023-02-22 15:34:37 +01:00
|
|
|
use super::logger::SearchLogger;
|
2023-02-28 11:49:24 +01:00
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
use super::QueryGraph;
|
|
|
|
use crate::new::graph_based_ranking_rule::GraphBasedRankingRule;
|
2023-03-06 08:35:01 +01:00
|
|
|
use crate::new::ranking_rule_graph::ProximityGraph;
|
|
|
|
use crate::new::ranking_rule_graph::TypoGraph;
|
2023-02-21 09:44:03 +01:00
|
|
|
use crate::new::words::Words;
|
2023-03-02 21:27:42 +01:00
|
|
|
// use crate::search::new::sort::Sort;
|
2023-03-06 08:35:01 +01:00
|
|
|
use crate::{Index, Result, TermsMatchingStrategy};
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
pub trait RankingRuleOutputIter<'transaction, Query> {
|
|
|
|
fn next_bucket(&mut self) -> Result<Option<RankingRuleOutput<Query>>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RankingRuleOutputIterWrapper<'transaction, Query> {
|
|
|
|
iter: Box<dyn Iterator<Item = Result<RankingRuleOutput<Query>>> + 'transaction>,
|
|
|
|
}
|
|
|
|
impl<'transaction, Query> RankingRuleOutputIterWrapper<'transaction, Query> {
|
|
|
|
pub fn new(
|
|
|
|
iter: Box<dyn Iterator<Item = Result<RankingRuleOutput<Query>>> + 'transaction>,
|
|
|
|
) -> Self {
|
|
|
|
Self { iter }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'transaction, Query> RankingRuleOutputIter<'transaction, Query>
|
|
|
|
for RankingRuleOutputIterWrapper<'transaction, Query>
|
|
|
|
{
|
|
|
|
fn next_bucket(&mut self) -> Result<Option<RankingRuleOutput<Query>>> {
|
|
|
|
match self.iter.next() {
|
|
|
|
Some(x) => x.map(Some),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RankingRuleQueryTrait: Sized + Clone + 'static {}
|
2023-02-21 12:33:32 +01:00
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct PlaceholderQuery;
|
|
|
|
impl RankingRuleQueryTrait for PlaceholderQuery {}
|
|
|
|
impl RankingRuleQueryTrait for QueryGraph {}
|
|
|
|
|
|
|
|
pub trait RankingRule<'transaction, Query: RankingRuleQueryTrait> {
|
2023-02-22 15:34:37 +01:00
|
|
|
fn id(&self) -> String;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Prepare the ranking rule such that it can start iterating over its
|
|
|
|
/// buckets using [`next_bucket`](RankingRule::next_bucket).
|
|
|
|
///
|
|
|
|
/// The given universe is the universe that will be given to [`next_bucket`](RankingRule::next_bucket).
|
2023-02-21 09:44:03 +01:00
|
|
|
fn start_iteration(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 15:34:37 +01:00
|
|
|
logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
query: &Query,
|
|
|
|
) -> Result<()>;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Return the next bucket of this ranking rule.
|
|
|
|
///
|
|
|
|
/// The returned candidates MUST be a subset of the given universe.
|
|
|
|
///
|
|
|
|
/// The universe given as argument is either:
|
|
|
|
/// - a subset of the universe given to the previous call to [`next_bucket`](RankingRule::next_bucket); OR
|
|
|
|
/// - the universe given to [`start_iteration`](RankingRule::start_iteration)
|
2023-02-21 09:44:03 +01:00
|
|
|
fn next_bucket(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 15:34:37 +01:00
|
|
|
logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
universe: &RoaringBitmap,
|
|
|
|
) -> Result<Option<RankingRuleOutput<Query>>>;
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
/// Finish iterating over the buckets, which yields control to the parent ranking rule
|
|
|
|
/// The next call to this ranking rule, if any, will be [`start_iteration`](RankingRule::start_iteration).
|
2023-02-21 09:44:03 +01:00
|
|
|
fn end_iteration(
|
|
|
|
&mut self,
|
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction RoTxn,
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
2023-02-22 15:34:37 +01:00
|
|
|
logger: &mut dyn SearchLogger<Query>,
|
2023-02-21 09:44:03 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RankingRuleOutput<Q> {
|
2023-02-21 12:33:32 +01:00
|
|
|
/// The query corresponding to the current bucket for the child ranking rule
|
2023-02-21 09:44:03 +01:00
|
|
|
pub query: Q,
|
|
|
|
/// The allowed candidates for the child ranking rule
|
|
|
|
pub candidates: RoaringBitmap,
|
|
|
|
}
|
|
|
|
|
2023-02-27 16:45:07 +01:00
|
|
|
// TODO: can make it generic over the query type (either query graph or placeholder) fairly easily
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-03-06 08:35:01 +01:00
|
|
|
pub fn apply_ranking_rules<'transaction>(
|
2023-02-21 09:44:03 +01:00
|
|
|
index: &Index,
|
|
|
|
txn: &'transaction heed::RoTxn,
|
|
|
|
// TODO: ranking rules parameter
|
|
|
|
db_cache: &mut DatabaseCache<'transaction>,
|
|
|
|
query_graph: &QueryGraph,
|
2023-03-06 08:35:01 +01:00
|
|
|
universe: &RoaringBitmap,
|
2023-02-27 16:14:53 +01:00
|
|
|
from: usize,
|
|
|
|
length: usize,
|
2023-02-27 16:45:07 +01:00
|
|
|
logger: &mut dyn SearchLogger<QueryGraph>,
|
2023-02-21 09:44:03 +01:00
|
|
|
) -> Result<Vec<u32>> {
|
2023-03-06 08:35:01 +01:00
|
|
|
logger.initial_query(query_graph);
|
2023-02-28 12:42:29 +01:00
|
|
|
let words = &mut Words::new(TermsMatchingStrategy::Last);
|
2023-03-02 21:27:42 +01:00
|
|
|
// let sort = &mut Sort::new(index, txn, "release_date".to_owned(), true)?;
|
2023-02-28 12:42:29 +01:00
|
|
|
let proximity = &mut GraphBasedRankingRule::<ProximityGraph>::new("proximity".to_owned());
|
2023-02-28 14:19:57 +01:00
|
|
|
let typo = &mut GraphBasedRankingRule::<TypoGraph>::new("typo".to_owned());
|
2023-02-21 09:44:03 +01:00
|
|
|
// TODO: ranking rules given as argument
|
2023-02-28 12:42:29 +01:00
|
|
|
let mut ranking_rules: Vec<&mut dyn RankingRule<'transaction, QueryGraph>> =
|
2023-03-02 21:27:42 +01:00
|
|
|
vec![words, typo, proximity /*sort*/];
|
2023-02-21 09:44:03 +01:00
|
|
|
|
2023-02-22 15:34:37 +01:00
|
|
|
logger.ranking_rules(&ranking_rules);
|
|
|
|
|
2023-02-27 16:45:07 +01:00
|
|
|
if universe.len() < from as u64 {
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
let ranking_rules_len = ranking_rules.len();
|
2023-03-06 08:35:01 +01:00
|
|
|
logger.start_iteration_ranking_rule(0, ranking_rules[0], query_graph, universe);
|
|
|
|
ranking_rules[0].start_iteration(index, txn, db_cache, logger, universe, query_graph)?;
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
let mut candidates = vec![RoaringBitmap::default(); ranking_rules_len];
|
|
|
|
candidates[0] = universe.clone();
|
|
|
|
|
|
|
|
let mut cur_ranking_rule_index = 0;
|
2023-02-27 15:04:40 +01:00
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
macro_rules! back {
|
|
|
|
() => {
|
2023-03-02 21:27:42 +01:00
|
|
|
assert!(candidates[cur_ranking_rule_index].is_empty());
|
2023-02-22 15:34:37 +01:00
|
|
|
logger.end_iteration_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-02-28 12:42:29 +01:00
|
|
|
ranking_rules[cur_ranking_rule_index],
|
2023-02-22 15:34:37 +01:00
|
|
|
&candidates[cur_ranking_rule_index],
|
|
|
|
);
|
2023-02-21 09:44:03 +01:00
|
|
|
candidates[cur_ranking_rule_index].clear();
|
2023-02-22 15:34:37 +01:00
|
|
|
ranking_rules[cur_ranking_rule_index].end_iteration(index, txn, db_cache, logger);
|
2023-02-21 09:44:03 +01:00
|
|
|
if cur_ranking_rule_index == 0 {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
cur_ranking_rule_index -= 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut results = vec![];
|
2023-02-27 16:14:53 +01:00
|
|
|
let mut cur_offset = 0usize;
|
|
|
|
|
2023-02-28 11:49:24 +01:00
|
|
|
// Add the candidates to the results. Take the `from`, `limit`, and `cur_offset`
|
|
|
|
// into account and inform the logger.
|
2023-02-27 16:45:07 +01:00
|
|
|
macro_rules! maybe_add_to_results {
|
2023-02-27 15:04:40 +01:00
|
|
|
($candidates:expr) => {
|
2023-02-27 16:14:53 +01:00
|
|
|
let candidates = $candidates;
|
|
|
|
let len = candidates.len();
|
2023-02-27 16:45:07 +01:00
|
|
|
// if the candidates are empty, there is nothing to do;
|
2023-02-27 16:14:53 +01:00
|
|
|
if !candidates.is_empty() {
|
|
|
|
if cur_offset < from {
|
|
|
|
if cur_offset + (candidates.len() as usize) < from {
|
|
|
|
logger.skip_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-02-28 12:42:29 +01:00
|
|
|
ranking_rules[cur_ranking_rule_index],
|
2023-02-27 16:14:53 +01:00
|
|
|
&candidates,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let all_candidates = candidates.iter().collect::<Vec<_>>();
|
|
|
|
let (skipped_candidates, candidates) =
|
|
|
|
all_candidates.split_at(from - cur_offset);
|
|
|
|
logger.skip_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-02-28 12:42:29 +01:00
|
|
|
ranking_rules[cur_ranking_rule_index],
|
2023-02-27 16:14:53 +01:00
|
|
|
&skipped_candidates.into_iter().collect(),
|
|
|
|
);
|
|
|
|
let candidates = candidates
|
|
|
|
.iter()
|
|
|
|
.take(length - results.len())
|
|
|
|
.copied()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
logger.add_to_results(&candidates);
|
|
|
|
results.extend(&candidates);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let candidates =
|
|
|
|
candidates.iter().take(length - results.len()).collect::<Vec<_>>();
|
|
|
|
logger.add_to_results(&candidates);
|
|
|
|
results.extend(&candidates);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur_offset += len as usize;
|
2023-02-27 15:04:40 +01:00
|
|
|
};
|
|
|
|
}
|
2023-02-27 16:14:53 +01:00
|
|
|
while results.len() < length {
|
2023-02-21 09:44:03 +01:00
|
|
|
// The universe for this bucket is zero or one element, so we don't need to sort
|
|
|
|
// anything, just extend the results and go back to the parent ranking rule.
|
|
|
|
if candidates[cur_ranking_rule_index].len() <= 1 {
|
2023-02-27 16:45:07 +01:00
|
|
|
maybe_add_to_results!(&candidates[cur_ranking_rule_index]);
|
2023-03-02 21:27:42 +01:00
|
|
|
candidates[cur_ranking_rule_index].clear();
|
2023-02-21 09:44:03 +01:00
|
|
|
back!();
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-22 15:34:37 +01:00
|
|
|
|
|
|
|
let Some(next_bucket) = ranking_rules[cur_ranking_rule_index].next_bucket(index, txn, db_cache, logger, &candidates[cur_ranking_rule_index])? else {
|
2023-02-28 14:19:57 +01:00
|
|
|
// TODO: add remaining candidates automatically here?
|
2023-02-21 09:44:03 +01:00
|
|
|
back!();
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
logger.next_bucket_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
|
|
|
ranking_rules[cur_ranking_rule_index],
|
|
|
|
&candidates[cur_ranking_rule_index],
|
|
|
|
&next_bucket.candidates,
|
|
|
|
);
|
|
|
|
|
2023-02-28 12:42:29 +01:00
|
|
|
assert!(candidates[cur_ranking_rule_index].is_superset(&next_bucket.candidates));
|
2023-02-21 09:44:03 +01:00
|
|
|
candidates[cur_ranking_rule_index] -= &next_bucket.candidates;
|
|
|
|
|
2023-02-27 16:45:07 +01:00
|
|
|
if cur_ranking_rule_index == ranking_rules_len - 1
|
|
|
|
|| next_bucket.candidates.len() <= 1
|
|
|
|
|| cur_offset + (next_bucket.candidates.len() as usize) < from
|
|
|
|
{
|
|
|
|
maybe_add_to_results!(&next_bucket.candidates);
|
2023-02-21 09:44:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-27 16:45:07 +01:00
|
|
|
|
|
|
|
cur_ranking_rule_index += 1;
|
|
|
|
candidates[cur_ranking_rule_index] = next_bucket.candidates.clone();
|
|
|
|
logger.start_iteration_ranking_rule(
|
|
|
|
cur_ranking_rule_index,
|
2023-02-28 12:42:29 +01:00
|
|
|
ranking_rules[cur_ranking_rule_index],
|
2023-02-27 16:45:07 +01:00
|
|
|
&next_bucket.query,
|
|
|
|
&candidates[cur_ranking_rule_index],
|
|
|
|
);
|
|
|
|
ranking_rules[cur_ranking_rule_index].start_iteration(
|
|
|
|
index,
|
|
|
|
txn,
|
|
|
|
db_cache,
|
|
|
|
logger,
|
|
|
|
&next_bucket.candidates,
|
|
|
|
&next_bucket.query,
|
|
|
|
)?;
|
2023-02-21 09:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-03-02 21:27:42 +01:00
|
|
|
// use crate::allocator::ALLOC;
|
2023-02-21 09:44:03 +01:00
|
|
|
use crate::documents::{DocumentsBatchBuilder, DocumentsBatchReader};
|
|
|
|
use crate::index::tests::TempIndex;
|
|
|
|
use crate::new::db_cache::DatabaseCache;
|
2023-03-06 08:35:01 +01:00
|
|
|
use crate::new::execute_search;
|
2023-03-02 21:27:42 +01:00
|
|
|
use big_s::S;
|
|
|
|
use heed::EnvOpenOptions;
|
|
|
|
use maplit::hashset;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{BufRead, BufReader, Cursor, Seek};
|
|
|
|
use std::time::Instant;
|
|
|
|
// use crate::new::logger::detailed::DetailedSearchLogger;
|
2023-03-06 08:35:01 +01:00
|
|
|
use crate::new::logger::DefaultSearchLogger;
|
2023-02-21 09:44:03 +01:00
|
|
|
use crate::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings};
|
|
|
|
use crate::{Criterion, Index, Object, Search, TermsMatchingStrategy};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_new_search() {
|
|
|
|
let index = TempIndex::new();
|
|
|
|
index
|
|
|
|
.add_documents(documents!([
|
|
|
|
{
|
|
|
|
"id": 7,
|
|
|
|
"text": "the super quick super brown fox jumps over",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 8,
|
|
|
|
"text": "the super quick brown fox jumps over",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 9,
|
|
|
|
"text": "the quick super brown fox jumps over",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 10,
|
|
|
|
"text": "the quick brown fox jumps over",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 11,
|
|
|
|
"text": "the quick brown fox jumps over the lazy dog",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 12,
|
|
|
|
"text": "the quick brown cat jumps over the lazy dog",
|
|
|
|
},
|
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
let mut db_cache = DatabaseCache::default();
|
|
|
|
|
2023-03-06 08:35:01 +01:00
|
|
|
let results = execute_search(
|
|
|
|
&index,
|
|
|
|
&txn,
|
|
|
|
&mut db_cache,
|
|
|
|
"releases from poison by the government",
|
|
|
|
None,
|
|
|
|
0,
|
|
|
|
50,
|
|
|
|
&mut DefaultSearchLogger,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
println!("{results:?}")
|
|
|
|
}
|
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
#[test]
|
|
|
|
fn search_wiki_new() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_wiki").unwrap();
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
println!("nbr docids: {}", index.documents_ids(&txn).unwrap().len());
|
|
|
|
|
|
|
|
// loop {
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let mut db_cache = DatabaseCache::default();
|
|
|
|
|
2023-03-06 10:21:28 +01:00
|
|
|
let mut logger = crate::new::logger::detailed::DetailedSearchLogger::new("log");
|
2023-03-02 21:27:42 +01:00
|
|
|
|
|
|
|
let results = execute_search(
|
|
|
|
&index,
|
|
|
|
&txn,
|
|
|
|
&mut db_cache,
|
2023-03-06 10:21:28 +01:00
|
|
|
"releases from poison by the government",
|
2023-03-02 21:27:42 +01:00
|
|
|
None,
|
|
|
|
0,
|
|
|
|
20,
|
2023-03-06 10:21:28 +01:00
|
|
|
// &mut DefaultSearchLogger,
|
|
|
|
&mut logger,
|
2023-03-02 21:27:42 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-06 10:21:28 +01:00
|
|
|
logger.write_d2_description();
|
2023-03-02 21:27:42 +01:00
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
2023-03-06 10:21:28 +01:00
|
|
|
let documents = index
|
2023-03-02 21:27:42 +01:00
|
|
|
.documents(&txn, results.iter().copied())
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
2023-03-06 10:21:28 +01:00
|
|
|
.map(|(id, obkv)| {
|
|
|
|
let mut object = serde_json::Map::default();
|
|
|
|
for (fid, fid_name) in index.fields_ids_map(&txn).unwrap().iter() {
|
|
|
|
let value = obkv.get(fid).unwrap();
|
|
|
|
let value: serde_json::Value = serde_json::from_slice(value).unwrap();
|
|
|
|
object.insert(fid_name.to_owned(), value);
|
|
|
|
}
|
|
|
|
(id, serde_json::to_string_pretty(&object).unwrap())
|
2023-03-02 21:27:42 +01:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2023-03-06 10:21:28 +01:00
|
|
|
println!("{}us: {:?}", elapsed.as_micros(), results);
|
|
|
|
for (id, document) in documents {
|
|
|
|
println!("{id}:");
|
|
|
|
println!("{document}");
|
|
|
|
}
|
2023-03-02 21:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn search_wiki_old() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_wiki").unwrap();
|
|
|
|
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let rr = index.criteria(&txn).unwrap();
|
|
|
|
println!("{rr:?}");
|
|
|
|
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
|
|
|
s.query("releases from poison by the government");
|
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::Last);
|
|
|
|
s.criterion_implementation_strategy(crate::CriterionImplementationStrategy::OnlySetBased);
|
|
|
|
let docs = s.execute().unwrap();
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
2023-03-06 10:21:28 +01:00
|
|
|
let documents = index
|
2023-03-02 21:27:42 +01:00
|
|
|
.documents(&txn, docs.documents_ids.iter().copied())
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
2023-03-06 10:21:28 +01:00
|
|
|
.map(|(id, obkv)| {
|
|
|
|
let mut object = serde_json::Map::default();
|
|
|
|
for (fid, fid_name) in index.fields_ids_map(&txn).unwrap().iter() {
|
|
|
|
let value = obkv.get(fid).unwrap();
|
|
|
|
let value: serde_json::Value = serde_json::from_slice(value).unwrap();
|
|
|
|
object.insert(fid_name.to_owned(), value);
|
|
|
|
}
|
|
|
|
(id, serde_json::to_string_pretty(&object).unwrap())
|
2023-03-02 21:27:42 +01:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
println!("{}us: {:?}", elapsed.as_micros(), docs.documents_ids);
|
2023-03-06 10:21:28 +01:00
|
|
|
for (id, document) in documents {
|
|
|
|
println!("{id}:");
|
|
|
|
println!("{document}");
|
|
|
|
}
|
2023-03-02 21:27:42 +01:00
|
|
|
}
|
2023-02-21 09:44:03 +01:00
|
|
|
#[test]
|
|
|
|
fn search_movies_new() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_movies").unwrap();
|
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
|
|
|
let primary_key = index.primary_key(&txn).unwrap().unwrap();
|
|
|
|
let primary_key = index.fields_ids_map(&txn).unwrap().id(primary_key).unwrap();
|
2023-03-02 21:27:42 +01:00
|
|
|
// loop {
|
2023-02-21 09:44:03 +01:00
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let mut db_cache = DatabaseCache::default();
|
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
let mut logger = crate::new::logger::detailed::DetailedSearchLogger::new("log");
|
2023-02-22 15:34:37 +01:00
|
|
|
|
|
|
|
let results = execute_search(
|
|
|
|
&index,
|
|
|
|
&txn,
|
|
|
|
&mut db_cache,
|
2023-03-06 08:35:01 +01:00
|
|
|
"releases from poison by the government",
|
2023-02-27 16:45:07 +01:00
|
|
|
None,
|
2023-03-02 21:27:42 +01:00
|
|
|
0,
|
2023-02-28 11:49:24 +01:00
|
|
|
20,
|
2023-03-02 21:27:42 +01:00
|
|
|
// &mut DefaultSearchLogger,
|
|
|
|
&mut logger,
|
2023-02-22 15:34:37 +01:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
logger.write_d2_description();
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
|
|
|
let ids = index
|
|
|
|
.documents(&txn, results.iter().copied())
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
let obkv = &x.1;
|
|
|
|
let id = obkv.get(primary_key).unwrap();
|
|
|
|
let id: serde_json::Value = serde_json::from_slice(id).unwrap();
|
|
|
|
id.as_str().unwrap().to_owned()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
println!("{}us: {results:?}", elapsed.as_micros());
|
|
|
|
println!("external ids: {ids:?}");
|
2023-03-02 21:27:42 +01:00
|
|
|
// }
|
2023-02-21 09:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn search_movies_old() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_movies").unwrap();
|
2023-03-02 21:27:42 +01:00
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
let txn = index.read_txn().unwrap();
|
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
let rr = index.criteria(&txn).unwrap();
|
|
|
|
println!("{rr:?}");
|
|
|
|
|
|
|
|
let primary_key = index.primary_key(&txn).unwrap().unwrap();
|
|
|
|
let primary_key = index.fields_ids_map(&txn).unwrap().id(primary_key).unwrap();
|
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
let mut s = Search::new(&txn, &index);
|
2023-03-02 21:27:42 +01:00
|
|
|
s.query("releases from poison by the government");
|
2023-02-21 09:44:03 +01:00
|
|
|
s.terms_matching_strategy(TermsMatchingStrategy::Last);
|
2023-02-21 12:33:32 +01:00
|
|
|
s.criterion_implementation_strategy(crate::CriterionImplementationStrategy::OnlySetBased);
|
2023-02-21 09:44:03 +01:00
|
|
|
let docs = s.execute().unwrap();
|
|
|
|
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
|
2023-03-02 21:27:42 +01:00
|
|
|
let ids = index
|
|
|
|
.documents(&txn, docs.documents_ids.iter().copied())
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
let obkv = &x.1;
|
|
|
|
let id = obkv.get(primary_key).unwrap();
|
|
|
|
let id: serde_json::Value = serde_json::from_slice(id).unwrap();
|
|
|
|
id.as_str().unwrap().to_owned()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2023-02-21 09:44:03 +01:00
|
|
|
println!("{}us: {:?}", elapsed.as_micros(), docs.documents_ids);
|
2023-03-02 21:27:42 +01:00
|
|
|
println!("external ids: {ids:?}");
|
2023-02-21 09:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn _settings_movies() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_movies").unwrap();
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
|
|
|
|
|
|
|
builder.set_min_word_len_one_typo(5);
|
|
|
|
builder.set_min_word_len_two_typos(100);
|
2023-03-02 21:27:42 +01:00
|
|
|
builder.set_sortable_fields(hashset! { S("release_date") });
|
|
|
|
builder.set_criteria(vec![
|
|
|
|
Criterion::Words,
|
|
|
|
Criterion::Typo,
|
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Asc("release_date".to_owned()),
|
|
|
|
]);
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
builder.execute(|_| (), || false).unwrap();
|
2023-03-02 21:27:42 +01:00
|
|
|
wtxn.commit().unwrap();
|
2023-02-21 09:44:03 +01:00
|
|
|
}
|
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
#[test]
|
2023-02-21 09:44:03 +01:00
|
|
|
fn _index_movies() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_movies").unwrap();
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
let primary_key = "id";
|
|
|
|
let searchable_fields = vec!["title", "overview"];
|
|
|
|
let filterable_fields = vec!["release_date", "genres"];
|
|
|
|
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
|
|
|
builder.set_primary_key(primary_key.to_owned());
|
|
|
|
let searchable_fields = searchable_fields.iter().map(|s| s.to_string()).collect();
|
|
|
|
builder.set_searchable_fields(searchable_fields);
|
|
|
|
let filterable_fields = filterable_fields.iter().map(|s| s.to_string()).collect();
|
|
|
|
builder.set_filterable_fields(filterable_fields);
|
2023-03-02 21:27:42 +01:00
|
|
|
|
2023-02-21 12:33:32 +01:00
|
|
|
builder.set_min_word_len_one_typo(5);
|
|
|
|
builder.set_min_word_len_two_typos(100);
|
|
|
|
builder.set_criteria(vec![Criterion::Words, Criterion::Proximity]);
|
2023-02-21 09:44:03 +01:00
|
|
|
builder.execute(|_| (), || false).unwrap();
|
|
|
|
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config = IndexDocumentsConfig::default();
|
|
|
|
let builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| (), || false)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let documents = documents_from(
|
|
|
|
"/Users/meilisearch/Documents/milli2/benchmarks/datasets/movies.json",
|
|
|
|
"json",
|
|
|
|
);
|
|
|
|
let (builder, user_error) = builder.add_documents(documents).unwrap();
|
|
|
|
user_error.unwrap();
|
|
|
|
builder.execute().unwrap();
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
index.prepare_for_closing().wait();
|
|
|
|
}
|
2023-03-02 21:27:42 +01:00
|
|
|
#[test]
|
|
|
|
fn _index_wiki() {
|
|
|
|
let mut options = EnvOpenOptions::new();
|
|
|
|
options.map_size(100 * 1024 * 1024 * 1024); // 100 GB
|
|
|
|
|
|
|
|
let index = Index::new(options, "data_wiki").unwrap();
|
|
|
|
let mut wtxn = index.write_txn().unwrap();
|
|
|
|
|
|
|
|
// let primary_key = "id";
|
|
|
|
let searchable_fields = vec!["body", "title", "url"];
|
|
|
|
// let filterable_fields = vec![];
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let mut builder = Settings::new(&mut wtxn, &index, &config);
|
|
|
|
// builder.set_primary_key(primary_key.to_owned());
|
|
|
|
let searchable_fields = searchable_fields.iter().map(|s| s.to_string()).collect();
|
|
|
|
builder.set_searchable_fields(searchable_fields);
|
|
|
|
// let filterable_fields = filterable_fields.iter().map(|s| s.to_string()).collect();
|
|
|
|
// builder.set_filterable_fields(filterable_fields);
|
|
|
|
|
|
|
|
// builder.set_min_word_len_one_typo(5);
|
|
|
|
// builder.set_min_word_len_two_typos(100);
|
|
|
|
builder.set_criteria(vec![Criterion::Words, Criterion::Typo, Criterion::Proximity]);
|
|
|
|
builder.execute(|_| (), || false).unwrap();
|
|
|
|
|
|
|
|
let config = IndexerConfig::default();
|
|
|
|
let indexing_config =
|
|
|
|
IndexDocumentsConfig { autogenerate_docids: true, ..Default::default() };
|
|
|
|
let builder =
|
|
|
|
IndexDocuments::new(&mut wtxn, &index, &config, indexing_config, |_| (), || false)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let documents = documents_from(
|
|
|
|
"/Users/meilisearch/Documents/milli2/benchmarks/datasets/smol-wiki-articles.csv",
|
|
|
|
"csv",
|
|
|
|
);
|
|
|
|
let (builder, user_error) = builder.add_documents(documents).unwrap();
|
|
|
|
user_error.unwrap();
|
|
|
|
builder.execute().unwrap();
|
|
|
|
wtxn.commit().unwrap();
|
|
|
|
|
|
|
|
index.prepare_for_closing().wait();
|
|
|
|
}
|
2023-02-21 09:44:03 +01:00
|
|
|
|
|
|
|
fn documents_from(filename: &str, filetype: &str) -> DocumentsBatchReader<impl BufRead + Seek> {
|
|
|
|
let reader = File::open(filename)
|
|
|
|
.unwrap_or_else(|_| panic!("could not find the dataset in: {}", filename));
|
|
|
|
let reader = BufReader::new(reader);
|
|
|
|
let documents = match filetype {
|
|
|
|
"csv" => documents_from_csv(reader).unwrap(),
|
|
|
|
"json" => documents_from_json(reader).unwrap(),
|
|
|
|
"jsonl" => documents_from_jsonl(reader).unwrap(),
|
|
|
|
otherwise => panic!("invalid update format {:?}", otherwise),
|
|
|
|
};
|
|
|
|
DocumentsBatchReader::from_reader(Cursor::new(documents)).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn documents_from_jsonl(reader: impl BufRead) -> crate::Result<Vec<u8>> {
|
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
|
|
|
|
for result in serde_json::Deserializer::from_reader(reader).into_iter::<Object>() {
|
|
|
|
let object = result.unwrap();
|
|
|
|
documents.append_json_object(&object)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
documents.into_inner().map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn documents_from_json(reader: impl BufRead) -> crate::Result<Vec<u8>> {
|
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
|
|
|
|
documents.append_json_array(reader)?;
|
|
|
|
|
|
|
|
documents.into_inner().map_err(Into::into)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn documents_from_csv(reader: impl BufRead) -> crate::Result<Vec<u8>> {
|
|
|
|
let csv = csv::Reader::from_reader(reader);
|
|
|
|
|
|
|
|
let mut documents = DocumentsBatchBuilder::new(Vec::new());
|
|
|
|
documents.append_csv(csv)?;
|
|
|
|
|
|
|
|
documents.into_inner().map_err(Into::into)
|
|
|
|
}
|
|
|
|
}
|