2021-01-20 17:27:43 +01:00
|
|
|
use std::collections::HashMap;
|
2020-11-27 12:14:56 +01:00
|
|
|
|
2020-12-04 12:02:22 +01:00
|
|
|
use anyhow::{Context, bail};
|
|
|
|
use regex::Regex;
|
2020-11-27 12:14:56 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
2021-01-20 17:27:43 +01:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
2020-08-12 10:43:02 +02:00
|
|
|
pub enum Criterion {
|
|
|
|
/// Sorted by increasing number of typos.
|
|
|
|
Typo,
|
|
|
|
/// Sorted by decreasing number of matched query terms.
|
|
|
|
Words,
|
|
|
|
/// Sorted by increasing distance between matched query terms.
|
|
|
|
Proximity,
|
|
|
|
/// Documents with quey words contained in more important
|
|
|
|
/// attributes are considred better.
|
|
|
|
Attribute,
|
|
|
|
/// Documents with query words at the front of an attribute is
|
|
|
|
/// considered better than if it was at the back.
|
|
|
|
WordsPosition,
|
|
|
|
/// Sorted by the similarity of the matched words with the query words.
|
|
|
|
Exactness,
|
|
|
|
/// Sorted by the increasing value of the field specified.
|
2021-01-20 17:27:43 +01:00
|
|
|
Asc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
/// Sorted by the decreasing value of the field specified.
|
2021-01-20 17:27:43 +01:00
|
|
|
Desc(String),
|
2020-08-12 10:43:02 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 12:02:22 +01:00
|
|
|
impl Criterion {
|
2021-01-20 17:27:43 +01:00
|
|
|
pub fn from_str(faceted_attributes: &HashMap<String, FacetType>, txt: &str) -> anyhow::Result<Criterion> {
|
2020-12-04 12:02:22 +01:00
|
|
|
match txt {
|
|
|
|
"typo" => Ok(Criterion::Typo),
|
|
|
|
"words" => Ok(Criterion::Words),
|
|
|
|
"proximity" => Ok(Criterion::Proximity),
|
|
|
|
"attribute" => Ok(Criterion::Attribute),
|
|
|
|
"wordsposition" => Ok(Criterion::WordsPosition),
|
|
|
|
"exactness" => Ok(Criterion::Exactness),
|
|
|
|
text => {
|
|
|
|
let re = Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#)?;
|
|
|
|
let caps = re.captures(text).with_context(|| format!("unknown criterion name: {}", text))?;
|
|
|
|
let order = caps.get(1).unwrap().as_str();
|
|
|
|
let field_name = caps.get(2).unwrap().as_str();
|
2021-01-20 17:27:43 +01:00
|
|
|
faceted_attributes.get(field_name).with_context(|| format!("Can't use {:?} as a criterion as it isn't a faceted field.", field_name))?;
|
2020-12-04 12:02:22 +01:00
|
|
|
match order {
|
2021-01-20 17:27:43 +01:00
|
|
|
"asc" => Ok(Criterion::Asc(field_name.to_string())),
|
|
|
|
"desc" => Ok(Criterion::Desc(field_name.to_string())),
|
2020-12-04 12:02:22 +01:00
|
|
|
otherwise => bail!("unknown criterion name: {}", otherwise),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:43:02 +02:00
|
|
|
pub fn default_criteria() -> Vec<Criterion> {
|
|
|
|
vec![
|
|
|
|
Criterion::Typo,
|
|
|
|
Criterion::Words,
|
|
|
|
Criterion::Proximity,
|
|
|
|
Criterion::Attribute,
|
|
|
|
Criterion::WordsPosition,
|
|
|
|
Criterion::Exactness,
|
|
|
|
]
|
|
|
|
}
|