mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-01-11 05:54:30 +01:00
fix bug
This commit is contained in:
parent
1639a7338d
commit
dc2e5ceed2
@ -246,7 +246,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_facet_key() {
|
fn test_facet_key() {
|
||||||
let mut schema = Schema::new();
|
let mut schema = Schema::new();
|
||||||
let id = schema.insert_and_index("hello").unwrap();
|
let id = schema.register_field("hello").unwrap();
|
||||||
let facet_list = [schema.id("hello").unwrap()];
|
let facet_list = [schema.id("hello").unwrap()];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
FacetKey::from_str("hello:12", &schema, &facet_list).unwrap(),
|
FacetKey::from_str("hello:12", &schema, &facet_list).unwrap(),
|
||||||
@ -287,7 +287,7 @@ mod test {
|
|||||||
fn test_parse_facet_array() {
|
fn test_parse_facet_array() {
|
||||||
use either::Either::{Left, Right};
|
use either::Either::{Left, Right};
|
||||||
let mut schema = Schema::new();
|
let mut schema = Schema::new();
|
||||||
let _id = schema.insert_and_index("hello").unwrap();
|
let _id = schema.register_field("hello").unwrap();
|
||||||
let facet_list = [schema.id("hello").unwrap()];
|
let facet_list = [schema.id("hello").unwrap()];
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
FacetFilter::from_str("[[\"hello:12\"]]", &schema, &facet_list).unwrap(),
|
FacetFilter::from_str("[[\"hello:12\"]]", &schema, &facet_list).unwrap(),
|
||||||
|
@ -94,6 +94,8 @@ impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let is_displayed = self.schema.is_displayed(attr);
|
let is_displayed = self.schema.is_displayed(attr);
|
||||||
|
// Check if displayed fields were requested, if yes, return only displayed fields,
|
||||||
|
// else return all fields
|
||||||
if !self.displayed || (is_displayed && self.fields.map_or(true, |f| f.contains(&attr))) {
|
if !self.displayed || (is_displayed && self.fields.map_or(true, |f| f.contains(&attr))) {
|
||||||
if let Some(attribute_name) = self.schema.name(attr) {
|
if let Some(attribute_name) = self.schema.name(attr) {
|
||||||
let cursor = Cursor::new(value.to_owned());
|
let cursor = Cursor::new(value.to_owned());
|
||||||
|
@ -201,14 +201,12 @@ pub fn apply_addition<'a, 'b>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let old_document = Option::<HashMap<String, Value>>::deserialize(&mut deserializer)?;
|
let old_document = Option::<HashMap<String, Value>>::deserialize(&mut deserializer)?;
|
||||||
println!("old document: {:#?}", old_document);
|
|
||||||
if let Some(old_document) = old_document {
|
if let Some(old_document) = old_document {
|
||||||
for (key, value) in old_document {
|
for (key, value) in old_document {
|
||||||
document.entry(key).or_insert(value);
|
document.entry(key).or_insert(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("new document: {:#?}", document);
|
|
||||||
documents_additions.insert(internal_docid, document);
|
documents_additions.insert(internal_docid, document);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,7 +229,7 @@ pub fn apply_addition<'a, 'b>(
|
|||||||
for (document_id, document) in &documents_additions {
|
for (document_id, document) in &documents_additions {
|
||||||
// For each key-value pair in the document.
|
// For each key-value pair in the document.
|
||||||
for (attribute, value) in document {
|
for (attribute, value) in document {
|
||||||
let field_id = schema.insert_and_index(&attribute)?;
|
let field_id = schema.register_field(&attribute)?;
|
||||||
index_document(
|
index_document(
|
||||||
writer,
|
writer,
|
||||||
index.documents_fields,
|
index.documents_fields,
|
||||||
|
@ -20,7 +20,6 @@ pub fn index_value<A>(
|
|||||||
) -> Option<usize>
|
) -> Option<usize>
|
||||||
where A: AsRef<[u8]>,
|
where A: AsRef<[u8]>,
|
||||||
{
|
{
|
||||||
println!("indexing value: {}", value);
|
|
||||||
match value {
|
match value {
|
||||||
Value::Null => None,
|
Value::Null => None,
|
||||||
Value::Bool(boolean) => {
|
Value::Bool(boolean) => {
|
||||||
|
@ -70,11 +70,9 @@ pub fn apply_settings_update(
|
|||||||
|
|
||||||
match settings.searchable_attributes.clone() {
|
match settings.searchable_attributes.clone() {
|
||||||
UpdateState::Update(v) => {
|
UpdateState::Update(v) => {
|
||||||
println!("updating indexed attributes");
|
|
||||||
if v.iter().any(|e| e == "*") || v.is_empty() {
|
if v.iter().any(|e| e == "*") || v.is_empty() {
|
||||||
schema.set_all_fields_as_indexed();
|
schema.set_all_fields_as_indexed();
|
||||||
} else {
|
} else {
|
||||||
println!("updating indexed");
|
|
||||||
schema.update_indexed(v)?;
|
schema.update_indexed(v)?;
|
||||||
}
|
}
|
||||||
must_reindex = true;
|
must_reindex = true;
|
||||||
|
@ -1779,7 +1779,7 @@ async fn update_documents_with_facet_distribution() {
|
|||||||
server.create_index(body).await;
|
server.create_index(body).await;
|
||||||
let settings = json!({
|
let settings = json!({
|
||||||
"attributesForFaceting": ["genre"],
|
"attributesForFaceting": ["genre"],
|
||||||
"displayedAttributes": ["genre", "type"],
|
"displayedAttributes": ["genre"],
|
||||||
"searchableAttributes": ["genre", "type"]
|
"searchableAttributes": ["genre", "type"]
|
||||||
});
|
});
|
||||||
server.update_all_settings(settings).await;
|
server.update_all_settings(settings).await;
|
||||||
@ -1809,7 +1809,6 @@ async fn update_documents_with_facet_distribution() {
|
|||||||
"facetsDistribution": ["genre"]
|
"facetsDistribution": ["genre"]
|
||||||
});
|
});
|
||||||
let (response1, _) = server.search_post(search.clone()).await;
|
let (response1, _) = server.search_post(search.clone()).await;
|
||||||
println!("response1: {}", response1);
|
|
||||||
let expected_facet_distribution = json!({
|
let expected_facet_distribution = json!({
|
||||||
"genre": {
|
"genre": {
|
||||||
"grunge": 1,
|
"grunge": 1,
|
||||||
@ -1828,6 +1827,5 @@ async fn update_documents_with_facet_distribution() {
|
|||||||
]);
|
]);
|
||||||
server.add_or_update_multiple_documents(update2).await;
|
server.add_or_update_multiple_documents(update2).await;
|
||||||
let (response2, _) = server.search_post(search).await;
|
let (response2, _) = server.search_post(search).await;
|
||||||
println!("response2: {}", response2);
|
|
||||||
assert_json_eq!(expected_facet_distribution, response2["facetsDistribution"].clone());
|
assert_json_eq!(expected_facet_distribution, response2["facetsDistribution"].clone());
|
||||||
}
|
}
|
||||||
|
@ -486,15 +486,7 @@ async fn test_displayed_attributes_field() {
|
|||||||
],
|
],
|
||||||
"distinctAttribute": "id",
|
"distinctAttribute": "id",
|
||||||
"searchableAttributes": [
|
"searchableAttributes": [
|
||||||
"id",
|
"*"
|
||||||
"name",
|
|
||||||
"color",
|
|
||||||
"gender",
|
|
||||||
"email",
|
|
||||||
"phone",
|
|
||||||
"address",
|
|
||||||
"registered",
|
|
||||||
"about"
|
|
||||||
],
|
],
|
||||||
"displayedAttributes": [
|
"displayedAttributes": [
|
||||||
"age",
|
"age",
|
||||||
|
@ -16,14 +16,6 @@ impl<T> OptionAll<T> {
|
|||||||
std::mem::replace(self, OptionAll::None)
|
std::mem::replace(self, OptionAll::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map<U, F: FnOnce(T) -> U>(self, f: F) -> OptionAll<U> {
|
|
||||||
match self {
|
|
||||||
OptionAll::Some(x) => OptionAll::Some(f(x)),
|
|
||||||
OptionAll::All => OptionAll::All,
|
|
||||||
OptionAll::None => OptionAll::None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_all(&self) -> bool {
|
pub fn is_all(&self) -> bool {
|
||||||
matches!(self, OptionAll::All)
|
matches!(self, OptionAll::All)
|
||||||
}
|
}
|
||||||
@ -44,7 +36,7 @@ pub struct Schema {
|
|||||||
displayed: OptionAll<HashSet<FieldId>>,
|
displayed: OptionAll<HashSet<FieldId>>,
|
||||||
|
|
||||||
indexed: OptionAll<HashSet<FieldId>>,
|
indexed: OptionAll<HashSet<FieldId>>,
|
||||||
indexed_map: HashMap<FieldId, IndexedPos>,
|
fields_position: HashMap<FieldId, IndexedPos>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Schema {
|
impl Schema {
|
||||||
@ -68,7 +60,7 @@ impl Schema {
|
|||||||
ranked: HashSet::new(),
|
ranked: HashSet::new(),
|
||||||
displayed: OptionAll::All,
|
displayed: OptionAll::All,
|
||||||
indexed: OptionAll::All,
|
indexed: OptionAll::All,
|
||||||
indexed_map,
|
fields_position: indexed_map,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,15 +101,15 @@ impl Schema {
|
|||||||
self.fields_map.insert(name)
|
self.fields_map.insert(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_and_index(&mut self, name: &str) -> SResult<FieldId> {
|
pub fn register_field(&mut self, name: &str) -> SResult<FieldId> {
|
||||||
match self.fields_map.id(name) {
|
match self.fields_map.id(name) {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let id = self.fields_map.insert(name)?;
|
let id = self.fields_map.insert(name)?;
|
||||||
let pos = self.indexed_map.len() as u16;
|
let pos = self.fields_position.len() as u16;
|
||||||
self.indexed_map.insert(id, pos.into());
|
self.fields_position.insert(id, pos.into());
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -163,9 +155,9 @@ impl Schema {
|
|||||||
OptionAll::Some(ref v) => v.iter().cloned().collect(),
|
OptionAll::Some(ref v) => v.iter().cloned().collect(),
|
||||||
OptionAll::All => {
|
OptionAll::All => {
|
||||||
let fields = self
|
let fields = self
|
||||||
.indexed_map
|
.fields_position
|
||||||
.iter()
|
.keys()
|
||||||
.map(|(&f, _)| f)
|
.cloned()
|
||||||
.collect();
|
.collect();
|
||||||
fields
|
fields
|
||||||
},
|
},
|
||||||
@ -202,18 +194,16 @@ impl Schema {
|
|||||||
|
|
||||||
pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
|
pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
|
||||||
let id = self.fields_map.insert(name)?;
|
let id = self.fields_map.insert(name)?;
|
||||||
println!("setting {} indexed", name);
|
|
||||||
|
|
||||||
self.indexed = self.indexed.take().map(|mut v| {
|
if let OptionAll::Some(ref mut v) = self.indexed {
|
||||||
v.insert(id);
|
v.insert(id);
|
||||||
v
|
}
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(indexed_pos) = self.indexed_map.get(&id) {
|
if let Some(indexed_pos) = self.fields_position.get(&id) {
|
||||||
return Ok((id, *indexed_pos))
|
return Ok((id, *indexed_pos))
|
||||||
};
|
};
|
||||||
let pos = self.indexed_map.len() as u16;
|
let pos = self.fields_position.len() as u16;
|
||||||
self.indexed_map.insert(id, pos.into());
|
self.fields_position.insert(id, pos.into());
|
||||||
Ok((id, pos.into()))
|
Ok((id, pos.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,56 +217,6 @@ impl Schema {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// remove field from displayed attributes. If diplayed attributes is OptionAll::All,
|
|
||||||
/// dipslayed attributes is turned into OptionAll::Some(v) where v is all displayed attributes
|
|
||||||
/// except name.
|
|
||||||
pub fn remove_displayed(&mut self, name: &str) {
|
|
||||||
if let Some(id) = self.fields_map.id(name) {
|
|
||||||
self.displayed = match self.displayed.take() {
|
|
||||||
OptionAll::Some(mut v) => {
|
|
||||||
v.remove(&id);
|
|
||||||
OptionAll::Some(v)
|
|
||||||
}
|
|
||||||
OptionAll::All => {
|
|
||||||
let displayed = self.fields_map
|
|
||||||
.iter()
|
|
||||||
.filter_map(|(key, &value)| {
|
|
||||||
if key != name {
|
|
||||||
Some(value)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect::<HashSet<_>>();
|
|
||||||
OptionAll::Some(displayed)
|
|
||||||
}
|
|
||||||
OptionAll::None => OptionAll::None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_indexed(&mut self, name: &str) {
|
|
||||||
if let Some(id) = self.fields_map.id(name) {
|
|
||||||
self.indexed_map.remove(&id);
|
|
||||||
self.indexed = match self.indexed.take() {
|
|
||||||
// valid because indexed is All and indexed() return the content of
|
|
||||||
// indexed_map that is already updated
|
|
||||||
OptionAll::All => {
|
|
||||||
let indexed = self.indexed_map
|
|
||||||
.iter()
|
|
||||||
.filter_map(|(&i, _)| if i != id { Some(i) } else { None })
|
|
||||||
.collect();
|
|
||||||
OptionAll::Some(indexed)
|
|
||||||
},
|
|
||||||
OptionAll::Some(mut v) => {
|
|
||||||
v.retain(|x| *x != id);
|
|
||||||
OptionAll::Some(v)
|
|
||||||
}
|
|
||||||
OptionAll::None => OptionAll::None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_ranked(&self, id: FieldId) -> bool {
|
pub fn is_ranked(&self, id: FieldId) -> bool {
|
||||||
self.ranked.get(&id).is_some()
|
self.ranked.get(&id).is_some()
|
||||||
}
|
}
|
||||||
@ -291,10 +231,10 @@ impl Schema {
|
|||||||
|
|
||||||
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
|
pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
|
||||||
match self.indexed {
|
match self.indexed {
|
||||||
OptionAll::All => self.indexed_map.get(&id),
|
OptionAll::All => self.fields_position.get(&id),
|
||||||
OptionAll::Some(ref v) => {
|
OptionAll::Some(ref v) => {
|
||||||
if v.contains(&id) {
|
if v.contains(&id) {
|
||||||
self.indexed_map.get(&id)
|
self.fields_position.get(&id)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@ -310,7 +250,7 @@ impl Schema {
|
|||||||
pub fn indexed_pos_to_field_id<I: Into<IndexedPos>>(&self, pos: I) -> Option<FieldId> {
|
pub fn indexed_pos_to_field_id<I: Into<IndexedPos>>(&self, pos: I) -> Option<FieldId> {
|
||||||
let indexed_pos = pos.into().0;
|
let indexed_pos = pos.into().0;
|
||||||
self
|
self
|
||||||
.indexed_map
|
.fields_position
|
||||||
.iter()
|
.iter()
|
||||||
.find(|(_, &v)| v.0 == indexed_pos)
|
.find(|(_, &v)| v.0 == indexed_pos)
|
||||||
.map(|(&k, _)| k)
|
.map(|(&k, _)| k)
|
||||||
@ -354,11 +294,11 @@ impl Schema {
|
|||||||
|
|
||||||
pub fn set_all_fields_as_indexed(&mut self) {
|
pub fn set_all_fields_as_indexed(&mut self) {
|
||||||
self.indexed = OptionAll::All;
|
self.indexed = OptionAll::All;
|
||||||
self.indexed_map.clear();
|
self.fields_position.clear();
|
||||||
|
|
||||||
for (_name, id) in self.fields_map.iter() {
|
for (_name, id) in self.fields_map.iter() {
|
||||||
let pos = self.indexed_map.len() as u16;
|
let pos = self.fields_position.len() as u16;
|
||||||
self.indexed_map.insert(*id, pos.into());
|
self.fields_position.insert(*id, pos.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user