2020-05-05 22:29:35 +02:00
|
|
|
use std::borrow::Cow;
|
2020-05-07 19:25:18 +02:00
|
|
|
use std::collections::HashMap;
|
2020-05-28 19:35:34 +02:00
|
|
|
use std::ops::{Deref, Range};
|
2019-12-11 17:02:10 +01:00
|
|
|
use std::time::Duration;
|
2019-10-02 17:34:32 +02:00
|
|
|
|
2020-05-12 14:36:28 +02:00
|
|
|
use either::Either;
|
2020-05-28 19:35:34 +02:00
|
|
|
use sdset::{SetOperation, SetBuf, Set};
|
2020-05-12 14:36:28 +02:00
|
|
|
|
|
|
|
use meilisearch_schema::FieldId;
|
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
use crate::bucket_sort::{bucket_sort, bucket_sort_with_distinct, SortResult, placeholder_document_sort, facet_count};
|
2019-12-11 17:36:53 +01:00
|
|
|
use crate::database::MainT;
|
2020-05-05 22:29:35 +02:00
|
|
|
use crate::facets::FacetFilter;
|
2020-05-28 19:35:34 +02:00
|
|
|
use crate::distinct_map::{DistinctMap, BufferedDistinctMap};
|
|
|
|
use crate::Document;
|
|
|
|
use crate::{criterion::Criteria, DocumentId};
|
|
|
|
use crate::{reordered_attrs::ReorderedAttrs, store, MResult, MainReader};
|
2019-10-02 17:34:32 +02:00
|
|
|
|
2020-05-12 12:19:44 +02:00
|
|
|
pub struct QueryBuilder<'c, 'f, 'd, 'i> {
|
2019-10-09 13:44:18 +02:00
|
|
|
criteria: Criteria<'c>,
|
|
|
|
searchable_attrs: Option<ReorderedAttrs>,
|
2019-10-17 14:45:21 +02:00
|
|
|
filter: Option<Box<dyn Fn(DocumentId) -> bool + 'f>>,
|
|
|
|
distinct: Option<(Box<dyn Fn(DocumentId) -> Option<u64> + 'd>, usize)>,
|
2019-10-09 17:59:31 +02:00
|
|
|
timeout: Option<Duration>,
|
2020-05-05 22:29:35 +02:00
|
|
|
index: &'i store::Index,
|
2020-05-12 11:22:09 +02:00
|
|
|
facet_filter: Option<FacetFilter>,
|
|
|
|
facets: Option<Vec<(FieldId, String)>>,
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 12:19:44 +02:00
|
|
|
impl<'c, 'f, 'd, 'i> QueryBuilder<'c, 'f, 'd, 'i> {
|
2020-05-05 22:29:35 +02:00
|
|
|
pub fn new(index: &'i store::Index) -> Self {
|
2020-05-28 19:35:34 +02:00
|
|
|
QueryBuilder::with_criteria(index, Criteria::default())
|
2019-10-09 13:44:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 19:25:18 +02:00
|
|
|
/// sets facet attributes to filter on
|
2020-05-12 11:22:09 +02:00
|
|
|
pub fn set_facet_filter(&mut self, facets: Option<FacetFilter>) {
|
|
|
|
self.facet_filter = facets;
|
2020-05-07 19:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// sets facet attributes for which to return the count
|
2020-05-12 12:19:44 +02:00
|
|
|
pub fn set_facets(&mut self, facets: Option<Vec<(FieldId, String)>>) {
|
2020-05-05 22:29:35 +02:00
|
|
|
self.facets = facets;
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
pub fn with_criteria(index: &'i store::Index, criteria: Criteria<'c>) -> Self {
|
2019-10-02 17:34:32 +02:00
|
|
|
QueryBuilder {
|
2019-10-09 13:44:18 +02:00
|
|
|
criteria,
|
|
|
|
searchable_attrs: None,
|
|
|
|
filter: None,
|
2019-10-17 14:45:21 +02:00
|
|
|
distinct: None,
|
2019-10-09 17:59:31 +02:00
|
|
|
timeout: None,
|
2020-05-05 22:29:35 +02:00
|
|
|
index,
|
2020-05-12 11:22:09 +02:00
|
|
|
facet_filter: None,
|
2020-05-05 22:29:35 +02:00
|
|
|
facets: None,
|
2019-10-02 17:34:32 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 13:44:18 +02:00
|
|
|
|
2019-10-17 14:45:21 +02:00
|
|
|
pub fn with_filter<F>(&mut self, function: F)
|
2019-10-18 13:05:28 +02:00
|
|
|
where
|
|
|
|
F: Fn(DocumentId) -> bool + 'f,
|
2019-10-09 13:44:18 +02:00
|
|
|
{
|
2019-10-17 14:45:21 +02:00
|
|
|
self.filter = Some(Box::new(function))
|
2019-10-09 13:44:18 +02:00
|
|
|
}
|
2019-10-02 17:34:32 +02:00
|
|
|
|
2019-10-17 14:45:21 +02:00
|
|
|
pub fn with_fetch_timeout(&mut self, timeout: Duration) {
|
|
|
|
self.timeout = Some(timeout)
|
2019-10-09 13:44:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-02 14:34:29 +01:00
|
|
|
pub fn with_distinct<F>(&mut self, size: usize, function: F)
|
2019-10-18 13:05:28 +02:00
|
|
|
where
|
|
|
|
F: Fn(DocumentId) -> Option<u64> + 'd,
|
2019-10-09 13:44:18 +02:00
|
|
|
{
|
2019-10-17 14:45:21 +02:00
|
|
|
self.distinct = Some((Box::new(function), size))
|
2019-10-09 13:44:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_searchable_attribute(&mut self, attribute: u16) {
|
2019-12-13 13:22:54 +01:00
|
|
|
let reorders = self.searchable_attrs.get_or_insert_with(ReorderedAttrs::new);
|
2019-10-09 13:44:18 +02:00
|
|
|
reorders.insert_attribute(attribute);
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
/// returns the documents ids associated with a facet filter by computing the union and
|
|
|
|
/// intersection of the document sets
|
|
|
|
fn facets_docids(&self, reader: &MainReader) -> MResult<Option<SetBuf<DocumentId>>> {
|
|
|
|
let facet_docids = match self.facet_filter {
|
|
|
|
Some(ref facets) => {
|
2020-05-05 22:29:35 +02:00
|
|
|
let mut ands = Vec::with_capacity(facets.len());
|
|
|
|
let mut ors = Vec::new();
|
|
|
|
for f in facets.deref() {
|
|
|
|
match f {
|
|
|
|
Either::Left(keys) => {
|
|
|
|
ors.reserve(keys.len());
|
|
|
|
for key in keys {
|
2020-05-28 19:35:34 +02:00
|
|
|
let docids = self
|
|
|
|
.index
|
|
|
|
.facets
|
|
|
|
.facet_document_ids(reader, &key)?
|
|
|
|
.unwrap_or_default();
|
2020-05-05 22:29:35 +02:00
|
|
|
ors.push(docids);
|
|
|
|
}
|
2020-06-26 14:06:29 +02:00
|
|
|
let sets: Vec<_> = ors.iter().map(|(_, i)| i).map(Cow::deref).collect();
|
2020-06-26 12:59:20 +02:00
|
|
|
let or_result = sdset::multi::OpBuilder::from_vec(sets).union().into_set_buf();
|
2020-05-05 22:29:35 +02:00
|
|
|
ands.push(Cow::Owned(or_result));
|
|
|
|
ors.clear();
|
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
Either::Right(key) => {
|
2020-05-05 22:29:35 +02:00
|
|
|
match self.index.facets.facet_document_ids(reader, &key)? {
|
2020-06-26 14:06:29 +02:00
|
|
|
Some((_name, docids)) => ands.push(docids),
|
2020-05-05 22:29:35 +02:00
|
|
|
// no candidates for search, early return.
|
2020-05-28 19:35:34 +02:00
|
|
|
None => return Ok(Some(SetBuf::default())),
|
2020-05-05 22:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let ands: Vec<_> = ands.iter().map(Cow::deref).collect();
|
2020-05-28 19:35:34 +02:00
|
|
|
Some(
|
|
|
|
sdset::multi::OpBuilder::from_vec(ands)
|
|
|
|
.intersection()
|
|
|
|
.into_set_buf(),
|
|
|
|
)
|
2020-05-05 22:29:35 +02:00
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
None => None,
|
2020-05-05 22:29:35 +02:00
|
|
|
};
|
2020-05-28 19:35:34 +02:00
|
|
|
Ok(facet_docids)
|
|
|
|
}
|
2020-05-05 22:29:35 +02:00
|
|
|
|
2020-05-28 19:35:34 +02:00
|
|
|
fn standard_query(self, reader: &MainReader, query: &str, range: Range<usize>) -> MResult<SortResult> {
|
|
|
|
let facets_docids = match self.facets_docids(reader)? {
|
|
|
|
Some(ids) if ids.is_empty() => return Ok(SortResult::default()),
|
|
|
|
other => other
|
|
|
|
};
|
2020-05-12 11:22:09 +02:00
|
|
|
// for each field to retrieve the count for, create an HashMap associating the attribute
|
|
|
|
// value to a set of matching documents. The HashMaps are them collected in another
|
|
|
|
// HashMap, associating each HashMap to it's field.
|
2020-05-28 19:35:34 +02:00
|
|
|
let facet_count_docids = self.facet_count_docids(reader)?;
|
2020-05-07 19:25:18 +02:00
|
|
|
|
2019-10-17 14:45:21 +02:00
|
|
|
match self.distinct {
|
2019-12-11 17:36:53 +01:00
|
|
|
Some((distinct, distinct_size)) => bucket_sort_with_distinct(
|
|
|
|
reader,
|
|
|
|
query,
|
|
|
|
range,
|
2020-05-05 22:29:35 +02:00
|
|
|
facets_docids,
|
2020-05-07 19:25:18 +02:00
|
|
|
facet_count_docids,
|
2019-12-11 17:36:53 +01:00
|
|
|
self.filter,
|
|
|
|
distinct,
|
|
|
|
distinct_size,
|
|
|
|
self.criteria,
|
2019-12-13 13:22:54 +01:00
|
|
|
self.searchable_attrs,
|
2020-06-27 11:36:59 +02:00
|
|
|
self.index,
|
2019-12-11 17:36:53 +01:00
|
|
|
),
|
2019-11-30 16:53:34 +01:00
|
|
|
None => bucket_sort(
|
2019-10-18 13:05:28 +02:00
|
|
|
reader,
|
|
|
|
query,
|
|
|
|
range,
|
2020-05-05 22:29:35 +02:00
|
|
|
facets_docids,
|
2020-05-07 19:25:18 +02:00
|
|
|
facet_count_docids,
|
2019-12-11 17:36:53 +01:00
|
|
|
self.filter,
|
2019-12-11 17:02:10 +01:00
|
|
|
self.criteria,
|
2019-12-13 13:22:54 +01:00
|
|
|
self.searchable_attrs,
|
2020-06-27 11:36:59 +02:00
|
|
|
self.index,
|
2019-10-18 13:05:28 +02:00
|
|
|
),
|
2019-10-09 13:44:18 +02:00
|
|
|
}
|
2019-10-17 14:45:21 +02:00
|
|
|
}
|
2020-05-28 19:35:34 +02:00
|
|
|
|
|
|
|
fn placeholder_query(self, reader: &heed::RoTxn<MainT>, range: Range<usize>) -> MResult<SortResult> {
|
|
|
|
match self.facets_docids(reader)? {
|
|
|
|
Some(docids) => {
|
|
|
|
// We sort the docids from facets according to the criteria set by the user
|
|
|
|
let mut sorted_docids = docids.clone().into_vec();
|
|
|
|
let mut sort_result = match self.index.main.ranked_map(reader)? {
|
|
|
|
Some(ranked_map) => {
|
|
|
|
placeholder_document_sort(&mut sorted_docids, self.index, reader, &ranked_map)?;
|
|
|
|
self.sort_result_from_docids(&sorted_docids, range)
|
|
|
|
},
|
|
|
|
// if we can't perform a sort, we return documents unordered
|
|
|
|
None => self.sort_result_from_docids(&docids, range),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(f) = self.facet_count_docids(reader)? {
|
|
|
|
sort_result.exhaustive_facets_count = Some(true);
|
|
|
|
sort_result.facets = Some(facet_count(f, &docids));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(sort_result)
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
match self.index.main.sorted_document_ids_cache(reader)? {
|
|
|
|
// build result from cached document ids
|
|
|
|
Some(docids) => {
|
|
|
|
let mut sort_result = self.sort_result_from_docids(&docids, range);
|
|
|
|
|
|
|
|
if let Some(f) = self.facet_count_docids(reader)? {
|
|
|
|
sort_result.exhaustive_facets_count = Some(true);
|
|
|
|
// document ids are not sorted in natural order, we need to construct a new set
|
|
|
|
let document_set = SetBuf::from_dirty(Vec::from(docids));
|
|
|
|
sort_result.facets = Some(facet_count(f, &document_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(sort_result)
|
|
|
|
},
|
|
|
|
// no document id cached, return empty result
|
|
|
|
None => Ok(SortResult::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:59:46 +02:00
|
|
|
fn facet_count_docids<'a>(&self, reader: &'a MainReader) -> MResult<Option<HashMap<String, HashMap<String, (&'a str, Cow<'a, Set<DocumentId>>)>>>> {
|
2020-05-28 19:35:34 +02:00
|
|
|
match self.facets {
|
|
|
|
Some(ref field_ids) => {
|
|
|
|
let mut facet_count_map = HashMap::new();
|
|
|
|
for (field_id, field_name) in field_ids {
|
|
|
|
let mut key_map = HashMap::new();
|
|
|
|
for pair in self.index.facets.field_document_ids(reader, *field_id)? {
|
|
|
|
let (facet_key, document_ids) = pair?;
|
|
|
|
let value = facet_key.value();
|
|
|
|
key_map.insert(value.to_string(), document_ids);
|
|
|
|
}
|
|
|
|
facet_count_map.insert(field_name.clone(), key_map);
|
|
|
|
}
|
|
|
|
Ok(Some(facet_count_map))
|
|
|
|
}
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sort_result_from_docids(&self, docids: &[DocumentId], range: Range<usize>) -> SortResult {
|
|
|
|
let mut sort_result = SortResult::default();
|
2020-07-20 17:03:46 +02:00
|
|
|
let mut filtered_count = 0;
|
2020-05-28 19:35:34 +02:00
|
|
|
let mut result = match self.filter {
|
|
|
|
Some(ref filter) => docids
|
|
|
|
.iter()
|
2020-07-20 17:03:46 +02:00
|
|
|
.filter(|item| {
|
|
|
|
let accepted = (filter)(**item);
|
|
|
|
if !accepted {
|
|
|
|
filtered_count += 1;
|
|
|
|
}
|
|
|
|
accepted
|
|
|
|
})
|
2020-05-28 19:35:34 +02:00
|
|
|
.skip(range.start)
|
|
|
|
.take(range.end - range.start)
|
|
|
|
.map(|&id| Document::from_highlights(id, &[]))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
None => docids
|
|
|
|
.iter()
|
|
|
|
.skip(range.start)
|
|
|
|
.take(range.end - range.start)
|
|
|
|
.map(|&id| Document::from_highlights(id, &[]))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// distinct is set, remove duplicates with disctinct function
|
|
|
|
if let Some((distinct, distinct_size)) = &self.distinct {
|
|
|
|
let mut distinct_map = DistinctMap::new(*distinct_size);
|
|
|
|
let mut distinct_map = BufferedDistinctMap::new(&mut distinct_map);
|
|
|
|
result.retain(|doc| {
|
|
|
|
let id = doc.id;
|
|
|
|
let key = (distinct)(id);
|
2020-07-20 17:03:46 +02:00
|
|
|
let distinct_accepted = match key {
|
2020-05-28 19:35:34 +02:00
|
|
|
Some(key) => distinct_map.register(key),
|
|
|
|
None => distinct_map.register_without_key(),
|
2020-07-20 17:03:46 +02:00
|
|
|
};
|
|
|
|
if !distinct_accepted {
|
|
|
|
filtered_count += 1;
|
2020-05-28 19:35:34 +02:00
|
|
|
}
|
2020-07-20 17:03:46 +02:00
|
|
|
distinct_accepted
|
2020-05-28 19:35:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_result.documents = result;
|
2020-07-20 17:03:46 +02:00
|
|
|
sort_result.nb_hits = docids.len() - filtered_count;
|
2020-05-28 19:35:34 +02:00
|
|
|
sort_result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(
|
|
|
|
self,
|
|
|
|
reader: &heed::RoTxn<MainT>,
|
|
|
|
query: Option<&str>,
|
|
|
|
range: Range<usize>,
|
|
|
|
) -> MResult<SortResult> {
|
|
|
|
match query {
|
|
|
|
Some(query) => self.standard_query(reader, query, range),
|
|
|
|
None => self.placeholder_query(reader, range),
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 14:45:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 15:22:36 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use std::collections::{BTreeSet, HashMap};
|
|
|
|
use std::iter::FromIterator;
|
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
use fst::IntoStreamer;
|
2020-01-13 19:10:58 +01:00
|
|
|
use meilisearch_schema::IndexedPos;
|
2019-10-08 15:22:36 +02:00
|
|
|
use sdset::SetBuf;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2019-10-09 11:45:19 +02:00
|
|
|
use crate::automaton::normalize_str;
|
2019-12-13 11:14:12 +01:00
|
|
|
use crate::bucket_sort::SimpleMatch;
|
2020-05-28 19:35:34 +02:00
|
|
|
use crate::database::{Database, DatabaseOptions};
|
2019-10-08 15:22:36 +02:00
|
|
|
use crate::store::Index;
|
2020-05-28 19:35:34 +02:00
|
|
|
use crate::DocIndex;
|
|
|
|
use crate::Document;
|
2020-01-15 18:53:49 +01:00
|
|
|
use meilisearch_schema::Schema;
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
fn set_from_stream<'f, I, S>(stream: I) -> fst::Set<Vec<u8>>
|
2019-10-08 16:16:30 +02:00
|
|
|
where
|
2019-10-18 13:05:28 +02:00
|
|
|
I: for<'a> fst::IntoStreamer<'a, Into = S, Item = &'a [u8]>,
|
|
|
|
S: 'f + for<'a> fst::Streamer<'a, Item = &'a [u8]>,
|
2019-10-08 16:16:30 +02:00
|
|
|
{
|
|
|
|
let mut builder = fst::SetBuilder::memory();
|
|
|
|
builder.extend_stream(stream).unwrap();
|
2020-05-22 15:00:50 +02:00
|
|
|
builder.into_set()
|
2019-10-08 16:16:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
fn insert_key<A: AsRef<[u8]>>(set: &fst::Set<A>, key: &[u8]) -> fst::Set<Vec<u8>> {
|
2019-10-08 16:16:30 +02:00
|
|
|
let unique_key = {
|
|
|
|
let mut builder = fst::SetBuilder::memory();
|
|
|
|
builder.insert(key).unwrap();
|
2020-05-22 15:00:50 +02:00
|
|
|
builder.into_set()
|
2019-10-08 16:16:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let union_ = set.op().add(unique_key.into_stream()).r#union();
|
|
|
|
|
|
|
|
set_from_stream(union_)
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
fn sdset_into_fstset(set: &sdset::Set<&str>) -> fst::Set<Vec<u8>> {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut builder = fst::SetBuilder::memory();
|
|
|
|
let set = SetBuf::from_dirty(set.into_iter().map(|s| normalize_str(s)).collect());
|
|
|
|
builder.extend_iter(set.into_iter()).unwrap();
|
2020-05-22 15:00:50 +02:00
|
|
|
builder.into_set()
|
2019-10-08 16:16:30 +02:00
|
|
|
}
|
2019-10-08 15:22:36 +02:00
|
|
|
|
2020-05-19 13:53:31 +02:00
|
|
|
const fn doc_index(document_id: u32, word_index: u16) -> DocIndex {
|
2019-10-08 15:22:36 +02:00
|
|
|
DocIndex {
|
|
|
|
document_id: DocumentId(document_id),
|
|
|
|
attribute: 0,
|
|
|
|
word_index,
|
|
|
|
char_index: 0,
|
|
|
|
char_length: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:53:31 +02:00
|
|
|
const fn doc_char_index(document_id: u32, word_index: u16, char_index: u16) -> DocIndex {
|
2019-10-08 15:22:36 +02:00
|
|
|
DocIndex {
|
|
|
|
document_id: DocumentId(document_id),
|
|
|
|
attribute: 0,
|
|
|
|
word_index,
|
|
|
|
char_index,
|
|
|
|
char_length: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TempDatabase {
|
|
|
|
database: Database,
|
|
|
|
index: Index,
|
2019-10-08 17:31:07 +02:00
|
|
|
_tempdir: TempDir,
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TempDatabase {
|
|
|
|
pub fn query_builder(&self) -> QueryBuilder {
|
|
|
|
self.index.query_builder()
|
|
|
|
}
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
pub fn add_synonym(&mut self, word: &str, new: SetBuf<&str>) {
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &self.database;
|
|
|
|
let mut writer = db.main_write_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
let word = normalize_str(word);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
let alternatives = self
|
2019-10-18 13:05:28 +02:00
|
|
|
.index
|
|
|
|
.synonyms
|
2020-05-22 12:03:57 +02:00
|
|
|
.synonyms_fst(&writer, word.as_bytes())
|
2020-05-22 15:00:50 +02:00
|
|
|
.unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let new = sdset_into_fstset(&new);
|
2019-10-18 13:05:28 +02:00
|
|
|
let new_alternatives =
|
|
|
|
set_from_stream(alternatives.op().add(new.into_stream()).r#union());
|
|
|
|
self.index
|
|
|
|
.synonyms
|
|
|
|
.put_synonyms(&mut writer, word.as_bytes(), &new_alternatives)
|
|
|
|
.unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
let synonyms = self.index.main.synonyms_fst(&writer).unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let synonyms_fst = insert_key(&synonyms, word.as_bytes());
|
2019-10-18 13:05:28 +02:00
|
|
|
self.index
|
|
|
|
.main
|
|
|
|
.put_synonyms_fst(&mut writer, &synonyms_fst)
|
|
|
|
.unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
writer.commit().unwrap();
|
|
|
|
}
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromIterator<(&'a str, &'a [DocIndex])> for TempDatabase {
|
2019-10-18 13:05:28 +02:00
|
|
|
fn from_iter<I: IntoIterator<Item = (&'a str, &'a [DocIndex])>>(iter: I) -> Self {
|
2019-10-08 15:22:36 +02:00
|
|
|
let tempdir = TempDir::new().unwrap();
|
2020-05-07 15:40:44 +02:00
|
|
|
let database = Database::open_or_create(&tempdir, DatabaseOptions::default()).unwrap();
|
2019-10-10 13:38:58 +02:00
|
|
|
let index = database.create_index("default").unwrap();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &database;
|
|
|
|
let mut writer = db.main_write_txn().unwrap();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
|
|
|
let mut words_fst = BTreeSet::new();
|
|
|
|
let mut postings_lists = HashMap::new();
|
2019-11-27 17:01:23 +01:00
|
|
|
let mut fields_counts = HashMap::<_, u16>::new();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
2020-03-09 18:40:49 +01:00
|
|
|
let mut schema = Schema::with_primary_key("id");
|
2020-01-15 18:53:49 +01:00
|
|
|
|
2019-10-08 15:22:36 +02:00
|
|
|
for (word, indexes) in iter {
|
2020-01-15 18:53:49 +01:00
|
|
|
let mut final_indexes = Vec::new();
|
|
|
|
for index in indexes {
|
|
|
|
let name = index.attribute.to_string();
|
2020-02-11 15:16:02 +01:00
|
|
|
schema.insert(&name).unwrap();
|
2020-01-15 18:53:49 +01:00
|
|
|
let indexed_pos = schema.set_indexed(&name).unwrap().1;
|
|
|
|
let index = DocIndex {
|
|
|
|
attribute: indexed_pos.0,
|
|
|
|
..*index
|
|
|
|
};
|
|
|
|
final_indexes.push(index);
|
|
|
|
}
|
|
|
|
|
2019-10-08 15:22:36 +02:00
|
|
|
let word = word.to_lowercase().into_bytes();
|
|
|
|
words_fst.insert(word.clone());
|
2019-10-18 13:05:28 +02:00
|
|
|
postings_lists
|
|
|
|
.entry(word)
|
|
|
|
.or_insert_with(Vec::new)
|
2020-01-15 18:53:49 +01:00
|
|
|
.extend_from_slice(&final_indexes);
|
|
|
|
for idx in final_indexes {
|
2019-10-14 18:48:32 +02:00
|
|
|
fields_counts.insert((idx.document_id, idx.attribute, idx.word_index), 1);
|
|
|
|
}
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 18:53:49 +01:00
|
|
|
index.main.put_schema(&mut writer, &schema).unwrap();
|
|
|
|
|
2020-05-22 15:00:50 +02:00
|
|
|
let words_fst = fst::Set::from_iter(words_fst).unwrap();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
|
|
|
index.main.put_words_fst(&mut writer, &words_fst).unwrap();
|
|
|
|
|
|
|
|
for (word, postings_list) in postings_lists {
|
|
|
|
let postings_list = SetBuf::from_dirty(postings_list);
|
2019-10-18 13:05:28 +02:00
|
|
|
index
|
|
|
|
.postings_lists
|
|
|
|
.put_postings_list(&mut writer, &word, &postings_list)
|
|
|
|
.unwrap();
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 18:48:32 +02:00
|
|
|
for ((docid, attr, _), count) in fields_counts {
|
2019-10-18 13:05:28 +02:00
|
|
|
let prev = index
|
|
|
|
.documents_fields_counts
|
2020-02-02 22:59:19 +01:00
|
|
|
.document_field_count(&writer, docid, IndexedPos(attr))
|
2019-10-18 13:05:28 +02:00
|
|
|
.unwrap();
|
2019-10-14 18:48:32 +02:00
|
|
|
|
|
|
|
let prev = prev.unwrap_or(0);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
index
|
|
|
|
.documents_fields_counts
|
2020-01-13 19:10:58 +01:00
|
|
|
.put_document_field_count(&mut writer, docid, IndexedPos(attr), prev + count)
|
2019-10-18 13:05:28 +02:00
|
|
|
.unwrap();
|
2019-10-14 18:48:32 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 15:22:36 +02:00
|
|
|
writer.commit().unwrap();
|
|
|
|
|
2019-12-13 11:33:22 +01:00
|
|
|
TempDatabase { database, index, _tempdir: tempdir }
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple() {
|
|
|
|
let store = TempDatabase::from_iter(vec![
|
|
|
|
("iphone", &[doc_char_index(0, 0, 0)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("from", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("apple", &[doc_char_index(0, 2, 2)][..]),
|
2019-10-08 15:22:36 +02:00
|
|
|
]);
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("iphone from apple"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 15:22:36 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, .. }));
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, .. }));
|
2019-10-08 15:22:36 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
2019-10-08 16:16:30 +02:00
|
|
|
#[test]
|
|
|
|
fn simple_synonyms() {
|
2019-10-18 13:05:28 +02:00
|
|
|
let mut store = TempDatabase::from_iter(vec![("hello", &[doc_index(0, 0)][..])]);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
store.add_synonym("bonjour", SetBuf::from_dirty(vec!["hello"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("hello"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("bonjour"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// #[test]
|
|
|
|
// fn prefix_synonyms() {
|
|
|
|
// let mut store = TempDatabase::from_iter(vec![("hello", &[doc_index(0, 0)][..])]);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// store.add_synonym("bonjour", SetBuf::from_dirty(vec!["hello"]));
|
|
|
|
// store.add_synonym("salut", SetBuf::from_dirty(vec!["hello"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let db = &store.database;
|
|
|
|
// let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "sal", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
// let mut matches = matches.into_iter();
|
|
|
|
// assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
|
|
|
// assert_matches!(matches.next(), None);
|
|
|
|
// });
|
|
|
|
// assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "bonj", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
// let mut matches = matches.into_iter();
|
|
|
|
// assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
|
|
|
// assert_matches!(matches.next(), None);
|
|
|
|
// });
|
|
|
|
// assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "sal blabla", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "bonj blabla", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), None);
|
|
|
|
// }
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// #[test]
|
|
|
|
// fn levenshtein_synonyms() {
|
|
|
|
// let mut store = TempDatabase::from_iter(vec![("hello", &[doc_index(0, 0)][..])]);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// store.add_synonym("salutation", SetBuf::from_dirty(vec!["hello"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let db = &store.database;
|
|
|
|
// let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "salutution", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
// let mut matches = matches.into_iter();
|
|
|
|
// assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
|
|
|
// assert_matches!(matches.next(), None);
|
|
|
|
// });
|
|
|
|
// assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// let builder = store.query_builder();
|
|
|
|
// let results = builder.query(&reader, "saluttion", 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
// let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
// let mut matches = matches.into_iter();
|
|
|
|
// assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
|
|
|
// assert_matches!(matches.next(), None);
|
|
|
|
// });
|
|
|
|
// assert_matches!(iter.next(), None);
|
|
|
|
// }
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn harder_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("hello", &[doc_index(0, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("bonjour", &[doc_index(1, 3)]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("salut", &[doc_index(2, 5)]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
store.add_synonym("hello", SetBuf::from_dirty(vec!["bonjour", "salut"]));
|
|
|
|
store.add_synonym("bonjour", SetBuf::from_dirty(vec!["hello", "salut"]));
|
|
|
|
store.add_synonym("salut", SetBuf::from_dirty(vec!["hello", "bonjour"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("hello"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 3, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 5, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("bonjour"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 3, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 5, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("salut"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 3, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 5, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Unique word has multi-word synonyms
|
|
|
|
fn unique_to_multiword_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_char_index(0, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("city", &[doc_char_index(0, 2, 2)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(0, 3, 3)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_char_index(1, 0, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(1, 1, 1)][..]),
|
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym(
|
|
|
|
"NY",
|
|
|
|
SetBuf::from_dirty(vec!["NYC", "new york", "new york city"]),
|
|
|
|
);
|
|
|
|
store.add_synonym(
|
|
|
|
"NYC",
|
|
|
|
SetBuf::from_dirty(vec!["NY", "new york", "new york city"]),
|
|
|
|
);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NY subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // subway
|
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // NY ± new
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // NY ± york
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // NY ± city
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // subway
|
|
|
|
assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NYC subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // subway
|
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // NYC ± new
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // NYC ± york
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // NYC ± city
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // subway
|
|
|
|
assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unique_to_multiword_synonyms_words_proximity() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_char_index(0, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("city", &[doc_char_index(0, 2, 2)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(0, 3, 3)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("york", &[doc_char_index(1, 0, 0)][..]),
|
|
|
|
("new", &[doc_char_index(1, 1, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(1, 2, 2)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_char_index(2, 0, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(2, 1, 1)][..]),
|
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym("NY", SetBuf::from_dirty(vec!["york new"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NY"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. })); // NY ± york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, .. })); // NY ± new
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. })); // york = NY
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, .. })); // new = NY
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 1, .. })); // york = NY
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 0, .. })); // new = NY
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("new york"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, .. })); // york
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None); // position rewritten ^
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 1, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 0, .. })); // new
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unique_to_multiword_synonyms_cumulative_word_index() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_char_index(0, 0, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(0, 1, 1)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_char_index(1, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(1, 1, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_char_index(1, 2, 2)][..]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
store.add_synonym("new york", SetBuf::from_dirty(vec!["NY"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NY subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // NY
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
// let mut matches = matches.into_iter();
|
|
|
|
// assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 2, is_exact: true, .. })); // subway
|
|
|
|
// assert_matches!(matches.next(), None);
|
|
|
|
// });
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } =
|
|
|
|
builder.query(&reader, Some("new york subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NY
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NY
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Unique word has multi-word synonyms
|
|
|
|
fn harder_unique_to_multiword_synonyms_one() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_char_index(0, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("city", &[doc_char_index(0, 2, 2)][..]),
|
|
|
|
("yellow", &[doc_char_index(0, 3, 3)][..]),
|
|
|
|
("subway", &[doc_char_index(0, 4, 4)][..]),
|
|
|
|
("broken", &[doc_char_index(0, 5, 5)][..]),
|
|
|
|
("NY", &[doc_char_index(1, 0, 0)][..]),
|
|
|
|
("blue", &[doc_char_index(1, 1, 1)][..]),
|
|
|
|
("subway", &[doc_char_index(1, 2, 2)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym(
|
|
|
|
"NY",
|
|
|
|
SetBuf::from_dirty(vec!["NYC", "new york", "new york city"]),
|
|
|
|
);
|
|
|
|
store.add_synonym(
|
|
|
|
"NYC",
|
|
|
|
SetBuf::from_dirty(vec!["NY", "new york", "new york city"]),
|
|
|
|
);
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NY subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city = NY
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // subway
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city = NY
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // subway
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NYC subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // NYC
|
|
|
|
// because one-word to one-word ^^^^
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // subway
|
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Unique word has multi-word synonyms
|
|
|
|
fn even_harder_unique_to_multiword_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_char_index(0, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("city", &[doc_char_index(0, 2, 2)][..]),
|
|
|
|
("yellow", &[doc_char_index(0, 3, 3)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("underground", &[doc_char_index(0, 4, 4)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("train", &[doc_char_index(0, 5, 5)][..]),
|
|
|
|
("broken", &[doc_char_index(0, 6, 6)][..]),
|
|
|
|
("NY", &[doc_char_index(1, 0, 0)][..]),
|
|
|
|
("blue", &[doc_char_index(1, 1, 1)][..]),
|
|
|
|
("subway", &[doc_char_index(1, 2, 2)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym(
|
|
|
|
"NY",
|
|
|
|
SetBuf::from_dirty(vec!["NYC", "new york", "new york city"]),
|
|
|
|
);
|
|
|
|
store.add_synonym(
|
|
|
|
"NYC",
|
|
|
|
SetBuf::from_dirty(vec!["NY", "new york", "new york city"]),
|
|
|
|
);
|
2019-10-08 16:16:30 +02:00
|
|
|
store.add_synonym("subway", SetBuf::from_dirty(vec!["underground train"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult {documents, .. } = builder.query(&reader, Some("NY subway broken"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city = NY
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: false, .. })); // underground = subway
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 4, word_index: 5, is_exact: false, .. })); // train = subway
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 5, word_index: 6, is_exact: true, .. })); // broken
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NYC subway"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // underground = subway
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 4, word_index: 5, is_exact: true, .. })); // train = subway
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), None); // position rewritten ^
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york = NYC
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city = NYC
|
2020-01-22 18:12:56 +01:00
|
|
|
// because one-word to one-word ^^^^
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: false, .. })); // subway = underground
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 4, word_index: 5, is_exact: false, .. })); // subway = train
|
|
|
|
assert_matches!(iter.next(), None);
|
2019-10-08 16:16:30 +02:00
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Multi-word has multi-word synonyms
|
|
|
|
fn multiword_to_multiword_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_char_index(0, 0, 0)][..]),
|
|
|
|
("subway", &[doc_char_index(0, 1, 1)][..]),
|
|
|
|
("NYC", &[doc_char_index(1, 0, 0)][..]),
|
|
|
|
("blue", &[doc_char_index(1, 1, 1)][..]),
|
|
|
|
("subway", &[doc_char_index(1, 2, 2)][..]),
|
|
|
|
("broken", &[doc_char_index(1, 3, 3)][..]),
|
|
|
|
("new", &[doc_char_index(2, 0, 0)][..]),
|
|
|
|
("york", &[doc_char_index(2, 1, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("underground", &[doc_char_index(2, 2, 2)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("train", &[doc_char_index(2, 3, 3)][..]),
|
|
|
|
("broken", &[doc_char_index(2, 4, 4)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym(
|
|
|
|
"new york",
|
|
|
|
SetBuf::from_dirty(vec!["NYC", "NY", "new york city"]),
|
|
|
|
);
|
|
|
|
store.add_synonym(
|
|
|
|
"new york city",
|
|
|
|
SetBuf::from_dirty(vec!["NYC", "NY", "new york"]),
|
|
|
|
);
|
|
|
|
store.add_synonym("underground train", SetBuf::from_dirty(vec!["subway"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder
|
|
|
|
.query(&reader, Some("new york underground train broken"), 0..20)
|
2019-10-18 13:05:28 +02:00
|
|
|
.unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // underground
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 4, is_exact: true, .. })); // train
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 5, word_index: 5, is_exact: true, .. })); // broken
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // NYC = new
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // NYC = york
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // NYC = city
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 4, is_exact: true, .. })); // subway = underground
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 4, word_index: 5, is_exact: true, .. })); // subway = train
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 5, word_index: 6, is_exact: true, .. })); // broken
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder
|
|
|
|
.query(&reader, Some("new york city underground train broken"), 0..20)
|
2019-10-18 13:05:28 +02:00
|
|
|
.unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 3, word_index: 2, is_exact: true, .. })); // underground
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 3, is_exact: true, .. })); // train
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 5, word_index: 4, is_exact: true, .. })); // broken
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // NYC = new
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // NYC = york
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // subway = underground
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 4, word_index: 4, is_exact: true, .. })); // subway = train
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 5, word_index: 5, is_exact: true, .. })); // broken
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn intercrossed_multiword_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("new", &[doc_index(0, 0)][..]),
|
|
|
|
("york", &[doc_index(0, 1)][..]),
|
|
|
|
("big", &[doc_index(0, 2)][..]),
|
|
|
|
("city", &[doc_index(0, 3)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym("new york", SetBuf::from_dirty(vec!["new york city"]));
|
|
|
|
store.add_synonym("new york city", SetBuf::from_dirty(vec!["new york"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("new york big "), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: false, .. })); // new
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 4, is_exact: false, .. })); // city
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // big
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_index(0, 0)][..]),
|
|
|
|
("city", &[doc_index(0, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_index(0, 2)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_index(1, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_index(1, 1)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("NY", &[doc_index(2, 0)][..]),
|
|
|
|
("york", &[doc_index(2, 1)][..]),
|
|
|
|
("city", &[doc_index(2, 2)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_index(2, 3)][..]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
store.add_synonym("NY", SetBuf::from_dirty(vec!["new york city story"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("NY subway "), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 3, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: false, .. })); // york
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
2020-01-22 18:12:56 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: false, .. })); // city
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 3, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
2019-10-08 16:16:30 +02:00
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // story
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 4, is_exact: true, .. })); // subway
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cumulative_word_indices() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("NYC", &[doc_index(0, 0)][..]),
|
|
|
|
("long", &[doc_index(0, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
("subway", &[doc_index(0, 2)][..]),
|
2019-10-18 13:05:28 +02:00
|
|
|
("cool", &[doc_index(0, 3)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
store.add_synonym("new york city", SetBuf::from_dirty(vec!["NYC"]));
|
2019-10-18 13:05:28 +02:00
|
|
|
store.add_synonym("subway", SetBuf::from_dirty(vec!["underground train"]));
|
2019-10-08 16:16:30 +02:00
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder
|
|
|
|
.query(&reader, Some("new york city long subway cool "), 0..20)
|
2019-10-18 13:05:28 +02:00
|
|
|
.unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut matches = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 0, word_index: 0, is_exact: true, .. })); // new = NYC
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 1, word_index: 1, is_exact: true, .. })); // york = NYC
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 2, word_index: 2, is_exact: true, .. })); // city = NYC
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 3, word_index: 3, is_exact: true, .. })); // long
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 4, word_index: 4, is_exact: true, .. })); // subway = underground
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 5, word_index: 5, is_exact: true, .. })); // subway = train
|
|
|
|
assert_matches!(matches.next(), Some(SimpleMatch { query_index: 6, word_index: 6, is_exact: true, .. })); // cool
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(matches.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deunicoded_synonyms() {
|
|
|
|
let mut store = TempDatabase::from_iter(vec![
|
2019-11-26 11:06:55 +01:00
|
|
|
("telephone", &[doc_index(0, 0)][..]), // meilisearch indexes the unidecoded
|
2019-10-14 18:48:32 +02:00
|
|
|
("téléphone", &[doc_index(0, 0)][..]), // and the original words on the same DocIndex
|
2019-10-18 13:05:28 +02:00
|
|
|
("iphone", &[doc_index(1, 0)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
store.add_synonym("téléphone", SetBuf::from_dirty(vec!["iphone"]));
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("telephone"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("téléphone"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("télephone"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, .. }));
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, distance: 1, word_index: 0, is_exact: false, .. })); // iphone | telephone
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_concatenation() {
|
|
|
|
let store = TempDatabase::from_iter(vec![
|
2019-10-18 13:05:28 +02:00
|
|
|
("iphone", &[doc_index(0, 0)][..]),
|
|
|
|
("case", &[doc_index(0, 1)][..]),
|
2019-10-08 16:16:30 +02:00
|
|
|
]);
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("i phone case"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-08 16:16:30 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 0, .. })); // iphone
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 1, distance: 0, .. })); // iphone
|
2020-01-22 18:12:56 +01:00
|
|
|
// assert_matches!(iter.next(), Some(SimpleMatch { query_index: 1, word_index: 0, distance: 1, .. })); "phone"
|
|
|
|
// but no typo on first letter ^^^^^^^
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 2, word_index: 2, distance: 0, .. })); // case
|
2019-10-08 16:16:30 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
2019-10-22 18:15:43 +02:00
|
|
|
|
2019-12-13 11:33:22 +01:00
|
|
|
#[test]
|
|
|
|
fn exact_field_count_one_word() {
|
|
|
|
let store = TempDatabase::from_iter(vec![
|
|
|
|
("searchengine", &[doc_index(0, 0)][..]),
|
|
|
|
("searchengine", &[doc_index(1, 0)][..]),
|
|
|
|
("blue", &[doc_index(1, 1)][..]),
|
|
|
|
("searchangine", &[doc_index(2, 0)][..]),
|
|
|
|
("searchengine", &[doc_index(3, 0)][..]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("searchengine"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-12-13 11:33:22 +01:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 0, .. })); // searchengine
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(3), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 0, .. })); // searchengine
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 0, .. })); // searchengine
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(2), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 1, .. })); // searchengine
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
2019-10-22 18:15:43 +02:00
|
|
|
#[test]
|
|
|
|
fn simple_phrase_query_splitting() {
|
|
|
|
let store = TempDatabase::from_iter(vec![
|
|
|
|
("search", &[doc_index(0, 0)][..]),
|
|
|
|
("engine", &[doc_index(0, 1)][..]),
|
|
|
|
("search", &[doc_index(1, 0)][..]),
|
2019-10-23 12:06:21 +02:00
|
|
|
("slow", &[doc_index(1, 1)][..]),
|
2019-10-22 18:15:43 +02:00
|
|
|
("engine", &[doc_index(1, 2)][..]),
|
|
|
|
]);
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-22 18:15:43 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("searchengine"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-22 18:15:43 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 0, distance: 0, .. })); // search
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 1, distance: 0, .. })); // engine
|
2019-10-22 18:15:43 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn harder_phrase_query_splitting() {
|
|
|
|
let store = TempDatabase::from_iter(vec![
|
|
|
|
("search", &[doc_index(0, 0)][..]),
|
|
|
|
("search", &[doc_index(0, 1)][..]),
|
|
|
|
("engine", &[doc_index(0, 2)][..]),
|
|
|
|
("search", &[doc_index(1, 0)][..]),
|
2019-10-23 12:06:21 +02:00
|
|
|
("slow", &[doc_index(1, 1)][..]),
|
2019-10-22 18:15:43 +02:00
|
|
|
("search", &[doc_index(1, 2)][..]),
|
|
|
|
("engine", &[doc_index(1, 3)][..]),
|
|
|
|
("search", &[doc_index(1, 0)][..]),
|
|
|
|
("search", &[doc_index(1, 1)][..]),
|
2019-10-23 12:06:21 +02:00
|
|
|
("slow", &[doc_index(1, 2)][..]),
|
2019-10-22 18:15:43 +02:00
|
|
|
("engine", &[doc_index(1, 3)][..]),
|
|
|
|
]);
|
|
|
|
|
2019-11-26 16:12:06 +01:00
|
|
|
let db = &store.database;
|
|
|
|
let reader = db.main_read_txn().unwrap();
|
2019-10-22 18:15:43 +02:00
|
|
|
|
|
|
|
let builder = store.query_builder();
|
2020-05-28 19:35:34 +02:00
|
|
|
let SortResult { documents, .. } = builder.query(&reader, Some("searchengine"), 0..20).unwrap();
|
2020-05-12 12:37:16 +02:00
|
|
|
let mut iter = documents.into_iter();
|
2019-10-22 18:15:43 +02:00
|
|
|
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 1, distance: 0, .. })); // search
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 2, distance: 0, .. })); // engine
|
2019-10-22 18:15:43 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(1), matches, .. }) => {
|
|
|
|
let mut iter = matches.into_iter();
|
2019-12-13 11:14:12 +01:00
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 2, distance: 0, .. })); // search
|
|
|
|
assert_matches!(iter.next(), Some(SimpleMatch { query_index: 0, word_index: 3, distance: 0, .. })); // engine
|
2019-10-22 18:15:43 +02:00
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
});
|
|
|
|
assert_matches!(iter.next(), None);
|
|
|
|
}
|
2019-10-08 15:22:36 +02:00
|
|
|
}
|