fix for review

This commit is contained in:
qdequele 2020-01-29 18:30:21 +01:00
parent 14b5fc4d6c
commit a5b0e468ee
No known key found for this signature in database
GPG Key ID: B3F0A000EBF11745
48 changed files with 558 additions and 1216 deletions

8
Cargo.lock generated
View File

@ -1037,6 +1037,7 @@ dependencies = [
"main_error 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "main_error 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"meilisearch-core 0.8.4", "meilisearch-core 0.8.4",
"meilisearch-schema 0.8.4", "meilisearch-schema 0.8.4",
"mime 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
"pretty-bytes 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "pretty-bytes 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1047,7 +1048,7 @@ dependencies = [
"structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"sysinfo 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)", "sysinfo 0.9.6 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"tide 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "tide 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ureq 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)", "ureq 0.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
"vergen 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "vergen 3.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1870,10 +1871,11 @@ dependencies = [
[[package]] [[package]]
name = "tide" name = "tide"
version = "0.5.1" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"async-std 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "async-std 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cookie 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"http 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)",
"http-service 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "http-service 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -2563,7 +2565,7 @@ dependencies = [
"checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum tide 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13c99b1991db81e611a2614cd1b07fec89ae33c5f755e1f8eb70826fb5af0eea" "checksum tide 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e619c99048ae107912703d0efeec4ff4fbff704f064e51d3eee614b28ea7b739"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4574b75faccaacddb9b284faecdf0b544b80b6b294f3d062d325c5726a209c20" "checksum tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4574b75faccaacddb9b284faecdf0b544b80b6b294f3d062d325c5726a209c20"
"checksum tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" "checksum tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6"

View File

@ -1,7 +1,7 @@
{ {
"attribute_identifier": "id", "identifier": "id",
"attributes_searchable": ["title", "overview"], "searchable_attributes": ["title", "overview"],
"attributes_displayed": [ "displayed_attributes": [
"id", "id",
"title", "title",
"overview", "overview",

View File

@ -32,7 +32,7 @@ serde_json = "1.0.41"
siphasher = "0.3.1" siphasher = "0.3.1"
slice-group-by = "0.2.6" slice-group-by = "0.2.6"
zerocopy = "0.2.8" zerocopy = "0.2.8"
regex = "1" regex = "1.3.1"
[dev-dependencies] [dev-dependencies]
assert_matches = "1.3" assert_matches = "1.3"

View File

@ -123,7 +123,7 @@ fn index_command(command: IndexCommand, database: Database) -> Result<(), Box<dy
let settings = { let settings = {
let string = fs::read_to_string(&command.settings)?; let string = fs::read_to_string(&command.settings)?;
let settings: Settings = serde_json::from_str(&string).unwrap(); let settings: Settings = serde_json::from_str(&string).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut update_writer = db.update_write_txn().unwrap(); let mut update_writer = db.update_write_txn().unwrap();
@ -359,7 +359,7 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<
}; };
let attr = schema let attr = schema
.get_id(filter) .id(filter)
.expect("Could not find filtered attribute"); .expect("Could not find filtered attribute");
builder.with_filter(move |document_id| { builder.with_filter(move |document_id| {
@ -390,7 +390,7 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<
for (name, text) in document.0 { for (name, text) in document.0 {
print!("{}: ", name); print!("{}: ", name);
let attr = schema.get_id(&name).unwrap(); let attr = schema.id(&name).unwrap();
let highlights = doc let highlights = doc
.highlights .highlights
.iter() .iter()
@ -410,7 +410,7 @@ fn search_command(command: SearchCommand, database: Database) -> Result<(), Box<
let mut matching_attributes = HashSet::new(); let mut matching_attributes = HashSet::new();
for highlight in doc.highlights { for highlight in doc.highlights {
let attr = FieldId::new(highlight.attribute); let attr = FieldId::new(highlight.attribute);
let name = schema.get_name(attr); let name = schema.name(attr);
matching_attributes.insert(name); matching_attributes.insert(name);
} }

View File

@ -161,11 +161,13 @@ where
debug!("criterion loop took {:.02?}", before_criterion_loop.elapsed()); debug!("criterion loop took {:.02?}", before_criterion_loop.elapsed());
debug!("proximity evaluation called {} times", proximity_count.load(Ordering::Relaxed)); debug!("proximity evaluation called {} times", proximity_count.load(Ordering::Relaxed));
let schema = main_store.schema(reader)?.ok_or(Error::SchemaMissing)?;
let iter = raw_documents.into_iter().skip(range.start).take(range.len()); let iter = raw_documents.into_iter().skip(range.start).take(range.len());
let iter = iter.map(|rd| Document::from_raw(rd, &queries_kinds, &arena, searchable_attrs.as_ref())); let iter = iter.map(|rd| Document::from_raw(rd, &automatons, &arena, searchable_attrs.as_ref(), &schema));
let documents = iter.collect(); let documents = iter.collect();
debug!("bucket sort took {:.02?}", before_bucket_sort.elapsed()); debug!("bucket sort took {:.02?}", before_bucket_sort.elapsed());
Ok(documents) Ok(documents)
} }
@ -330,7 +332,7 @@ where
// once we classified the documents related to the current // once we classified the documents related to the current
// automatons we save that as the next valid result // automatons we save that as the next valid result
let mut seen = BufferedDistinctMap::new(&mut distinct_map); let mut seen = BufferedDistinctMap::new(&mut distinct_map);
let schema = main_store.schema(reader)?.unwrap(); let schema = main_store.schema(reader)?.ok_or(Error::SchemaMissing)?;
let mut documents = Vec::with_capacity(range.len()); let mut documents = Vec::with_capacity(range.len());
for raw_document in raw_documents.into_iter().skip(distinct_raw_offset) { for raw_document in raw_documents.into_iter().skip(distinct_raw_offset) {

View File

@ -68,12 +68,12 @@ impl<'a> SortByAttr<'a> {
attr_name: &str, attr_name: &str,
reversed: bool, reversed: bool,
) -> Result<SortByAttr<'a>, SortByAttrError> { ) -> Result<SortByAttr<'a>, SortByAttrError> {
let field_id = match schema.get_id(attr_name) { let field_id = match schema.id(attr_name) {
Some(field_id) => field_id, Some(field_id) => field_id,
None => return Err(SortByAttrError::AttributeNotFound), None => return Err(SortByAttrError::AttributeNotFound),
}; };
if !schema.id_is_ranked(field_id) { if !schema.is_ranked(field_id) {
return Err(SortByAttrError::AttributeNotRegisteredForRanking); return Err(SortByAttrError::AttributeNotRegisteredForRanking);
} }

View File

@ -353,7 +353,6 @@ impl Database {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::criterion::{self, CriteriaBuilder}; use crate::criterion::{self, CriteriaBuilder};
@ -381,13 +380,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description"] "displayedAttributes": ["name", "description"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut update_writer = db.update_write_txn().unwrap(); let mut update_writer = db.update_write_txn().unwrap();
@ -441,13 +440,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description"] "displayedAttributes": ["name", "description"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut update_writer = db.update_write_txn().unwrap(); let mut update_writer = db.update_write_txn().unwrap();
@ -500,13 +499,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name"], "searchableAttributes": ["name"],
"attributesDisplayed": ["name"] "displayedAttributes": ["name"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut update_writer = db.update_write_txn().unwrap(); let mut update_writer = db.update_write_txn().unwrap();
@ -552,13 +551,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description"] "displayedAttributes": ["name", "description"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut update_writer = db.update_write_txn().unwrap(); let mut update_writer = db.update_write_txn().unwrap();
@ -586,17 +585,16 @@ mod tests {
let _update_id = additions.finalize(&mut update_writer).unwrap(); let _update_id = additions.finalize(&mut update_writer).unwrap();
update_writer.commit().unwrap(); update_writer.commit().unwrap();
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description", "age", "sex"], "searchableAttributes": ["name", "description", "age", "sex"],
"attributesDisplayed": ["name", "description", "age", "sex"] "displayedAttributes": ["name", "description", "age", "sex"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();
@ -657,13 +655,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description", "city", "age", "sex"], "searchableAttributes": ["name", "description", "city", "age", "sex"],
"attributesDisplayed": ["name", "description", "city", "age", "sex"] "displayedAttributes": ["name", "description", "city", "age", "sex"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();
@ -696,13 +694,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description"] "displayedAttributes": ["name", "description"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();
@ -773,13 +771,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description", "id"] "displayedAttributes": ["name", "description", "id"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();
@ -909,13 +907,13 @@ mod tests {
let settings = { let settings = {
let data = r#" let data = r#"
{ {
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "description"], "searchableAttributes": ["name", "description"],
"attributesDisplayed": ["name", "description"] "displayedAttributes": ["name", "description"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();
@ -982,13 +980,13 @@ mod tests {
"_exact", "_exact",
"dsc(release_date)" "dsc(release_date)"
], ],
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": ["name", "release_date"], "searchableAttributes": ["name", "release_date"],
"attributesDisplayed": ["name", "release_date"] "displayedAttributes": ["name", "release_date"]
} }
"#; "#;
let settings: Settings = serde_json::from_str(data).unwrap(); let settings: Settings = serde_json::from_str(data).unwrap();
settings.into() settings.into_update().unwrap()
}; };
let mut writer = db.update_write_txn().unwrap(); let mut writer = db.update_write_txn().unwrap();

View File

@ -8,7 +8,7 @@ pub type MResult<T> = Result<T, Error>;
pub enum Error { pub enum Error {
Io(io::Error), Io(io::Error),
IndexAlreadyExists, IndexAlreadyExists,
MissingSchemaIdentifier, MissingIdentifier,
SchemaMissing, SchemaMissing,
WordIndexMissing, WordIndexMissing,
MissingDocumentId, MissingDocumentId,
@ -83,7 +83,7 @@ impl fmt::Display for Error {
match self { match self {
Io(e) => write!(f, "{}", e), Io(e) => write!(f, "{}", e),
IndexAlreadyExists => write!(f, "index already exists"), IndexAlreadyExists => write!(f, "index already exists"),
MissingSchemaIdentifier => write!(f, "schema cannot be build without identifier"), MissingIdentifier => write!(f, "schema cannot be build without identifier"),
SchemaMissing => write!(f, "this index does not have a schema"), SchemaMissing => write!(f, "this index does not have a schema"),
WordIndexMissing => write!(f, "this index does not have a word index"), WordIndexMissing => write!(f, "this index does not have a word index"),
MissingDocumentId => write!(f, "document id is missing"), MissingDocumentId => write!(f, "document id is missing"),

View File

@ -1,94 +0,0 @@
use std::io::{Read, Write};
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::{MResult, Error};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldsMap {
name_map: HashMap<String, u16>,
id_map: HashMap<u16, String>,
next_id: u16
}
impl FieldsMap {
pub fn len(&self) -> usize {
self.name_map.len()
}
pub fn is_empty(&self) -> bool {
self.name_map.is_empty()
}
pub fn insert<T: ToString>(&mut self, name: T) -> MResult<u16> {
let name = name.to_string();
if let Some(id) = self.name_map.get(&name) {
return Ok(*id)
}
let id = self.next_id;
if self.next_id.checked_add(1).is_none() {
return Err(Error::MaxFieldsLimitExceeded)
} else {
self.next_id += 1;
}
self.name_map.insert(name.clone(), id);
self.id_map.insert(id, name);
Ok(id)
}
pub fn remove<T: ToString>(&mut self, name: T) {
let name = name.to_string();
if let Some(id) = self.name_map.get(&name) {
self.id_map.remove(&id);
}
self.name_map.remove(&name);
}
pub fn get_id<T: ToString>(&self, name: T) -> Option<&u16> {
let name = name.to_string();
self.name_map.get(&name)
}
pub fn get_name(&self, id: u16) -> Option<&String> {
self.id_map.get(&id)
}
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<FieldsMap> {
bincode::deserialize_from(reader)
}
pub fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
bincode::serialize_into(writer, &self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fields_map() {
let mut fields_map = FieldsMap::default();
assert_eq!(fields_map.insert("id").unwrap(), 0);
assert_eq!(fields_map.insert("title").unwrap(), 1);
assert_eq!(fields_map.insert("descritpion").unwrap(), 2);
assert_eq!(fields_map.insert("id").unwrap(), 0);
assert_eq!(fields_map.insert("title").unwrap(), 1);
assert_eq!(fields_map.insert("descritpion").unwrap(), 2);
assert_eq!(fields_map.get_id("id"), Some(&0));
assert_eq!(fields_map.get_id("title"), Some(&1));
assert_eq!(fields_map.get_id("descritpion"), Some(&2));
assert_eq!(fields_map.get_id("date"), None);
assert_eq!(fields_map.len(), 3);
assert_eq!(fields_map.get_name(0), Some(&"id".to_owned()));
assert_eq!(fields_map.get_name(1), Some(&"title".to_owned()));
assert_eq!(fields_map.get_name(2), Some(&"descritpion".to_owned()));
assert_eq!(fields_map.get_name(4), None);
fields_map.remove("title");
assert_eq!(fields_map.get_id("title"), None);
assert_eq!(fields_map.insert("title").unwrap(), 3);
assert_eq!(fields_map.len(), 3);
}
}

View File

@ -86,7 +86,7 @@ fn highlights_from_raw_document<'a, 'tag, 'txn>(
Some(field_id) => field_id.0, Some(field_id) => field_id.0,
None => { None => {
error!("Cannot convert indexed_pos {} to field_id", attribute); error!("Cannot convert indexed_pos {} to field_id", attribute);
trace!("Schema is compronized; {:?}", schema); trace!("Schema is compromized; {:?}", schema);
continue continue
} }
}; };
@ -164,7 +164,7 @@ impl Document {
Some(field_id) => field_id.0, Some(field_id) => field_id.0,
None => { None => {
error!("Cannot convert indexed_pos {} to field_id", attribute); error!("Cannot convert indexed_pos {} to field_id", attribute);
trace!("Schema is compronized; {:?}", schema); trace!("Schema is compromized; {:?}", schema);
continue continue
} }
}; };

View File

@ -19,16 +19,16 @@ impl RankedMap {
self.0.is_empty() self.0.is_empty()
} }
pub fn insert(&mut self, document: DocumentId, attribute: FieldId, number: Number) { pub fn insert(&mut self, document: DocumentId, field: FieldId, number: Number) {
self.0.insert((document, attribute), number); self.0.insert((document, field), number);
} }
pub fn remove(&mut self, document: DocumentId, attribute: FieldId) { pub fn remove(&mut self, document: DocumentId, field: FieldId) {
self.0.remove(&(document, attribute)); self.0.remove(&(document, field));
} }
pub fn get(&self, document: DocumentId, attribute: FieldId) -> Option<Number> { pub fn get(&self, document: DocumentId, field: FieldId) -> Option<Number> {
self.0.get(&(document, attribute)).cloned() self.0.get(&(document, field)).cloned()
} }
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<RankedMap> { pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<RankedMap> {

View File

@ -178,7 +178,6 @@ fn token_to_docindex(id: DocumentId, indexed_pos: IndexedPos, token: Token) -> O
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use meilisearch_schema::IndexedPos; use meilisearch_schema::IndexedPos;

View File

@ -54,7 +54,7 @@ pub struct Deserializer<'a> {
pub reader: &'a heed::RoTxn<MainT>, pub reader: &'a heed::RoTxn<MainT>,
pub documents_fields: DocumentsFields, pub documents_fields: DocumentsFields,
pub schema: &'a Schema, pub schema: &'a Schema,
pub attributes: Option<&'a HashSet<FieldId>>, pub fields: Option<&'a HashSet<FieldId>>,
} }
impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> { impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> {
@ -92,9 +92,9 @@ impl<'de, 'a, 'b> de::Deserializer<'de> for &'b mut Deserializer<'a> {
} }
}; };
let is_displayed = self.schema.id_is_displayed(attr); let is_displayed = self.schema.is_displayed(attr);
if is_displayed && self.attributes.map_or(true, |f| f.contains(&attr)) { if is_displayed && self.fields.map_or(true, |f| f.contains(&attr)) {
if let Some(attribute_name) = self.schema.get_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());
let ioread = SerdeJsonIoRead::new(cursor); let ioread = SerdeJsonIoRead::new(cursor);
let value = Value(SerdeJsonDeserializer::new(ioread)); let value = Value(SerdeJsonDeserializer::new(ioread));

View File

@ -2,7 +2,7 @@ use std::hash::{Hash, Hasher};
use crate::DocumentId; use crate::DocumentId;
use serde::{ser, Serialize}; use serde::{ser, Serialize};
use serde_json::Value; use serde_json::{Value, Number};
use siphasher::sip::SipHasher; use siphasher::sip::SipHasher;
use super::{ConvertToString, SerializerError}; use super::{ConvertToString, SerializerError};
@ -18,18 +18,27 @@ where
document.serialize(serializer) document.serialize(serializer)
} }
fn validate_number(value: &Number) -> Option<String> {
if value.is_f64() {
return None
}
return Some(value.to_string())
}
fn validate_string(value: &String) -> Option<String> {
if value.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') {
Some(value.to_string())
} else {
None
}
}
pub fn value_to_string(value: &Value) -> Option<String> { pub fn value_to_string(value: &Value) -> Option<String> {
match value { match value {
Value::Null => None, Value::Null => None,
Value::Bool(_) => None, Value::Bool(_) => None,
Value::Number(value) => Some(value.to_string()), Value::Number(value) => validate_number(value),
Value::String(value) => { Value::String(value) => validate_string(value),
if value.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_') {
Some(value.to_string())
} else {
None
}
},
Value::Array(_) => None, Value::Array(_) => None,
Value::Object(_) => None, Value::Object(_) => None,
} }

View File

@ -1,4 +1,4 @@
use meilisearch_schema::{IndexedPos}; use meilisearch_schema::IndexedPos;
use serde::ser; use serde::ser;
use serde::Serialize; use serde::Serialize;

View File

@ -57,7 +57,7 @@ impl fmt::Display for SerializerError {
f.write_str("serialized document does not have an id according to the schema") f.write_str("serialized document does not have an id according to the schema")
} }
SerializerError::InvalidDocumentIdType => { SerializerError::InvalidDocumentIdType => {
f.write_str("document identifier can only be of type number or string (A-Z, a-z, 0-9, -_)") f.write_str("documents identifiers can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).")
} }
SerializerError::Zlmdb(e) => write!(f, "heed related error: {}", e), SerializerError::Zlmdb(e) => write!(f, "heed related error: {}", e),
SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e), SerializerError::SerdeJson(e) => write!(f, "serde json error: {}", e),

View File

@ -305,7 +305,7 @@ pub fn serialize_value<'a, T: ?Sized>(
where where
T: ser::Serialize, T: ser::Serialize,
{ {
let field_id = schema.get_or_create(attribute.clone())?; let field_id = schema.get_or_create(&attribute)?;
serialize_value_with_id( serialize_value_with_id(
txn, txn,
@ -337,7 +337,7 @@ where
let serialized = serde_json::to_vec(value)?; let serialized = serde_json::to_vec(value)?;
document_store.put_document_field(txn, document_id, field_id, &serialized)?; document_store.put_document_field(txn, document_id, field_id, &serialized)?;
if let Some(indexed_pos) = schema.id_is_indexed(field_id) { if let Some(indexed_pos) = schema.is_indexed(field_id) {
let indexer = Indexer { let indexer = Indexer {
pos: *indexed_pos, pos: *indexed_pos,
indexer, indexer,
@ -353,7 +353,7 @@ where
} }
} }
if schema.id_is_ranked(field_id) { if schema.is_ranked(field_id) {
let number = value.serialize(ConvertToNumber)?; let number = value.serialize(ConvertToNumber)?;
ranked_map.insert(document_id, field_id, number); ranked_map.insert(document_id, field_id, number);
} }

View File

@ -1,16 +1,14 @@
use std::sync::Mutex;
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::str::FromStr; use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize}; use serde::{Deserialize, Deserializer, Serialize};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
static RANKING_RULE_REGEX: Lazy<Mutex<regex::Regex>> = Lazy::new(|| { static RANKING_RULE_REGEX: Lazy<regex::Regex> = Lazy::new(|| {
let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap(); let regex = regex::Regex::new(r"(asc|dsc)\(([a-zA-Z0-9-_]*)\)").unwrap();
Mutex::new(regex) regex
}); });
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Settings { pub struct Settings {
@ -19,11 +17,11 @@ pub struct Settings {
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub ranking_distinct: Option<Option<String>>, pub ranking_distinct: Option<Option<String>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub attribute_identifier: Option<Option<String>>, pub identifier: Option<Option<String>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub attributes_searchable: Option<Option<Vec<String>>>, pub searchable_attributes: Option<Option<Vec<String>>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub attributes_displayed: Option<Option<HashSet<String>>>, pub displayed_attributes: Option<Option<HashSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
pub stop_words: Option<Option<BTreeSet<String>>>, pub stop_words: Option<Option<BTreeSet<String>>>,
#[serde(default, deserialize_with = "deserialize_some")] #[serde(default, deserialize_with = "deserialize_some")]
@ -40,34 +38,32 @@ fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
Deserialize::deserialize(deserializer).map(Some) Deserialize::deserialize(deserializer).map(Some)
} }
impl Into<SettingsUpdate> for Settings { impl Settings {
fn into(self) -> SettingsUpdate { pub fn into_update(&self) -> Result<SettingsUpdate, RankingRuleConversionError> {
let settings = self.clone(); let settings = self.clone();
let ranking_rules = match settings.ranking_rules { let ranking_rules = match settings.ranking_rules {
Some(Some(rules)) => UpdateState::Update(RankingRule::from_vec(rules)), Some(Some(rules)) => UpdateState::Update(RankingRule::from_vec(rules.iter().map(|m| m.as_ref()).collect())?),
Some(None) => UpdateState::Clear, Some(None) => UpdateState::Clear,
None => UpdateState::Nothing, None => UpdateState::Nothing,
}; };
SettingsUpdate { Ok(SettingsUpdate {
ranking_rules: ranking_rules, ranking_rules: ranking_rules,
ranking_distinct: settings.ranking_distinct.into(), ranking_distinct: settings.ranking_distinct.into(),
attribute_identifier: settings.attribute_identifier.into(), identifier: settings.identifier.into(),
attributes_searchable: settings.attributes_searchable.into(), searchable_attributes: settings.searchable_attributes.into(),
attributes_displayed: settings.attributes_displayed.into(), displayed_attributes: settings.displayed_attributes.into(),
stop_words: settings.stop_words.into(), stop_words: settings.stop_words.into(),
synonyms: settings.synonyms.into(), synonyms: settings.synonyms.into(),
index_new_fields: settings.index_new_fields.into(), index_new_fields: settings.index_new_fields.into(),
} })
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateState<T> { pub enum UpdateState<T> {
Update(T), Update(T),
Add(T),
Delete(T),
Clear, Clear,
Nothing, Nothing,
} }
@ -82,15 +78,6 @@ impl <T> From<Option<Option<T>>> for UpdateState<T> {
} }
} }
impl<T> UpdateState<T> {
pub fn is_changed(&self) -> bool {
match self {
UpdateState::Nothing => false,
_ => true,
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RankingRuleConversionError; pub struct RankingRuleConversionError;
@ -139,10 +126,10 @@ impl FromStr for RankingRule {
"_words_position" => RankingRule::WordsPosition, "_words_position" => RankingRule::WordsPosition,
"_exact" => RankingRule::Exact, "_exact" => RankingRule::Exact,
_ => { _ => {
let captures = RANKING_RULE_REGEX.lock().unwrap().captures(s).unwrap(); let captures = RANKING_RULE_REGEX.captures(s).ok_or(RankingRuleConversionError)?;
match captures[1].as_ref() { match (captures.get(1).map(|m| m.as_str()), captures.get(2)) {
"asc" => RankingRule::Asc(captures[2].to_string()), (Some("asc"), Some(field)) => RankingRule::Asc(field.as_str().to_string()),
"dsc" => RankingRule::Dsc(captures[2].to_string()), (Some("dsc"), Some(field)) => RankingRule::Dsc(field.as_str().to_string()),
_ => return Err(RankingRuleConversionError) _ => return Err(RankingRuleConversionError)
} }
} }
@ -152,17 +139,16 @@ impl FromStr for RankingRule {
} }
impl RankingRule { impl RankingRule {
pub fn get_field(&self) -> Option<String> { pub fn get_field(&self) -> Option<&str> {
match self { match self {
RankingRule::Asc(field) | RankingRule::Dsc(field) => Some((*field).clone()), RankingRule::Asc(field) | RankingRule::Dsc(field) => Some(field),
_ => None, _ => None,
} }
} }
pub fn from_vec(rules: Vec<String>) -> Vec<RankingRule> { pub fn from_vec(rules: Vec<&str>) -> Result<Vec<RankingRule>, RankingRuleConversionError> {
rules.iter() rules.iter()
.map(|s| RankingRule::from_str(s.as_str())) .map(|s| RankingRule::from_str(s))
.filter_map(Result::ok)
.collect() .collect()
} }
} }
@ -171,9 +157,9 @@ impl RankingRule {
pub struct SettingsUpdate { pub struct SettingsUpdate {
pub ranking_rules: UpdateState<Vec<RankingRule>>, pub ranking_rules: UpdateState<Vec<RankingRule>>,
pub ranking_distinct: UpdateState<String>, pub ranking_distinct: UpdateState<String>,
pub attribute_identifier: UpdateState<String>, pub identifier: UpdateState<String>,
pub attributes_searchable: UpdateState<Vec<String>>, pub searchable_attributes: UpdateState<Vec<String>>,
pub attributes_displayed: UpdateState<HashSet<String>>, pub displayed_attributes: UpdateState<HashSet<String>>,
pub stop_words: UpdateState<BTreeSet<String>>, pub stop_words: UpdateState<BTreeSet<String>>,
pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>, pub synonyms: UpdateState<BTreeMap<String, Vec<String>>>,
pub index_new_fields: UpdateState<bool>, pub index_new_fields: UpdateState<bool>,
@ -184,9 +170,9 @@ impl Default for SettingsUpdate {
Self { Self {
ranking_rules: UpdateState::Nothing, ranking_rules: UpdateState::Nothing,
ranking_distinct: UpdateState::Nothing, ranking_distinct: UpdateState::Nothing,
attribute_identifier: UpdateState::Nothing, identifier: UpdateState::Nothing,
attributes_searchable: UpdateState::Nothing, searchable_attributes: UpdateState::Nothing,
attributes_displayed: UpdateState::Nothing, displayed_attributes: UpdateState::Nothing,
stop_words: UpdateState::Nothing, stop_words: UpdateState::Nothing,
synonyms: UpdateState::Nothing, synonyms: UpdateState::Nothing,
index_new_fields: UpdateState::Nothing, index_new_fields: UpdateState::Nothing,

View File

@ -16,10 +16,10 @@ impl DocumentsFields {
self, self,
writer: &mut heed::RwTxn<MainT>, writer: &mut heed::RwTxn<MainT>,
document_id: DocumentId, document_id: DocumentId,
attribute: FieldId, field: FieldId,
value: &[u8], value: &[u8],
) -> ZResult<()> { ) -> ZResult<()> {
let key = DocumentFieldStoredKey::new(document_id, attribute); let key = DocumentFieldStoredKey::new(document_id, field);
self.documents_fields.put(writer, &key, value) self.documents_fields.put(writer, &key, value)
} }
@ -41,9 +41,9 @@ impl DocumentsFields {
self, self,
reader: &'txn heed::RoTxn<MainT>, reader: &'txn heed::RoTxn<MainT>,
document_id: DocumentId, document_id: DocumentId,
attribute: FieldId, field: FieldId,
) -> ZResult<Option<&'txn [u8]>> { ) -> ZResult<Option<&'txn [u8]>> {
let key = DocumentFieldStoredKey::new(document_id, attribute); let key = DocumentFieldStoredKey::new(document_id, field);
self.documents_fields.get(reader, &key) self.documents_fields.get(reader, &key)
} }

View File

@ -11,11 +11,11 @@ use crate::RankedMap;
use crate::settings::RankingRule; use crate::settings::RankingRule;
const CREATED_AT_KEY: &str = "created-at"; const CREATED_AT_KEY: &str = "created-at";
const RANKING_RULES_KEY: &str = "ranking-rules-key"; const RANKING_RULES_KEY: &str = "ranking-rules";
const RANKING_DISTINCT_KEY: &str = "ranking-distinct-key"; const RANKING_DISTINCT_KEY: &str = "ranking-distinct";
const STOP_WORDS_KEY: &str = "stop-words-key"; const STOP_WORDS_KEY: &str = "stop-words";
const SYNONYMS_KEY: &str = "synonyms-key"; const SYNONYMS_KEY: &str = "synonyms";
const CUSTOMS_KEY: &str = "customs-key"; const CUSTOMS_KEY: &str = "customs";
const FIELDS_FREQUENCY_KEY: &str = "fields-frequency"; const FIELDS_FREQUENCY_KEY: &str = "fields-frequency";
const NAME_KEY: &str = "name"; const NAME_KEY: &str = "name";
const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents"; const NUMBER_OF_DOCUMENTS_KEY: &str = "number-of-documents";
@ -188,7 +188,7 @@ impl Main {
} }
} }
pub fn ranking_rules<'txn>(&self, reader: &'txn heed::RoTxn<MainT>) -> ZResult<Option<Vec<RankingRule>>> { pub fn ranking_rules(&self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<Vec<RankingRule>>> {
self.main.get::<_, Str, SerdeBincode<Vec<RankingRule>>>(reader, RANKING_RULES_KEY) self.main.get::<_, Str, SerdeBincode<Vec<RankingRule>>>(reader, RANKING_RULES_KEY)
} }
@ -200,7 +200,7 @@ impl Main {
self.main.delete::<_, Str>(writer, RANKING_RULES_KEY) self.main.delete::<_, Str>(writer, RANKING_RULES_KEY)
} }
pub fn ranking_distinct<'txn>(&self, reader: &'txn heed::RoTxn<MainT>) -> ZResult<Option<String>> { pub fn ranking_distinct(&self, reader: &heed::RoTxn<MainT>) -> ZResult<Option<String>> {
self.main.get::<_, Str, SerdeBincode<String>>(reader, RANKING_DISTINCT_KEY) self.main.get::<_, Str, SerdeBincode<String>>(reader, RANKING_DISTINCT_KEY)
} }

View File

@ -223,7 +223,7 @@ impl Index {
let schema = schema.ok_or(Error::SchemaMissing)?; let schema = schema.ok_or(Error::SchemaMissing)?;
let attributes = match attributes { let attributes = match attributes {
Some(attributes) => Some(attributes.iter().filter_map(|name| schema.get_id(*name)).collect()), Some(attributes) => Some(attributes.iter().filter_map(|name| schema.id(*name)).collect()),
None => None, None => None,
}; };
@ -232,7 +232,7 @@ impl Index {
reader, reader,
documents_fields: self.documents_fields, documents_fields: self.documents_fields,
schema: &schema, schema: &schema,
attributes: attributes.as_ref(), fields: attributes.as_ref(),
}; };
Ok(Option::<T>::deserialize(&mut deserializer)?) Ok(Option::<T>::deserialize(&mut deserializer)?)

View File

@ -158,8 +158,6 @@ pub fn apply_documents_addition<'a, 'b>(
document.serialize(serializer)?; document.serialize(serializer)?;
} }
write_documents_addition_index( write_documents_addition_index(
writer, writer,
index, index,
@ -199,7 +197,7 @@ pub fn apply_documents_partial_addition<'a, 'b>(
reader: writer, reader: writer,
documents_fields: index.documents_fields, documents_fields: index.documents_fields,
schema: &schema, schema: &schema,
attributes: None, fields: None,
}; };
// retrieve the old document and // retrieve the old document and
@ -246,8 +244,6 @@ pub fn apply_documents_partial_addition<'a, 'b>(
document.serialize(serializer)?; document.serialize(serializer)?;
} }
write_documents_addition_index( write_documents_addition_index(
writer, writer,
index, index,

View File

@ -101,12 +101,12 @@ pub fn apply_documents_deletion(
}; };
// collect the ranked attributes according to the schema // collect the ranked attributes according to the schema
let ranked_attrs = schema.get_ranked(); let ranked_fields = schema.ranked();
let mut words_document_ids = HashMap::new(); let mut words_document_ids = HashMap::new();
for id in idset { for id in idset {
// remove all the ranked attributes from the ranked_map // remove all the ranked attributes from the ranked_map
for ranked_attr in &ranked_attrs { for ranked_attr in &ranked_fields {
ranked_map.remove(id, *ranked_attr); ranked_map.remove(id, *ranked_attr);
} }

View File

@ -4,7 +4,6 @@ mod documents_addition;
mod documents_deletion; mod documents_deletion;
mod settings_update; mod settings_update;
pub use self::clear_all::{apply_clear_all, push_clear_all}; pub use self::clear_all::{apply_clear_all, push_clear_all};
pub use self::customs_update::{apply_customs_update, push_customs_update}; pub use self::customs_update::{apply_customs_update, push_customs_update};
pub use self::documents_addition::{ pub use self::documents_addition::{

View File

@ -35,27 +35,27 @@ pub fn apply_settings_update(
let mut schema = match index.main.schema(writer)? { let mut schema = match index.main.schema(writer)? {
Some(schema) => schema, Some(schema) => schema,
None => { None => {
match settings.attribute_identifier.clone() { match settings.identifier.clone() {
UpdateState::Update(id) => Schema::with_identifier(id), UpdateState::Update(id) => Schema::with_identifier(&id),
_ => return Err(Error::MissingSchemaIdentifier) _ => return Err(Error::MissingIdentifier)
} }
} }
}; };
match settings.ranking_rules { match settings.ranking_rules {
UpdateState::Update(v) => { UpdateState::Update(v) => {
let ranked_field: Vec<String> = v.iter().filter_map(RankingRule::get_field).collect(); let ranked_field: Vec<&str> = v.iter().filter_map(RankingRule::get_field).collect();
schema.update_ranked(ranked_field)?; schema.update_ranked(ranked_field)?;
index.main.put_ranking_rules(writer, v)?; index.main.put_ranking_rules(writer, v)?;
must_reindex = true; must_reindex = true;
}, },
UpdateState::Clear => { UpdateState::Clear => {
let clear: Vec<String> = Vec::new(); let clear: Vec<&str> = Vec::new();
schema.update_ranked(clear)?; schema.update_ranked(clear)?;
index.main.delete_ranking_rules(writer)?; index.main.delete_ranking_rules(writer)?;
must_reindex = true; must_reindex = true;
}, },
_ => (), UpdateState::Nothing => (),
} }
match settings.ranking_distinct { match settings.ranking_distinct {
@ -65,65 +65,43 @@ pub fn apply_settings_update(
UpdateState::Clear => { UpdateState::Clear => {
index.main.delete_ranking_distinct(writer)?; index.main.delete_ranking_distinct(writer)?;
}, },
_ => (), UpdateState::Nothing => (),
} }
match settings.index_new_fields { match settings.index_new_fields {
UpdateState::Update(v) => { UpdateState::Update(v) => {
schema.set_must_index_new_fields(v); schema.set_index_new_fields(v);
}, },
UpdateState::Clear => { UpdateState::Clear => {
schema.set_must_index_new_fields(true); schema.set_index_new_fields(true);
}, },
_ => (), UpdateState::Nothing => (),
} }
match settings.attributes_searchable.clone() { match settings.searchable_attributes.clone() {
UpdateState::Update(v) => { UpdateState::Update(v) => {
schema.update_indexed(v)?; schema.update_indexed(v)?;
must_reindex = true; must_reindex = true;
}, },
UpdateState::Clear => { UpdateState::Clear => {
let clear: Vec<String> = Vec::new(); let clear: Vec<&str> = Vec::new();
schema.update_indexed(clear)?; schema.update_indexed(clear)?;
must_reindex = true; must_reindex = true;
}, },
UpdateState::Nothing => (), UpdateState::Nothing => (),
UpdateState::Add(attrs) => {
for attr in attrs {
schema.set_indexed(attr)?;
}
must_reindex = true;
},
UpdateState::Delete(attrs) => {
for attr in attrs {
schema.remove_indexed(attr);
}
must_reindex = true;
}
}; };
match settings.attributes_displayed.clone() { match settings.displayed_attributes.clone() {
UpdateState::Update(v) => schema.update_displayed(v)?, UpdateState::Update(v) => schema.update_displayed(v)?,
UpdateState::Clear => { UpdateState::Clear => {
let clear: Vec<String> = Vec::new(); let clear: Vec<&str> = Vec::new();
schema.update_displayed(clear)?; schema.update_displayed(clear)?;
}, },
UpdateState::Nothing => (), UpdateState::Nothing => (),
UpdateState::Add(attrs) => {
for attr in attrs {
schema.set_displayed(attr)?;
}
},
UpdateState::Delete(attrs) => {
for attr in attrs {
schema.remove_displayed(attr);
}
}
}; };
match settings.attribute_identifier.clone() { match settings.identifier.clone() {
UpdateState::Update(v) => { UpdateState::Update(v) => {
schema.set_identifier(v)?; schema.set_identifier(v.as_ref())?;
index.main.put_schema(writer, &schema)?; index.main.put_schema(writer, &schema)?;
must_reindex = true; must_reindex = true;
}, },
@ -168,7 +146,7 @@ pub fn apply_settings_update(
docs_words_store, docs_words_store,
)?; )?;
} }
if let UpdateState::Clear = settings.attribute_identifier { if let UpdateState::Clear = settings.identifier {
index.main.delete_schema(writer)?; index.main.delete_schema(writer)?;
} }
Ok(()) Ok(())
@ -189,8 +167,8 @@ pub fn apply_stop_words_update(
.stream() .stream()
.into_strs().unwrap().into_iter().collect(); .into_strs().unwrap().into_iter().collect();
let deletion: BTreeSet<String> = old_stop_words.clone().difference(&stop_words).cloned().collect(); let deletion: BTreeSet<String> = old_stop_words.difference(&stop_words).cloned().collect();
let addition: BTreeSet<String> = stop_words.clone().difference(&old_stop_words).cloned().collect(); let addition: BTreeSet<String> = stop_words.difference(&old_stop_words).cloned().collect();
if !addition.is_empty() { if !addition.is_empty() {
apply_stop_words_addition( apply_stop_words_addition(
@ -201,11 +179,12 @@ pub fn apply_stop_words_update(
} }
if !deletion.is_empty() { if !deletion.is_empty() {
must_reindex = apply_stop_words_deletion( apply_stop_words_deletion(
writer, writer,
index, index,
deletion deletion
)?; )?;
must_reindex = true;
} }
Ok(must_reindex) Ok(must_reindex)
@ -275,7 +254,7 @@ fn apply_stop_words_deletion(
writer: &mut heed::RwTxn<MainT>, writer: &mut heed::RwTxn<MainT>,
index: &store::Index, index: &store::Index,
deletion: BTreeSet<String>, deletion: BTreeSet<String>,
) -> MResult<bool> { ) -> MResult<()> {
let main_store = index.main; let main_store = index.main;
@ -306,17 +285,7 @@ fn apply_stop_words_deletion(
.and_then(fst::Set::from_bytes) .and_then(fst::Set::from_bytes)
.unwrap(); .unwrap();
main_store.put_stop_words_fst(writer, &stop_words_fst)?; Ok(main_store.put_stop_words_fst(writer, &stop_words_fst)?)
// now that we have setup the stop words
// lets reindex everything...
if let Ok(number) = main_store.number_of_documents(writer) {
if number > 0 {
return Ok(true)
}
}
Ok(false)
} }
pub fn apply_synonyms_update( pub fn apply_synonyms_update(

View File

@ -14,7 +14,7 @@ name = "meilisearch"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
async-std = { version = "1.0.1", features = ["unstable", "attributes"] } async-std = { version = "1.0.1", features = ["attributes"] }
bincode = "1.2.0" bincode = "1.2.0"
chrono = { version = "0.4.9", features = ["serde"] } chrono = { version = "0.4.9", features = ["serde"] }
crossbeam-channel = "0.4.0" crossbeam-channel = "0.4.0"
@ -35,12 +35,13 @@ serde_qs = "0.5.1"
siphasher = "0.3.1" siphasher = "0.3.1"
structopt = "0.3.3" structopt = "0.3.3"
sysinfo = "0.9.5" sysinfo = "0.9.5"
tide = "0.5.1" tide = "0.6.0"
ureq = { version = "0.11.2", features = ["tls"], default-features = false } ureq = { version = "0.11.2", features = ["tls"], default-features = false }
walkdir = "2.2.9" walkdir = "2.2.9"
whoami = "0.6" whoami = "0.6"
http-service = "0.4.0" http-service = "0.4.0"
futures = "0.3.1" futures = "0.3.1"
mime = "0.3.16"
[dev-dependencies] [dev-dependencies]
http-service-mock = "0.4.0" http-service-mock = "0.4.0"

View File

@ -1,424 +0,0 @@
//! Cors middleware
use futures::future::BoxFuture;
use http::header::HeaderValue;
use http::{header, Method, StatusCode};
use http_service::Body;
use tide::middleware::{Middleware, Next};
use tide::{Request, Response};
/// Middleware for CORS
///
/// # Example
///
/// ```no_run
/// use http::header::HeaderValue;
/// use tide::middleware::{Cors, Origin};
///
/// Cors::new()
/// .allow_methods(HeaderValue::from_static("GET, POST, OPTIONS"))
/// .allow_origin(Origin::from("*"))
/// .allow_credentials(false);
/// ```
#[derive(Clone, Debug, Hash)]
pub struct Cors {
allow_credentials: Option<HeaderValue>,
allow_headers: HeaderValue,
allow_methods: HeaderValue,
allow_origin: Origin,
expose_headers: Option<HeaderValue>,
max_age: HeaderValue,
}
pub const DEFAULT_MAX_AGE: &str = "86400";
pub const DEFAULT_METHODS: &str = "GET, POST, OPTIONS";
pub const WILDCARD: &str = "*";
impl Cors {
/// Creates a new Cors middleware.
pub fn new() -> Self {
Self {
allow_credentials: None,
allow_headers: HeaderValue::from_static(WILDCARD),
allow_methods: HeaderValue::from_static(DEFAULT_METHODS),
allow_origin: Origin::Any,
expose_headers: None,
max_age: HeaderValue::from_static(DEFAULT_MAX_AGE),
}
}
/// Set allow_credentials and return new Cors
pub fn allow_credentials(mut self, allow_credentials: bool) -> Self {
self.allow_credentials = match HeaderValue::from_str(&allow_credentials.to_string()) {
Ok(header) => Some(header),
Err(_) => None,
};
self
}
/// Set allow_headers and return new Cors
pub fn allow_headers<T: Into<HeaderValue>>(mut self, headers: T) -> Self {
self.allow_headers = headers.into();
self
}
/// Set max_age and return new Cors
pub fn max_age<T: Into<HeaderValue>>(mut self, max_age: T) -> Self {
self.max_age = max_age.into();
self
}
/// Set allow_methods and return new Cors
pub fn allow_methods<T: Into<HeaderValue>>(mut self, methods: T) -> Self {
self.allow_methods = methods.into();
self
}
/// Set allow_origin and return new Cors
pub fn allow_origin<T: Into<Origin>>(mut self, origin: T) -> Self {
self.allow_origin = origin.into();
self
}
/// Set expose_headers and return new Cors
pub fn expose_headers<T: Into<HeaderValue>>(mut self, headers: T) -> Self {
self.expose_headers = Some(headers.into());
self
}
fn build_preflight_response(&self, origin: &HeaderValue) -> http::response::Response<Body> {
let mut response = http::Response::builder()
.status(StatusCode::OK)
.header::<_, HeaderValue>(header::ACCESS_CONTROL_ALLOW_ORIGIN, origin.clone())
.header(
header::ACCESS_CONTROL_ALLOW_METHODS,
self.allow_methods.clone(),
)
.header(
header::ACCESS_CONTROL_ALLOW_HEADERS,
self.allow_headers.clone(),
)
.header(header::ACCESS_CONTROL_MAX_AGE, self.max_age.clone())
.body(Body::empty())
.unwrap();
if let Some(allow_credentials) = self.allow_credentials.clone() {
response
.headers_mut()
.append(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, allow_credentials);
}
if let Some(expose_headers) = self.expose_headers.clone() {
response
.headers_mut()
.append(header::ACCESS_CONTROL_EXPOSE_HEADERS, expose_headers);
}
response
}
/// Look at origin of request and determine allow_origin
fn response_origin<T: Into<HeaderValue>>(&self, origin: T) -> Option<HeaderValue> {
let origin = origin.into();
if !self.is_valid_origin(origin.clone()) {
return None;
}
match self.allow_origin {
Origin::Any => Some(HeaderValue::from_static(WILDCARD)),
_ => Some(origin),
}
}
/// Determine if origin is appropriate
fn is_valid_origin<T: Into<HeaderValue>>(&self, origin: T) -> bool {
let origin = match origin.into().to_str() {
Ok(s) => s.to_string(),
Err(_) => return false,
};
match &self.allow_origin {
Origin::Any => true,
Origin::Exact(s) => s == &origin,
Origin::List(list) => list.contains(&origin),
}
}
}
impl<State: Send + Sync + 'static> Middleware<State> for Cors {
fn handle<'a>(&'a self, req: Request<State>, next: Next<'a, State>) -> BoxFuture<'a, Response> {
Box::pin(async move {
let origin = req
.headers()
.get(header::ORIGIN)
.cloned()
.unwrap_or_else(|| HeaderValue::from_static(""));
if !self.is_valid_origin(&origin) {
return http::Response::builder()
.status(StatusCode::UNAUTHORIZED)
.body(Body::empty())
.unwrap()
.into();
}
// Return results immediately upon preflight request
if req.method() == Method::OPTIONS {
return self.build_preflight_response(&origin).into();
}
let mut response: http_service::Response = next.run(req).await.into();
let headers = response.headers_mut();
headers.append(
header::ACCESS_CONTROL_ALLOW_ORIGIN,
self.response_origin(origin).unwrap(),
);
if let Some(allow_credentials) = self.allow_credentials.clone() {
headers.append(header::ACCESS_CONTROL_ALLOW_CREDENTIALS, allow_credentials);
}
if let Some(expose_headers) = self.expose_headers.clone() {
headers.append(header::ACCESS_CONTROL_EXPOSE_HEADERS, expose_headers);
}
response.into()
})
}
}
impl Default for Cors {
fn default() -> Self {
Self::new()
}
}
/// allow_origin enum
#[derive(Clone, Debug, Hash, PartialEq)]
pub enum Origin {
/// Wildcard. Accept all origin requests
Any,
/// Set a single allow_origin target
Exact(String),
/// Set multiple allow_origin targets
List(Vec<String>),
}
impl From<String> for Origin {
fn from(s: String) -> Self {
if s == "*" {
return Origin::Any;
}
Origin::Exact(s)
}
}
impl From<&str> for Origin {
fn from(s: &str) -> Self {
Origin::from(s.to_string())
}
}
impl From<Vec<String>> for Origin {
fn from(list: Vec<String>) -> Self {
if list.len() == 1 {
return Self::from(list[0].clone());
}
Origin::List(list)
}
}
impl From<Vec<&str>> for Origin {
fn from(list: Vec<&str>) -> Self {
Origin::from(list.iter().map(|s| s.to_string()).collect::<Vec<String>>())
}
}
#[cfg(test)]
mod test {
use super::*;
use http::header::HeaderValue;
use http_service::Body;
use http_service_mock::make_server;
const ALLOW_ORIGIN: &str = "example.com";
const ALLOW_METHODS: &str = "GET, POST, OPTIONS, DELETE";
const EXPOSE_HEADER: &str = "X-My-Custom-Header";
const ENDPOINT: &str = "/cors";
fn app() -> tide::Server<()> {
let mut app = tide::Server::new();
app.at(ENDPOINT).get(|_| async move { "Hello World" });
app
}
fn request() -> http::Request<http_service::Body> {
http::Request::get(ENDPOINT)
.header(http::header::ORIGIN, ALLOW_ORIGIN)
.method(http::method::Method::GET)
.body(Body::empty())
.unwrap()
}
#[test]
fn preflight_request() {
let mut app = app();
app.middleware(
Cors::new()
.allow_origin(Origin::from(ALLOW_ORIGIN))
.allow_methods(HeaderValue::from_static(ALLOW_METHODS))
.expose_headers(HeaderValue::from_static(EXPOSE_HEADER))
.allow_credentials(true),
);
let mut server = make_server(app.into_http_service()).unwrap();
let req = http::Request::get(ENDPOINT)
.header(http::header::ORIGIN, ALLOW_ORIGIN)
.method(http::method::Method::OPTIONS)
.body(Body::empty())
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
res.headers().get("access-control-allow-origin").unwrap(),
ALLOW_ORIGIN
);
assert_eq!(
res.headers().get("access-control-allow-methods").unwrap(),
ALLOW_METHODS
);
assert_eq!(
res.headers().get("access-control-allow-headers").unwrap(),
WILDCARD
);
assert_eq!(
res.headers().get("access-control-max-age").unwrap(),
DEFAULT_MAX_AGE
);
assert_eq!(
res.headers()
.get("access-control-allow-credentials")
.unwrap(),
"true"
);
}
#[test]
fn default_cors_middleware() {
let mut app = app();
app.middleware(Cors::new());
let mut server = make_server(app.into_http_service()).unwrap();
let res = server.simulate(request()).unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
res.headers().get("access-control-allow-origin").unwrap(),
"*"
);
}
#[test]
fn custom_cors_middleware() {
let mut app = app();
app.middleware(
Cors::new()
.allow_origin(Origin::from(ALLOW_ORIGIN))
.allow_credentials(false)
.allow_methods(HeaderValue::from_static(ALLOW_METHODS))
.expose_headers(HeaderValue::from_static(EXPOSE_HEADER)),
);
let mut server = make_server(app.into_http_service()).unwrap();
let res = server.simulate(request()).unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
res.headers().get("access-control-allow-origin").unwrap(),
ALLOW_ORIGIN
);
}
#[test]
fn credentials_true() {
let mut app = app();
app.middleware(Cors::new().allow_credentials(true));
let mut server = make_server(app.into_http_service()).unwrap();
let res = server.simulate(request()).unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
res.headers()
.get("access-control-allow-credentials")
.unwrap(),
"true"
);
}
#[test]
fn set_allow_origin_list() {
let mut app = app();
let origins = vec![ALLOW_ORIGIN, "foo.com", "bar.com"];
app.middleware(Cors::new().allow_origin(origins.clone()));
let mut server = make_server(app.into_http_service()).unwrap();
for origin in origins {
let request = http::Request::get(ENDPOINT)
.header(http::header::ORIGIN, origin)
.method(http::method::Method::GET)
.body(Body::empty())
.unwrap();
let res = server.simulate(request).unwrap();
assert_eq!(res.status(), 200);
assert_eq!(
res.headers().get("access-control-allow-origin").unwrap(),
origin
);
}
}
#[test]
fn not_set_origin_header() {
let mut app = app();
app.middleware(Cors::new());
let request = http::Request::get(ENDPOINT)
.method(http::method::Method::GET)
.body(Body::empty())
.unwrap();
let mut server = make_server(app.into_http_service()).unwrap();
let res = server.simulate(request).unwrap();
assert_eq!(res.status(), 200);
}
#[test]
fn unauthorized_origin() {
let mut app = app();
app.middleware(Cors::new().allow_origin(ALLOW_ORIGIN));
let request = http::Request::get(ENDPOINT)
.header(http::header::ORIGIN, "unauthorize-origin.net")
.method(http::method::Method::GET)
.body(Body::empty())
.unwrap();
let mut server = make_server(app.into_http_service()).unwrap();
let res = server.simulate(request).unwrap();
assert_eq!(res.status(), 401);
}
}

View File

@ -92,7 +92,7 @@ impl DataInner {
// convert attributes to their names // convert attributes to their names
let frequency: HashMap<_, _> = fields_frequency let frequency: HashMap<_, _> = fields_frequency
.into_iter() .into_iter()
.map(|(a, c)| (schema.get_name(a).unwrap(), c)) .map(|(a, c)| (schema.name(a).unwrap().to_string(), c))
.collect(); .collect();
index index

View File

@ -127,6 +127,12 @@ fn error(message: String, status: StatusCode) -> Response {
.unwrap() .unwrap()
} }
impl From<serde_json::Error> for ResponseError {
fn from(err: serde_json::Error) -> ResponseError {
ResponseError::internal(err)
}
}
impl From<meilisearch_core::Error> for ResponseError { impl From<meilisearch_core::Error> for ResponseError {
fn from(err: meilisearch_core::Error) -> ResponseError { fn from(err: meilisearch_core::Error) -> ResponseError {
ResponseError::internal(err) ResponseError::internal(err)
@ -151,11 +157,16 @@ impl From<SearchError> for ResponseError {
} }
} }
impl From<meilisearch_core::settings::RankingRuleConversionError> for ResponseError {
fn from(err: meilisearch_core::settings::RankingRuleConversionError) -> ResponseError {
ResponseError::internal(err)
}
}
pub trait IntoInternalError<T> { pub trait IntoInternalError<T> {
fn into_internal_error(self) -> SResult<T>; fn into_internal_error(self) -> SResult<T>;
} }
/// Must be used only
impl<T> IntoInternalError<T> for Option<T> { impl<T> IntoInternalError<T> for Option<T> {
fn into_internal_error(self) -> SResult<T> { fn into_internal_error(self) -> SResult<T> {
match self { match self {

View File

@ -63,6 +63,12 @@ impl From<meilisearch_core::Error> for Error {
} }
} }
impl From<heed::Error> for Error {
fn from(error: heed::Error) -> Self {
Error::Internal(error.to_string())
}
}
pub trait IndexSearchExt { pub trait IndexSearchExt {
fn new_search(&self, query: String) -> SearchBuilder; fn new_search(&self, query: String) -> SearchBuilder;
} }
@ -171,7 +177,7 @@ impl<'a> SearchBuilder<'a> {
let ref_index = &self.index; let ref_index = &self.index;
let value = value.trim().to_lowercase(); let value = value.trim().to_lowercase();
let attr = match schema.get_id(attr) { let attr = match schema.id(attr) {
Some(attr) => attr, Some(attr) => attr,
None => return Err(Error::UnknownFilteredAttribute), None => return Err(Error::UnknownFilteredAttribute),
}; };
@ -271,7 +277,7 @@ impl<'a> SearchBuilder<'a> {
ranked_map: &'a RankedMap, ranked_map: &'a RankedMap,
schema: &Schema, schema: &Schema,
) -> Result<Option<Criteria<'a>>, Error> { ) -> Result<Option<Criteria<'a>>, Error> {
let ranking_rules = self.index.main.ranking_rules(reader).unwrap(); let ranking_rules = self.index.main.ranking_rules(reader)?;
if let Some(ranking_rules) = ranking_rules { if let Some(ranking_rules) = ranking_rules {
let mut builder = CriteriaBuilder::with_capacity(7 + ranking_rules.len()); let mut builder = CriteriaBuilder::with_capacity(7 + ranking_rules.len());
@ -283,10 +289,18 @@ impl<'a> SearchBuilder<'a> {
RankingRule::Attribute => builder.push(Attribute), RankingRule::Attribute => builder.push(Attribute),
RankingRule::WordsPosition => builder.push(WordsPosition), RankingRule::WordsPosition => builder.push(WordsPosition),
RankingRule::Exact => builder.push(Exact), RankingRule::Exact => builder.push(Exact),
RankingRule::Asc(field) => builder RankingRule::Asc(field) => {
.push(SortByAttr::lower_is_better(&ranked_map, &schema, &field).unwrap()), match SortByAttr::lower_is_better(&ranked_map, &schema, &field) {
RankingRule::Dsc(field) => builder Ok(rule) => builder.push(rule),
.push(SortByAttr::higher_is_better(&ranked_map, &schema, &field).unwrap()), Err(err) => error!("Error during criteria builder; {:?}", err),
}
}
RankingRule::Dsc(field) => {
match SortByAttr::higher_is_better(&ranked_map, &schema, &field) {
Ok(rule) => builder.push(rule),
Err(err) => error!("Error during criteria builder; {:?}", err),
}
}
}; };
} }
builder.push(DocumentId); builder.push(DocumentId);
@ -334,8 +348,6 @@ pub struct SearchResult {
pub limit: usize, pub limit: usize,
pub processing_time_ms: usize, pub processing_time_ms: usize,
pub query: String, pub query: String,
// pub parsed_query: String,
// pub params: Option<String>,
} }
fn crop_text( fn crop_text(
@ -369,7 +381,7 @@ fn crop_document(
matches.sort_unstable_by_key(|m| (m.char_index, m.char_length)); matches.sort_unstable_by_key(|m| (m.char_index, m.char_length));
for (field, length) in fields { for (field, length) in fields {
let attribute = match schema.get_id(field) { let attribute = match schema.id(field) {
Some(attribute) => attribute, Some(attribute) => attribute,
None => continue, None => continue,
}; };
@ -398,16 +410,16 @@ fn calculate_matches(
) -> MatchesInfos { ) -> MatchesInfos {
let mut matches_result: HashMap<String, Vec<MatchPosition>> = HashMap::new(); let mut matches_result: HashMap<String, Vec<MatchPosition>> = HashMap::new();
for m in matches.iter() { for m in matches.iter() {
if let Some(attribute) = schema.get_name(FieldId::new(m.attribute)) { if let Some(attribute) = schema.name(FieldId::new(m.attribute)) {
if let Some(attributes_to_retrieve) = attributes_to_retrieve.clone() { if let Some(attributes_to_retrieve) = attributes_to_retrieve.clone() {
if !attributes_to_retrieve.contains(attribute.as_str()) { if !attributes_to_retrieve.contains(attribute) {
continue; continue;
} }
}; };
if !schema.get_displayed_name().contains(attribute.as_str()) { if !schema.displayed_name().contains(attribute) {
continue; continue;
} }
if let Some(pos) = matches_result.get_mut(&attribute) { if let Some(pos) = matches_result.get_mut(attribute) {
pos.push(MatchPosition { pos.push(MatchPosition {
start: m.char_index as usize, start: m.char_index as usize,
length: m.char_length as usize, length: m.char_length as usize,
@ -418,7 +430,7 @@ fn calculate_matches(
start: m.char_index as usize, start: m.char_index as usize,
length: m.char_length as usize, length: m.char_length as usize,
}); });
matches_result.insert(attribute, positions); matches_result.insert(attribute.to_string(), positions);
} }
} }
} }

View File

@ -88,7 +88,7 @@ impl RequestExt for Request<Data> {
fn url_param(&self, name: &str) -> SResult<String> { fn url_param(&self, name: &str) -> SResult<String> {
let param = self let param = self
.param::<String>(name) .param::<String>(name)
.map_err(|_| ResponseError::bad_parameter("identifier", ""))?; .map_err(|_| ResponseError::bad_parameter("identifier", name))?;
Ok(param) Ok(param)
} }
@ -105,7 +105,7 @@ impl RequestExt for Request<Data> {
fn identifier(&self) -> SResult<String> { fn identifier(&self) -> SResult<String> {
let name = self let name = self
.param::<String>("identifier") .param::<String>("identifier")
.map_err(|_| ResponseError::bad_parameter("identifier", ""))?; .map_err(|_| ResponseError::bad_parameter("identifier", "identifier"))?;
Ok(name) Ok(name)
} }

View File

@ -5,17 +5,14 @@ use async_std::task;
use log::info; use log::info;
use main_error::MainError; use main_error::MainError;
use structopt::StructOpt; use structopt::StructOpt;
use tide::middleware::RequestLogger; use tide::middleware::{Cors, RequestLogger};
use meilisearch_http::data::Data; use meilisearch_http::data::Data;
use meilisearch_http::option::Opt; use meilisearch_http::option::Opt;
use meilisearch_http::routes; use meilisearch_http::routes;
use meilisearch_http::routes::index::index_update_callback; use meilisearch_http::routes::index::index_update_callback;
use cors::Cors;
mod analytics; mod analytics;
mod cors;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
#[global_allocator] #[global_allocator]
@ -40,15 +37,11 @@ pub fn main() -> Result<(), MainError> {
app.middleware(Cors::new()); app.middleware(Cors::new());
app.middleware(RequestLogger::new()); app.middleware(RequestLogger::new());
// app.middleware(tide_compression::Compression::new());
// app.middleware(tide_compression::Decompression::new());
routes::load_routes(&mut app); routes::load_routes(&mut app);
info!("Server HTTP enabled"); info!("Server HTTP enabled");
task::block_on(async { task::block_on(app.listen(opt.http_addr))?;
app.listen(opt.http_addr).await.unwrap();
});
Ok(()) Ok(())
} }

View File

@ -30,7 +30,7 @@ pub async fn get_document(ctx: Request<Data>) -> SResult<Response> {
return Err(ResponseError::document_not_found(identifier)); return Err(ResponseError::document_not_found(identifier));
} }
Ok(tide::Response::new(200).body_json(&response).unwrap()) Ok(tide::Response::new(200).body_json(&response)?)
} }
#[derive(Default, Serialize)] #[derive(Default, Serialize)]
@ -54,7 +54,7 @@ pub async fn delete_document(ctx: Request<Data>) -> SResult<Response> {
update_writer.commit()?; update_writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
#[derive(Default, Deserialize)] #[derive(Default, Deserialize)]
@ -106,7 +106,7 @@ pub async fn get_all_documents(ctx: Request<Data>) -> SResult<Response> {
} }
} }
Ok(tide::Response::new(200).body_json(&response_body).unwrap()) Ok(tide::Response::new(200).body_json(&response_body)?)
} }
fn find_identifier(document: &IndexMap<String, Value>) -> Option<String> { fn find_identifier(document: &IndexMap<String, Value>) -> Option<String> {
@ -146,10 +146,10 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
}, },
}; };
let settings = Settings { let settings = Settings {
attribute_identifier: Some(Some(id)), identifier: Some(Some(id)),
..Settings::default() ..Settings::default()
}; };
index.settings_update(&mut update_writer, settings.into())?; index.settings_update(&mut update_writer, settings.into_update()?)?;
} }
let mut document_addition = if is_partial { let mut document_addition = if is_partial {
@ -166,7 +166,7 @@ async fn update_multiple_documents(mut ctx: Request<Data>, is_partial: bool) ->
update_writer.commit()?; update_writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn add_or_replace_multiple_documents(ctx: Request<Data>) -> SResult<Response> { pub async fn add_or_replace_multiple_documents(ctx: Request<Data>) -> SResult<Response> {
@ -200,7 +200,7 @@ pub async fn delete_multiple_documents(mut ctx: Request<Data>) -> SResult<Respon
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn clear_all_documents(ctx: Request<Data>) -> SResult<Response> { pub async fn clear_all_documents(ctx: Request<Data>) -> SResult<Response> {
@ -215,5 +215,5 @@ pub async fn clear_all_documents(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -55,7 +55,7 @@ pub async fn list_indexes(ctx: Request<Data>) -> SResult<Response> {
} }
} }
Ok(tide::Response::new(200).body_json(&response_body).unwrap()) Ok(tide::Response::new(200).body_json(&response_body)?)
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -87,7 +87,7 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
updated_at, updated_at,
}; };
Ok(tide::Response::new(200).body_json(&response_body).unwrap()) Ok(tide::Response::new(200).body_json(&response_body)?)
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -95,7 +95,7 @@ pub async fn get_index(ctx: Request<Data>) -> SResult<Response> {
struct IndexCreateRequest { struct IndexCreateRequest {
name: Option<String>, name: Option<String>,
uid: Option<String>, uid: Option<String>,
attribute_identifier: Option<String>, identifier: Option<String>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -150,10 +150,10 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
.updated_at(&writer)? .updated_at(&writer)?
.into_internal_error()?; .into_internal_error()?;
if let Some(id) = body.attribute_identifier { if let Some(id) = body.identifier {
created_index created_index
.main .main
.put_schema(&mut writer, &Schema::with_identifier(id))?; .put_schema(&mut writer, &Schema::with_identifier(&id))?;
} }
writer.commit()?; writer.commit()?;
@ -165,7 +165,7 @@ pub async fn create_index(mut ctx: Request<Data>) -> SResult<Response> {
updated_at, updated_at,
}; };
Ok(tide::Response::new(201).body_json(&response_body).unwrap()) Ok(tide::Response::new(201).body_json(&response_body)?)
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -214,7 +214,7 @@ pub async fn update_index(mut ctx: Request<Data>) -> SResult<Response> {
updated_at, updated_at,
}; };
Ok(tide::Response::new(200).body_json(&response_body).unwrap()) Ok(tide::Response::new(200).body_json(&response_body)?)
} }
pub async fn get_update_status(ctx: Request<Data>) -> SResult<Response> { pub async fn get_update_status(ctx: Request<Data>) -> SResult<Response> {

View File

@ -23,175 +23,131 @@ async fn into_response<T: IntoResponse, U: IntoResponse>(
} }
pub fn load_routes(app: &mut tide::Server<Data>) { pub fn load_routes(app: &mut tide::Server<Data>) {
app.at("").nest(|router| { app.at("/").get(|_| {
// expose the web interface static files async move {
router.at("/").get(|_| { tide::Response::new(200)
async move { .body_string(include_str!("../../public/interface.html").to_string())
let response = include_str!("../../public/interface.html"); .set_mime(mime::TEXT_HTML_UTF_8)
response }
} });
}); app.at("/bulma.min.css").get(|_| {
router.at("/bulma.min.css").get(|_| { async {
async { tide::Response::new(200)
let response = include_str!("../../public/bulma.min.css"); .body_string(include_str!("../../public/bulma.min.css").to_string())
response .set_mime(mime::TEXT_CSS_UTF_8)
} }
});
router.at("/indexes").nest(|router| {
router
.at("/")
.get(|ctx| into_response(index::list_indexes(ctx)))
.post(|ctx| into_response(index::create_index(ctx)));
router
.at("/search")
.post(|ctx| into_response(search::search_multi_index(ctx)));
router.at("/:index").nest(|router| {
router
.at("/search")
.get(|ctx| into_response(search::search_with_url_query(ctx)));
router.at("/updates").nest(|router| {
router
.at("/")
.get(|ctx| into_response(index::get_all_updates_status(ctx)));
router
.at("/:update_id")
.get(|ctx| into_response(index::get_update_status(ctx)));
});
router
.at("/")
.get(|ctx| into_response(index::get_index(ctx)))
.put(|ctx| into_response(index::update_index(ctx)))
.delete(|ctx| into_response(index::delete_index(ctx)));
router.at("/documents").nest(|router| {
router
.at("/")
.get(|ctx| into_response(document::get_all_documents(ctx)))
.post(|ctx| into_response(document::add_or_replace_multiple_documents(ctx)))
.put(|ctx| into_response(document::add_or_update_multiple_documents(ctx)))
.delete(|ctx| into_response(document::clear_all_documents(ctx)));
router.at("/:identifier").nest(|router| {
router
.at("/")
.get(|ctx| into_response(document::get_document(ctx)))
.delete(|ctx| into_response(document::delete_document(ctx)));
});
router
.at("/delete-batch")
.post(|ctx| into_response(document::delete_multiple_documents(ctx)));
});
router.at("/settings").nest(|router| {
router
.get(|ctx| into_response(setting::get_all(ctx)))
.post(|ctx| into_response(setting::update_all(ctx)))
.delete(|ctx| into_response(setting::delete_all(ctx)));
router.at("/ranking").nest(|router| {
router
.get(|ctx| into_response(setting::get_ranking(ctx)))
.post(|ctx| into_response(setting::update_ranking(ctx)))
.delete(|ctx| into_response(setting::delete_ranking(ctx)));
router
.at("/rules")
.get(|ctx| into_response(setting::get_rules(ctx)))
.post(|ctx| into_response(setting::update_rules(ctx)))
.delete(|ctx| into_response(setting::delete_rules(ctx)));
router
.at("/distinct")
.get(|ctx| into_response(setting::get_distinct(ctx)))
.post(|ctx| into_response(setting::update_distinct(ctx)))
.delete(|ctx| into_response(setting::delete_distinct(ctx)));
});
router.at("/attributes").nest(|router| {
router
.get(|ctx| into_response(setting::get_attributes(ctx)))
.post(|ctx| into_response(setting::update_attributes(ctx)))
.delete(|ctx| into_response(setting::delete_attributes(ctx)));
router
.at("/identifier")
.get(|ctx| into_response(setting::get_identifier(ctx)));
router
.at("/searchable")
.get(|ctx| into_response(setting::get_searchable(ctx)))
.post(|ctx| into_response(setting::update_searchable(ctx)))
.delete(|ctx| into_response(setting::delete_searchable(ctx)));
router
.at("/displayed")
.get(|ctx| into_response(setting::get_displayed(ctx)))
.post(|ctx| into_response(setting::update_displayed(ctx)))
.delete(|ctx| into_response(setting::delete_displayed(ctx)));
});
router
.at("/index-new-fields")
.get(|ctx| into_response(setting::get_index_new_fields(ctx)))
.post(|ctx| into_response(setting::update_index_new_fields(ctx)));
router
.at("/synonyms")
.get(|ctx| into_response(synonym::get(ctx)))
.post(|ctx| into_response(synonym::update(ctx)))
.delete(|ctx| into_response(synonym::delete(ctx)));
router
.at("/stop-words")
.get(|ctx| into_response(stop_words::get(ctx)))
.post(|ctx| into_response(stop_words::update(ctx)))
.delete(|ctx| into_response(stop_words::delete(ctx)));
});
router
.at("/stats")
.get(|ctx| into_response(stats::index_stat(ctx)));
});
});
router.at("/keys").nest(|router| {
router
.at("/")
.get(|ctx| into_response(key::list(ctx)))
.post(|ctx| into_response(key::create(ctx)));
router
.at("/:key")
.get(|ctx| into_response(key::get(ctx)))
.put(|ctx| into_response(key::update(ctx)))
.delete(|ctx| into_response(key::delete(ctx)));
});
}); });
app.at("").nest(|router| { app.at("/indexes/")
router .get(|ctx| into_response(index::list_indexes(ctx)))
.at("/health") .post(|ctx| into_response(index::create_index(ctx)));
.get(|ctx| into_response(health::get_health(ctx)))
.put(|ctx| into_response(health::change_healthyness(ctx)));
router app.at("/indexes/search")
.at("/stats") .post(|ctx| into_response(search::search_multi_index(ctx)));
.get(|ctx| into_response(stats::get_stats(ctx)));
router app.at("/indexes/:index")
.at("/version") .get(|ctx| into_response(index::get_index(ctx)))
.get(|ctx| into_response(stats::get_version(ctx))); .put(|ctx| into_response(index::update_index(ctx)))
router .delete(|ctx| into_response(index::delete_index(ctx)));
.at("/sys-info")
.get(|ctx| into_response(stats::get_sys_info(ctx))); app.at("/indexes/:index/search")
router .get(|ctx| into_response(search::search_with_url_query(ctx)));
.at("/sys-info/pretty")
.get(|ctx| into_response(stats::get_sys_info_pretty(ctx))); app.at("/indexes/:index/updates")
}); .get(|ctx| into_response(index::get_all_updates_status(ctx)));
app.at("/indexes/:index/updates/:update_id")
.get(|ctx| into_response(index::get_update_status(ctx)));
app.at("/indexes/:index/documents")
.get(|ctx| into_response(document::get_all_documents(ctx)))
.post(|ctx| into_response(document::add_or_replace_multiple_documents(ctx)))
.put(|ctx| into_response(document::add_or_update_multiple_documents(ctx)))
.delete(|ctx| into_response(document::clear_all_documents(ctx)));
app.at("/indexes/:index/documents/:identifier")
.get(|ctx| into_response(document::get_document(ctx)))
.delete(|ctx| into_response(document::delete_document(ctx)));
app.at("/indexes/:index/documents/:identifier/delete-batch")
.post(|ctx| into_response(document::delete_multiple_documents(ctx)));
app.at("/indexes/:index/settings")
.get(|ctx| into_response(setting::get_all(ctx)))
.post(|ctx| into_response(setting::update_all(ctx)))
.delete(|ctx| into_response(setting::delete_all(ctx)));
app.at("/indexes/:index/settings/ranking")
.get(|ctx| into_response(setting::get_ranking(ctx)))
.post(|ctx| into_response(setting::update_ranking(ctx)))
.delete(|ctx| into_response(setting::delete_ranking(ctx)));
app.at("/indexes/:index/settings/ranking/rules")
.get(|ctx| into_response(setting::get_rules(ctx)))
.post(|ctx| into_response(setting::update_rules(ctx)))
.delete(|ctx| into_response(setting::delete_rules(ctx)));
app.at("/indexes/:index/settings/ranking/distinct")
.get(|ctx| into_response(setting::get_distinct(ctx)))
.post(|ctx| into_response(setting::update_distinct(ctx)))
.delete(|ctx| into_response(setting::delete_distinct(ctx)));
app.at("/indexes/:index/settings/attributes")
.get(|ctx| into_response(setting::get_attributes(ctx)))
.post(|ctx| into_response(setting::update_attributes(ctx)))
.delete(|ctx| into_response(setting::delete_attributes(ctx)));
app.at("/indexes/:index/settings/attributes/identifier")
.get(|ctx| into_response(setting::get_identifier(ctx)));
app.at("/indexes/:index/settings/attributes/searchable")
.get(|ctx| into_response(setting::get_searchable(ctx)))
.post(|ctx| into_response(setting::update_searchable(ctx)))
.delete(|ctx| into_response(setting::delete_searchable(ctx)));
app.at("/indexes/:index/settings/attributes/displayed")
.get(|ctx| into_response(setting::displayed(ctx)))
.post(|ctx| into_response(setting::update_displayed(ctx)))
.delete(|ctx| into_response(setting::delete_displayed(ctx)));
app.at("/indexes/:index/settings/index-new-field")
.get(|ctx| into_response(setting::get_index_new_fields(ctx)))
.post(|ctx| into_response(setting::update_index_new_fields(ctx)));
app.at("/indexes/:index/settings/synonyms")
.get(|ctx| into_response(synonym::get(ctx)))
.post(|ctx| into_response(synonym::update(ctx)))
.delete(|ctx| into_response(synonym::delete(ctx)));
app.at("/indexes/:index/settings/stop_words")
.get(|ctx| into_response(stop_words::get(ctx)))
.post(|ctx| into_response(stop_words::update(ctx)))
.delete(|ctx| into_response(stop_words::delete(ctx)));
app.at("/indexes/:index/stats")
.get(|ctx| into_response(stats::index_stat(ctx)));
app.at("/keys/")
.get(|ctx| into_response(key::list(ctx)))
.post(|ctx| into_response(key::create(ctx)));
app.at("/keys/:key")
.get(|ctx| into_response(key::get(ctx)))
.put(|ctx| into_response(key::update(ctx)))
.delete(|ctx| into_response(key::delete(ctx)));
app.at("/health")
.get(|ctx| into_response(health::get_health(ctx)))
.put(|ctx| into_response(health::change_healthyness(ctx)));
app.at("/stats")
.get(|ctx| into_response(stats::get_stats(ctx)));
app.at("/version")
.get(|ctx| into_response(stats::get_version(ctx)));
app.at("/sys-info")
.get(|ctx| into_response(stats::get_sys_info(ctx)));
app.at("/sys-info/pretty")
.get(|ctx| into_response(stats::get_sys_info_pretty(ctx)));
} }

View File

@ -62,7 +62,7 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
let crop_length = query.crop_length.unwrap_or(200); let crop_length = query.crop_length.unwrap_or(200);
if attributes_to_crop == "*" { if attributes_to_crop == "*" {
let attributes_to_crop = schema let attributes_to_crop = schema
.get_displayed_name() .displayed_name()
.iter() .iter()
.map(|attr| (attr.to_string(), crop_length)) .map(|attr| (attr.to_string(), crop_length))
.collect(); .collect();
@ -78,11 +78,15 @@ pub async fn search_with_url_query(ctx: Request<Data>) -> SResult<Response> {
if let Some(attributes_to_highlight) = query.attributes_to_highlight { if let Some(attributes_to_highlight) = query.attributes_to_highlight {
let attributes_to_highlight = if attributes_to_highlight == "*" { let attributes_to_highlight = if attributes_to_highlight == "*" {
schema.get_displayed_name() schema
.displayed_name()
.iter()
.map(|s| s.to_string())
.collect()
} else { } else {
attributes_to_highlight attributes_to_highlight
.split(',') .split(',')
.map(ToString::to_string) .map(|s| s.to_string())
.collect() .collect()
}; };

View File

@ -18,7 +18,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let stop_words_fst = index.main.stop_words_fst(&reader)?; let stop_words_fst = index.main.stop_words_fst(&reader)?;
let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?;
let stop_words: BTreeSet<String> = stop_words.into_iter().collect(); let stop_words: BTreeSet<String> = stop_words.into_iter().collect();
let stop_words = if stop_words.len() > 0 { let stop_words = if stop_words.is_empty() {
Some(stop_words) Some(stop_words)
} else { } else {
None None
@ -40,7 +40,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
} }
} }
let synonyms = if synonyms.len() > 0 { let synonyms = if synonyms.is_empty() {
Some(synonyms) Some(synonyms)
} else { } else {
None None
@ -54,17 +54,21 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attribute_identifier = schema.clone().map(|s| s.identifier()); let identifier = schema.clone().map(|s| s.identifier().to_owned());
let attributes_searchable = schema.clone().map(|s| s.get_indexed_name()); let searchable_attributes = schema
let attributes_displayed = schema.clone().map(|s| s.get_displayed_name()); .clone()
let index_new_fields = schema.map(|s| s.must_index_new_fields()); .map(|s| s.indexed_name().iter().map(|s| s.to_string()).collect());
let displayed_attributes = schema
.clone()
.map(|s| s.displayed_name().iter().map(|s| s.to_string()).collect());
let index_new_fields = schema.map(|s| s.index_new_fields());
let settings = Settings { let settings = Settings {
ranking_rules: Some(ranking_rules), ranking_rules: Some(ranking_rules),
ranking_distinct: Some(ranking_distinct), ranking_distinct: Some(ranking_distinct),
attribute_identifier: Some(attribute_identifier), identifier: Some(identifier),
attributes_searchable: Some(attributes_searchable), searchable_attributes: Some(searchable_attributes),
attributes_displayed: Some(attributes_displayed), displayed_attributes: Some(displayed_attributes),
stop_words: Some(stop_words), stop_words: Some(stop_words),
synonyms: Some(synonyms), synonyms: Some(synonyms),
index_new_fields: Some(index_new_fields), index_new_fields: Some(index_new_fields),
@ -78,9 +82,9 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
pub struct UpdateSettings { pub struct UpdateSettings {
pub ranking_rules: Option<Vec<String>>, pub ranking_rules: Option<Vec<String>>,
pub ranking_distinct: Option<String>, pub ranking_distinct: Option<String>,
pub attribute_identifier: Option<String>, pub identifier: Option<String>,
pub attributes_searchable: Option<Vec<String>>, pub searchable_attributes: Option<Vec<String>>,
pub attributes_displayed: Option<HashSet<String>>, pub displayed_attributes: Option<HashSet<String>>,
pub stop_words: Option<BTreeSet<String>>, pub stop_words: Option<BTreeSet<String>>,
pub synonyms: Option<BTreeMap<String, Vec<String>>>, pub synonyms: Option<BTreeMap<String, Vec<String>>>,
pub index_new_fields: Option<bool>, pub index_new_fields: Option<bool>,
@ -96,20 +100,20 @@ pub async fn update_all(mut ctx: Request<Data>) -> SResult<Response> {
let settings = Settings { let settings = Settings {
ranking_rules: Some(settings_update.ranking_rules), ranking_rules: Some(settings_update.ranking_rules),
ranking_distinct: Some(settings_update.ranking_distinct), ranking_distinct: Some(settings_update.ranking_distinct),
attribute_identifier: Some(settings_update.attribute_identifier), identifier: Some(settings_update.identifier),
attributes_searchable: Some(settings_update.attributes_searchable), searchable_attributes: Some(settings_update.searchable_attributes),
attributes_displayed: Some(settings_update.attributes_displayed), displayed_attributes: Some(settings_update.displayed_attributes),
stop_words: Some(settings_update.stop_words), stop_words: Some(settings_update.stop_words),
synonyms: Some(settings_update.synonyms), synonyms: Some(settings_update.synonyms),
index_new_fields: Some(settings_update.index_new_fields), index_new_fields: Some(settings_update.index_new_fields),
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
@ -121,9 +125,9 @@ pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
let settings = SettingsUpdate { let settings = SettingsUpdate {
ranking_rules: UpdateState::Clear, ranking_rules: UpdateState::Clear,
ranking_distinct: UpdateState::Clear, ranking_distinct: UpdateState::Clear,
attribute_identifier: UpdateState::Clear, identifier: UpdateState::Clear,
attributes_searchable: UpdateState::Clear, searchable_attributes: UpdateState::Clear,
attributes_displayed: UpdateState::Clear, displayed_attributes: UpdateState::Clear,
stop_words: UpdateState::Clear, stop_words: UpdateState::Clear,
synonyms: UpdateState::Clear, synonyms: UpdateState::Clear,
index_new_fields: UpdateState::Clear, index_new_fields: UpdateState::Clear,
@ -134,12 +138,12 @@ pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetRankingSettings { pub struct RankingSettings {
pub ranking_rules: Option<Vec<String>>, pub ranking_rules: Option<Vec<String>>,
pub ranking_distinct: Option<String>, pub ranking_distinct: Option<String>,
} }
@ -156,7 +160,7 @@ pub async fn get_ranking(ctx: Request<Data>) -> SResult<Response> {
}; };
let ranking_distinct = index.main.ranking_distinct(&reader)?; let ranking_distinct = index.main.ranking_distinct(&reader)?;
let settings = GetRankingSettings { let settings = RankingSettings {
ranking_rules, ranking_rules,
ranking_distinct, ranking_distinct,
}; };
@ -164,17 +168,10 @@ pub async fn get_ranking(ctx: Request<Data>) -> SResult<Response> {
Ok(tide::Response::new(200).body_json(&settings).unwrap()) Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetRankingSettings {
pub ranking_rules: Option<Vec<String>>,
pub ranking_distinct: Option<String>,
}
pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: SetRankingSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?; let settings: RankingSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
@ -184,11 +181,11 @@ pub async fn update_ranking(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_ranking(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_ranking(ctx: Request<Data>) -> SResult<Response> {
@ -208,7 +205,7 @@ pub async fn delete_ranking(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> { pub async fn get_rules(ctx: Request<Data>) -> SResult<Response> {
@ -238,11 +235,11 @@ pub async fn update_rules(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
@ -261,13 +258,7 @@ pub async fn delete_rules(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
}
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetRankingDistinctSettings {
pub ranking_distinct: Option<String>,
} }
pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> { pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
@ -283,12 +274,6 @@ pub async fn get_distinct(ctx: Request<Data>) -> SResult<Response> {
.unwrap()) .unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetRankingDistinctSettings {
pub ranking_distinct: Option<String>,
}
pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
@ -302,11 +287,11 @@ pub async fn update_distinct(mut ctx: Request<Data>) -> SResult<Response> {
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
@ -325,15 +310,15 @@ pub async fn delete_distinct(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
#[derive(Default, Clone, Serialize, Deserialize)] #[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)] #[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetAttributesSettings { pub struct AttributesSettings {
pub attribute_identifier: Option<String>, pub identifier: Option<String>,
pub attributes_searchable: Option<Vec<String>>, pub searchable_attributes: Option<Vec<String>>,
pub attributes_displayed: Option<HashSet<String>>, pub displayed_attributes: Option<HashSet<String>>,
} }
pub async fn get_attributes(ctx: Request<Data>) -> SResult<Response> { pub async fn get_attributes(ctx: Request<Data>) -> SResult<Response> {
@ -344,47 +329,42 @@ pub async fn get_attributes(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attribute_identifier = schema.clone().map(|s| s.identifier()); let identifier = schema.clone().map(|s| s.identifier().to_string());
let attributes_searchable = schema.clone().map(|s| s.get_indexed_name()); let searchable_attributes = schema
let attributes_displayed = schema.clone().map(|s| s.get_displayed_name()); .clone()
.map(|s| s.indexed_name().iter().map(|s| s.to_string()).collect());
let displayed_attributes = schema
.clone()
.map(|s| s.displayed_name().iter().map(|s| s.to_string()).collect());
let settings = GetAttributesSettings { let settings = AttributesSettings {
attribute_identifier, identifier,
attributes_searchable, searchable_attributes,
attributes_displayed, displayed_attributes,
}; };
Ok(tide::Response::new(200).body_json(&settings).unwrap()) Ok(tide::Response::new(200).body_json(&settings).unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetAttributesSettings {
pub attribute_identifier: Option<String>,
pub attributes_searchable: Option<Vec<String>>,
pub attributes_displayed: Option<HashSet<String>>,
}
pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_attributes(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let settings: SetAttributesSettings = let settings: AttributesSettings = ctx.body_json().await.map_err(ResponseError::bad_request)?;
ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attribute_identifier: Some(settings.attribute_identifier), identifier: Some(settings.identifier),
attributes_searchable: Some(settings.attributes_searchable), searchable_attributes: Some(settings.searchable_attributes),
attributes_displayed: Some(settings.attributes_displayed), displayed_attributes: Some(settings.displayed_attributes),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> {
@ -393,8 +373,8 @@ pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = SettingsUpdate { let settings = SettingsUpdate {
attributes_searchable: UpdateState::Clear, searchable_attributes: UpdateState::Clear,
attributes_displayed: UpdateState::Clear, displayed_attributes: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
@ -403,7 +383,7 @@ pub async fn delete_attributes(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> { pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> {
@ -414,11 +394,9 @@ pub async fn get_identifier(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attribute_identifier = schema.map(|s| s.identifier()); let identifier = schema.map(|s| s.identifier().to_string());
Ok(tide::Response::new(200) Ok(tide::Response::new(200).body_json(&identifier).unwrap())
.body_json(&attribute_identifier)
.unwrap())
} }
pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> { pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
@ -429,37 +407,32 @@ pub async fn get_searchable(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attributes_searchable = schema.map(|s| s.get_indexed_name()); let searchable_attributes: Option<HashSet<String>> =
schema.map(|s| s.indexed_name().iter().map(|i| i.to_string()).collect());
Ok(tide::Response::new(200) Ok(tide::Response::new(200)
.body_json(&attributes_searchable) .body_json(&searchable_attributes)
.unwrap()) .unwrap())
} }
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SetAttributesSearchableSettings {
pub attributes_searchable: Option<Vec<String>>,
}
pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_searchable(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let attributes_searchable: Option<Vec<String>> = let searchable_attributes: Option<Vec<String>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_searchable: Some(attributes_searchable), searchable_attributes: Some(searchable_attributes),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
@ -468,7 +441,7 @@ pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = SettingsUpdate { let settings = SettingsUpdate {
attributes_searchable: UpdateState::Clear, searchable_attributes: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
@ -477,10 +450,10 @@ pub async fn delete_searchable(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> { pub async fn displayed(ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsRead)?; ctx.is_allowed(SettingsRead)?;
let index = ctx.index()?; let index = ctx.index()?;
let db = &ctx.state().db; let db = &ctx.state().db;
@ -488,31 +461,32 @@ pub async fn get_displayed(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let attributes_displayed = schema.map(|s| s.get_displayed_name()); let displayed_attributes: Option<HashSet<String>> =
schema.map(|s| s.displayed_name().iter().map(|i| i.to_string()).collect());
Ok(tide::Response::new(200) Ok(tide::Response::new(200)
.body_json(&attributes_displayed) .body_json(&displayed_attributes)
.unwrap()) .unwrap())
} }
pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> { pub async fn update_displayed(mut ctx: Request<Data>) -> SResult<Response> {
ctx.is_allowed(SettingsWrite)?; ctx.is_allowed(SettingsWrite)?;
let index = ctx.index()?; let index = ctx.index()?;
let attributes_displayed: Option<HashSet<String>> = let displayed_attributes: Option<HashSet<String>> =
ctx.body_json().await.map_err(ResponseError::bad_request)?; ctx.body_json().await.map_err(ResponseError::bad_request)?;
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = Settings { let settings = Settings {
attributes_displayed: Some(attributes_displayed), displayed_attributes: Some(displayed_attributes),
..Settings::default() ..Settings::default()
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> { pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
@ -521,7 +495,7 @@ pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
let db = &ctx.state().db; let db = &ctx.state().db;
let settings = SettingsUpdate { let settings = SettingsUpdate {
attributes_displayed: UpdateState::Clear, displayed_attributes: UpdateState::Clear,
..SettingsUpdate::default() ..SettingsUpdate::default()
}; };
@ -530,7 +504,7 @@ pub async fn delete_displayed(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> { pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> {
@ -541,7 +515,7 @@ pub async fn get_index_new_fields(ctx: Request<Data>) -> SResult<Response> {
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let index_new_fields = schema.map(|s| s.must_index_new_fields()); let index_new_fields = schema.map(|s| s.index_new_fields());
Ok(tide::Response::new(200) Ok(tide::Response::new(200)
.body_json(&index_new_fields) .body_json(&index_new_fields)
@ -561,9 +535,9 @@ pub async fn update_index_new_fields(mut ctx: Request<Data>) -> SResult<Response
}; };
let mut writer = db.update_write_txn()?; let mut writer = db.update_write_txn()?;
let update_id = index.settings_update(&mut writer, settings.into())?; let update_id = index.settings_update(&mut writer, settings.into_update()?)?;
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -39,7 +39,7 @@ pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
@ -59,5 +59,5 @@ pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -57,7 +57,7 @@ pub async fn update(mut ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }
pub async fn delete(ctx: Request<Data>) -> SResult<Response> { pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
@ -78,5 +78,5 @@ pub async fn delete(ctx: Request<Data>) -> SResult<Response> {
writer.commit()?; writer.commit()?;
let response_body = IndexUpdateResponse { update_id }; let response_body = IndexUpdateResponse { update_id };
Ok(tide::Response::new(202).body_json(&response_body).unwrap()) Ok(tide::Response::new(202).body_json(&response_body)?)
} }

View File

@ -1,4 +1,3 @@
#![allow(dead_code)]
use serde_json::Value; use serde_json::Value;
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
@ -64,8 +63,8 @@ pub fn enrich_server_with_movies_settings(
"dsc(vote_average)", "dsc(vote_average)",
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -75,7 +74,7 @@ pub fn enrich_server_with_movies_settings(
"production_companies", "production_companies",
"genres", "genres",
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",

View File

@ -3,7 +3,6 @@ use async_std::task::block_on;
use http_service::Body; use http_service::Body;
use serde_json::json; use serde_json::json;
use serde_json::Value; use serde_json::Value;
use std::convert::Into;
mod common; mod common;

View File

@ -636,8 +636,8 @@ fn search_with_settings_basic() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -647,7 +647,7 @@ fn search_with_settings_basic() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",
@ -742,8 +742,8 @@ fn search_with_settings_stop_words() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -753,7 +753,7 @@ fn search_with_settings_stop_words() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",
@ -849,8 +849,8 @@ fn search_with_settings_synonyms() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -860,7 +860,7 @@ fn search_with_settings_synonyms() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",
@ -961,8 +961,8 @@ fn search_with_settings_ranking_rules() {
"dsc(popularity)" "dsc(popularity)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -972,7 +972,7 @@ fn search_with_settings_ranking_rules() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",
@ -1052,7 +1052,7 @@ fn search_with_settings_ranking_rules() {
} }
#[test] #[test]
fn search_with_settings_attributes_searchable() { fn search_with_settings_searchable_attributes() {
let mut server = common::setup_server().unwrap(); let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap(); common::enrich_server_with_movies_index(&mut server).unwrap();
@ -1068,8 +1068,8 @@ fn search_with_settings_attributes_searchable() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"tagline", "tagline",
"overview", "overview",
"cast", "cast",
@ -1078,7 +1078,7 @@ fn search_with_settings_attributes_searchable() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"director", "director",
"producer", "producer",
@ -1158,7 +1158,7 @@ fn search_with_settings_attributes_searchable() {
} }
#[test] #[test]
fn search_with_settings_attributes_displayed() { fn search_with_settings_displayed_attributes() {
let mut server = common::setup_server().unwrap(); let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap(); common::enrich_server_with_movies_index(&mut server).unwrap();
@ -1174,8 +1174,8 @@ fn search_with_settings_attributes_displayed() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"tagline", "tagline",
"overview", "overview",
@ -1185,7 +1185,7 @@ fn search_with_settings_attributes_displayed() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"tagline", "tagline",
"id", "id",
@ -1229,7 +1229,7 @@ fn search_with_settings_attributes_displayed() {
} }
#[test] #[test]
fn search_with_settings_attributes_searchable_2() { fn search_with_settings_searchable_attributes_2() {
let mut server = common::setup_server().unwrap(); let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap(); common::enrich_server_with_movies_index(&mut server).unwrap();
@ -1245,8 +1245,8 @@ fn search_with_settings_attributes_searchable_2() {
"dsc(vote_average)" "dsc(vote_average)"
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "id", "identifier": "id",
"attributesSearchable": [ "searchableAttributes": [
"tagline", "tagline",
"overview", "overview",
"title", "title",
@ -1256,7 +1256,7 @@ fn search_with_settings_attributes_searchable_2() {
"production_companies", "production_companies",
"genres" "genres"
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"tagline", "tagline",
"id", "id",

View File

@ -50,8 +50,8 @@ fn write_all_and_delete() {
"dsc(rank)", "dsc(rank)",
], ],
"rankingDistinct": "movie_id", "rankingDistinct": "movie_id",
"attributeIdentifier": "uid", "identifier": "uid",
"attributesSearchable": [ "searchableAttributes": [
"uid", "uid",
"movie_id", "movie_id",
"title", "title",
@ -60,7 +60,7 @@ fn write_all_and_delete() {
"release_date", "release_date",
"rank", "rank",
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"description", "description",
"poster", "poster",
@ -128,9 +128,9 @@ fn write_all_and_delete() {
let json = json!({ let json = json!({
"rankingRules": null, "rankingRules": null,
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": null, "identifier": null,
"attributesSearchable": null, "searchableAttributes": null,
"attributesDisplayed": null, "displayedAttributes": null,
"stopWords": null, "stopWords": null,
"synonyms": null, "synonyms": null,
"indexNewFields": null, "indexNewFields": null,
@ -179,8 +179,8 @@ fn write_all_and_update() {
"dsc(rank)", "dsc(rank)",
], ],
"rankingDistinct": "movie_id", "rankingDistinct": "movie_id",
"attributeIdentifier": "uid", "identifier": "uid",
"attributesSearchable": [ "searchableAttributes": [
"uid", "uid",
"movie_id", "movie_id",
"title", "title",
@ -189,7 +189,7 @@ fn write_all_and_update() {
"release_date", "release_date",
"rank", "rank",
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"description", "description",
"poster", "poster",
@ -244,13 +244,13 @@ fn write_all_and_update() {
"_exact", "_exact",
"dsc(release_date)", "dsc(release_date)",
], ],
"attributeIdentifier": "uid", "identifier": "uid",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"description", "description",
"uid", "uid",
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"description", "description",
"release_date", "release_date",
@ -299,13 +299,13 @@ fn write_all_and_update() {
"dsc(release_date)", "dsc(release_date)",
], ],
"rankingDistinct": null, "rankingDistinct": null,
"attributeIdentifier": "uid", "identifier": "uid",
"attributesSearchable": [ "searchableAttributes": [
"title", "title",
"description", "description",
"uid", "uid",
], ],
"attributesDisplayed": [ "displayedAttributes": [
"title", "title",
"description", "description",
"release_date", "release_date",

View File

@ -1,4 +1,3 @@
use std::convert::Into;
use std::time::Duration; use std::time::Duration;
use assert_json_diff::assert_json_eq; use assert_json_diff::assert_json_eq;
@ -26,7 +25,7 @@ fn write_all_and_delete() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"attributeIdentifier": "uid", "identifier": "uid",
}) })
.to_string() .to_string()
.into_bytes(); .into_bytes();
@ -123,7 +122,7 @@ fn write_all_and_update() {
let body = json!({ let body = json!({
"uid": "movies", "uid": "movies",
"attributeIdentifier": "uid", "identifier": "uid",
}) })
.to_string() .to_string()
.into_bytes(); .into_bytes();

View File

@ -13,8 +13,8 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Error::*; use self::Error::*;
match self { match self {
FieldNameNotFound(field) => write!(f, "The field {} doesn't exist", field), FieldNameNotFound(field) => write!(f, "The field {:?} doesn't exist", field),
MaxFieldsLimitExceeded => write!(f, "The maximum of possible reatributed field id has been reached"), MaxFieldsLimitExceeded => write!(f, "The maximum of possible reattributed field id has been reached"),
} }
} }
} }

View File

@ -1,11 +1,9 @@
use std::io::{Read, Write};
use std::collections::HashMap; use std::collections::HashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{SResult, FieldId}; use crate::{SResult, FieldId};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FieldsMap { pub struct FieldsMap {
name_map: HashMap<String, FieldId>, name_map: HashMap<String, FieldId>,
@ -22,41 +20,30 @@ impl FieldsMap {
self.name_map.is_empty() self.name_map.is_empty()
} }
pub fn insert<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> { pub fn insert(&mut self, name: &str) -> SResult<FieldId> {
let name = name.into(); if let Some(id) = self.name_map.get(name) {
if let Some(id) = self.name_map.get(&name) {
return Ok(*id) return Ok(*id)
} }
let id = self.next_id.into(); let id = self.next_id.into();
self.next_id = self.next_id.next()?; self.next_id = self.next_id.next()?;
self.name_map.insert(name.clone(), id); self.name_map.insert(name.to_string(), id);
self.id_map.insert(id, name); self.id_map.insert(id, name.to_string());
Ok(id) Ok(id)
} }
pub fn remove<S: Into<String>>(&mut self, name: S) { pub fn remove(&mut self, name: &str) {
let name = name.into(); if let Some(id) = self.name_map.get(name) {
if let Some(id) = self.name_map.get(&name) {
self.id_map.remove(&id); self.id_map.remove(&id);
} }
self.name_map.remove(&name); self.name_map.remove(name);
} }
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> { pub fn id(&self, name: &str) -> Option<FieldId> {
let name = name.into(); self.name_map.get(name).map(|s| *s)
self.name_map.get(&name).map(|s| *s)
} }
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> { pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.id_map.get(&id.into()).map(|s| s.to_string()) self.id_map.get(&id.into()).map(|s| s.as_str())
}
pub fn read_from_bin<R: Read>(reader: R) -> bincode::Result<FieldsMap> {
bincode::deserialize_from(reader)
}
pub fn write_to_bin<W: Write>(&self, writer: W) -> bincode::Result<()> {
bincode::serialize_into(writer, &self)
} }
} }
@ -73,17 +60,17 @@ mod tests {
assert_eq!(fields_map.insert("id").unwrap(), 0.into()); assert_eq!(fields_map.insert("id").unwrap(), 0.into());
assert_eq!(fields_map.insert("title").unwrap(), 1.into()); assert_eq!(fields_map.insert("title").unwrap(), 1.into());
assert_eq!(fields_map.insert("descritpion").unwrap(), 2.into()); assert_eq!(fields_map.insert("descritpion").unwrap(), 2.into());
assert_eq!(fields_map.get_id("id"), Some(0.into())); assert_eq!(fields_map.id("id"), Some(0.into()));
assert_eq!(fields_map.get_id("title"), Some(1.into())); assert_eq!(fields_map.id("title"), Some(1.into()));
assert_eq!(fields_map.get_id("descritpion"), Some(2.into())); assert_eq!(fields_map.id("descritpion"), Some(2.into()));
assert_eq!(fields_map.get_id("date"), None); assert_eq!(fields_map.id("date"), None);
assert_eq!(fields_map.len(), 3); assert_eq!(fields_map.len(), 3);
assert_eq!(fields_map.get_name(0), Some("id".to_owned())); assert_eq!(fields_map.name(0), Some("id"));
assert_eq!(fields_map.get_name(1), Some("title".to_owned())); assert_eq!(fields_map.name(1), Some("title"));
assert_eq!(fields_map.get_name(2), Some("descritpion".to_owned())); assert_eq!(fields_map.name(2), Some("descritpion"));
assert_eq!(fields_map.get_name(4), None); assert_eq!(fields_map.name(4), None);
fields_map.remove("title"); fields_map.remove("title");
assert_eq!(fields_map.get_id("title"), None); assert_eq!(fields_map.id("title"), None);
assert_eq!(fields_map.insert("title").unwrap(), 3.into()); assert_eq!(fields_map.insert("title").unwrap(), 3.into());
assert_eq!(fields_map.len(), 3); assert_eq!(fields_map.len(), 3);
} }

View File

@ -22,14 +22,6 @@ impl IndexedPos {
pub const fn max() -> IndexedPos { pub const fn max() -> IndexedPos {
IndexedPos(u16::max_value()) IndexedPos(u16::max_value())
} }
pub fn next(self) -> SResult<IndexedPos> {
self.0.checked_add(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
}
pub fn prev(self) -> SResult<IndexedPos> {
self.0.checked_sub(1).map(IndexedPos).ok_or(Error::MaxFieldsLimitExceeded)
}
} }
impl From<u16> for IndexedPos { impl From<u16> for IndexedPos {
@ -44,7 +36,6 @@ impl Into<u16> for IndexedPos {
} }
} }
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)] #[derive(Serialize, Deserialize, Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct FieldId(pub u16); pub struct FieldId(pub u16);
@ -64,10 +55,6 @@ impl FieldId {
pub fn next(self) -> SResult<FieldId> { pub fn next(self) -> SResult<FieldId> {
self.0.checked_add(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded) self.0.checked_add(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
} }
pub fn prev(self) -> SResult<FieldId> {
self.0.checked_sub(1).map(FieldId).ok_or(Error::MaxFieldsLimitExceeded)
}
} }
impl From<u16> for FieldId { impl From<u16> for FieldId {

View File

@ -15,12 +15,11 @@ pub struct Schema {
indexed: Vec<FieldId>, indexed: Vec<FieldId>,
indexed_map: HashMap<FieldId, IndexedPos>, indexed_map: HashMap<FieldId, IndexedPos>,
must_index_new_fields: bool, index_new_fields: bool,
} }
impl Schema { impl Schema {
pub fn with_identifier(name: &str) -> Schema {
pub fn with_identifier<S: Into<String>>(name: S) -> Schema {
let mut fields_map = FieldsMap::default(); let mut fields_map = FieldsMap::default();
let field_id = fields_map.insert(name.into()).unwrap(); let field_id = fields_map.insert(name.into()).unwrap();
@ -31,47 +30,47 @@ impl Schema {
displayed: HashSet::new(), displayed: HashSet::new(),
indexed: Vec::new(), indexed: Vec::new(),
indexed_map: HashMap::new(), indexed_map: HashMap::new(),
must_index_new_fields: true, index_new_fields: true,
} }
} }
pub fn identifier(&self) -> String { pub fn identifier(&self) -> &str {
self.fields_map.get_name(self.identifier).unwrap().to_string() self.fields_map.name(self.identifier).unwrap()
} }
pub fn set_identifier(&mut self, id: String) -> SResult<()> { pub fn set_identifier(&mut self, id: &str) -> SResult<()> {
match self.get_id(id.clone()) { match self.id(id) {
Some(id) => { Some(id) => {
self.identifier = id; self.identifier = id;
Ok(()) Ok(())
}, },
None => Err(Error::FieldNameNotFound(id)) None => Err(Error::FieldNameNotFound(id.to_string()))
} }
} }
pub fn get_id<S: Into<String>>(&self, name: S) -> Option<FieldId> { pub fn id(&self, name: &str) -> Option<FieldId> {
self.fields_map.get_id(name) self.fields_map.id(name)
} }
pub fn get_name<I: Into<FieldId>>(&self, id: I) -> Option<String> { pub fn name<I: Into<FieldId>>(&self, id: I) -> Option<&str> {
self.fields_map.get_name(id) self.fields_map.name(id)
} }
pub fn contains<S: Into<String>>(&self, name: S) -> bool { pub fn contains(&self, name: &str) -> bool {
self.fields_map.get_id(name.into()).is_some() self.fields_map.id(name.into()).is_some()
} }
pub fn get_or_create_empty<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> { pub fn get_or_create_empty(&mut self, name: &str) -> SResult<FieldId> {
self.fields_map.insert(name) self.fields_map.insert(name)
} }
pub fn get_or_create<S: Into<String> + std::clone::Clone>(&mut self, name: S) -> SResult<FieldId> { pub fn get_or_create(&mut self, name: &str) -> SResult<FieldId> {
match self.fields_map.get_id(name.clone()) { match self.fields_map.id(name.clone()) {
Some(id) => { Some(id) => {
Ok(id) Ok(id)
} }
None => { None => {
if self.must_index_new_fields { if self.index_new_fields {
self.set_indexed(name.clone())?; self.set_indexed(name.clone())?;
self.set_displayed(name) self.set_displayed(name)
} else { } else {
@ -81,43 +80,43 @@ impl Schema {
} }
} }
pub fn get_ranked(&self) -> HashSet<FieldId> { pub fn ranked(&self) -> HashSet<FieldId> {
self.ranked.clone() self.ranked.clone()
} }
pub fn get_ranked_name(&self) -> HashSet<String> { pub fn ranked_name(&self) -> HashSet<&str> {
self.ranked.iter().filter_map(|a| self.get_name(*a)).collect() self.ranked.iter().filter_map(|a| self.name(*a)).collect()
} }
pub fn get_displayed(&self) -> HashSet<FieldId> { pub fn displayed(&self) -> HashSet<FieldId> {
self.displayed.clone() self.displayed.clone()
} }
pub fn get_displayed_name(&self) -> HashSet<String> { pub fn displayed_name(&self) -> HashSet<&str> {
self.displayed.iter().filter_map(|a| self.get_name(*a)).collect() self.displayed.iter().filter_map(|a| self.name(*a)).collect()
} }
pub fn get_indexed(&self) -> Vec<FieldId> { pub fn indexed(&self) -> Vec<FieldId> {
self.indexed.clone() self.indexed.clone()
} }
pub fn get_indexed_name(&self) -> Vec<String> { pub fn indexed_name(&self) -> Vec<&str> {
self.indexed.iter().filter_map(|a| self.get_name(*a)).collect() self.indexed.iter().filter_map(|a| self.name(*a)).collect()
} }
pub fn set_ranked<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> { pub fn set_ranked(&mut self, name: &str) -> SResult<FieldId> {
let id = self.fields_map.insert(name.into())?; let id = self.fields_map.insert(name.into())?;
self.ranked.insert(id); self.ranked.insert(id);
Ok(id) Ok(id)
} }
pub fn set_displayed<S: Into<String>>(&mut self, name: S) -> SResult<FieldId> { pub fn set_displayed(&mut self, name: &str) -> SResult<FieldId> {
let id = self.fields_map.insert(name.into())?; let id = self.fields_map.insert(name.into())?;
self.displayed.insert(id); self.displayed.insert(id);
Ok(id) Ok(id)
} }
pub fn set_indexed<S: Into<String>>(&mut self, name: S) -> SResult<(FieldId, IndexedPos)> { pub fn set_indexed(&mut self, name: &str) -> SResult<(FieldId, IndexedPos)> {
let id = self.fields_map.insert(name.into())?; let id = self.fields_map.insert(name.into())?;
if let Some(indexed_pos) = self.indexed_map.get(&id) { if let Some(indexed_pos) = self.indexed_map.get(&id) {
return Ok((id, *indexed_pos)) return Ok((id, *indexed_pos))
@ -128,55 +127,34 @@ impl Schema {
Ok((id, pos.into())) Ok((id, pos.into()))
} }
pub fn remove_ranked<S: Into<String>>(&mut self, name: S) { pub fn remove_ranked(&mut self, name: &str) {
if let Some(id) = self.fields_map.get_id(name.into()) { if let Some(id) = self.fields_map.id(name.into()) {
self.ranked.remove(&id); self.ranked.remove(&id);
} }
} }
pub fn remove_displayed<S: Into<String>>(&mut self, name: S) { pub fn remove_displayed(&mut self, name: &str) {
if let Some(id) = self.fields_map.get_id(name.into()) { if let Some(id) = self.fields_map.id(name.into()) {
self.displayed.remove(&id); self.displayed.remove(&id);
} }
} }
pub fn remove_indexed<S: Into<String>>(&mut self, name: S) { pub fn remove_indexed(&mut self, name: &str) {
if let Some(id) = self.fields_map.get_id(name.into()) { if let Some(id) = self.fields_map.id(name.into()) {
self.indexed_map.remove(&id); self.indexed_map.remove(&id);
self.indexed.retain(|x| *x != id); self.indexed.retain(|x| *x != id);
} }
} }
pub fn is_ranked<S: Into<String>>(&self, name: S) -> Option<FieldId> { pub fn is_ranked(&self, id: FieldId) -> bool {
match self.fields_map.get_id(name.into()) {
Some(id) => self.ranked.get(&id).map(|s| *s),
None => None,
}
}
pub fn is_displayed<S: Into<String>>(&self, name: S) -> Option<FieldId> {
match self.fields_map.get_id(name.into()) {
Some(id) => self.displayed.get(&id).map(|s| *s),
None => None,
}
}
pub fn is_indexed<S: Into<String>>(&self, name: S) -> Option<IndexedPos> {
match self.fields_map.get_id(name.into()) {
Some(id) => self.indexed_map.get(&id).map(|s| *s),
None => None,
}
}
pub fn id_is_ranked(&self, id: FieldId) -> bool {
self.ranked.get(&id).is_some() self.ranked.get(&id).is_some()
} }
pub fn id_is_displayed(&self, id: FieldId) -> bool { pub fn is_displayed(&self, id: FieldId) -> bool {
self.displayed.get(&id).is_some() self.displayed.get(&id).is_some()
} }
pub fn id_is_indexed(&self, id: FieldId) -> Option<&IndexedPos> { pub fn is_indexed(&self, id: FieldId) -> Option<&IndexedPos> {
self.indexed_map.get(&id) self.indexed_map.get(&id)
} }
@ -189,36 +167,36 @@ impl Schema {
} }
} }
pub fn update_ranked<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> { pub fn update_ranked<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
self.ranked = HashSet::new(); self.ranked = HashSet::new();
for name in data { for name in data {
self.set_ranked(name)?; self.set_ranked(name.as_ref())?;
} }
Ok(()) Ok(())
} }
pub fn update_displayed<S: Into<String>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> { pub fn update_displayed<S: AsRef<str>>(&mut self, data: impl IntoIterator<Item = S>) -> SResult<()> {
self.displayed = HashSet::new(); self.displayed = HashSet::new();
for name in data { for name in data {
self.set_displayed(name)?; self.set_displayed(name.as_ref())?;
} }
Ok(()) Ok(())
} }
pub fn update_indexed<S: Into<String>>(&mut self, data: Vec<S>) -> SResult<()> { pub fn update_indexed<S: AsRef<str>>(&mut self, data: Vec<S>) -> SResult<()> {
self.indexed = Vec::new(); self.indexed = Vec::new();
self.indexed_map = HashMap::new(); self.indexed_map = HashMap::new();
for name in data { for name in data {
self.set_indexed(name)?; self.set_indexed(name.as_ref())?;
} }
Ok(()) Ok(())
} }
pub fn must_index_new_fields(&self) -> bool { pub fn index_new_fields(&self) -> bool {
self.must_index_new_fields self.index_new_fields
} }
pub fn set_must_index_new_fields(&mut self, value: bool) { pub fn set_index_new_fields(&mut self, value: bool) {
self.must_index_new_fields = value; self.index_new_fields = value;
} }
} }