MeiliSearch/milli/src/criterion.rs

98 lines
3.1 KiB
Rust
Raw Normal View History

use std::fmt;
use std::str::FromStr;
2020-11-27 12:14:56 +01:00
2021-08-20 18:09:17 +02:00
use once_cell::sync::Lazy;
use regex::Regex;
2021-06-16 18:33:33 +02:00
use serde::{Deserialize, Serialize};
2020-11-27 12:14:56 +01:00
use crate::error::{Error, UserError};
2021-08-20 18:09:17 +02:00
static ASC_DESC_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(asc|desc)\(([\w_-]+)\)"#).unwrap());
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
2020-08-12 10:43:02 +02:00
pub enum Criterion {
/// Sorted by decreasing number of matched query terms.
/// Query words at the front of an attribute is considered better than if it was at the back.
2020-08-12 10:43:02 +02:00
Words,
/// Sorted by increasing number of typos.
Typo,
2020-08-12 10:43:02 +02:00
/// Sorted by increasing distance between matched query terms.
Proximity,
/// Documents with quey words contained in more important
2021-08-20 18:09:17 +02:00
/// attributes are considred better.
2020-08-12 10:43:02 +02:00
Attribute,
/// Sorted by the similarity of the matched words with the query words.
Exactness,
/// Sorted by the increasing value of the field specified.
Asc(String),
2020-08-12 10:43:02 +02:00
/// Sorted by the decreasing value of the field specified.
Desc(String),
2020-08-12 10:43:02 +02:00
}
impl Criterion {
/// Returns the field name parameter of this criterion.
pub fn field_name(&self) -> Option<&str> {
match self {
Criterion::Asc(name) | Criterion::Desc(name) => Some(name),
_otherwise => None,
}
}
}
impl FromStr for Criterion {
type Err = Error;
2021-08-20 18:09:17 +02:00
fn from_str(txt: &str) -> Result<Criterion, Self::Err> {
match txt {
2020-12-04 12:02:22 +01:00
"words" => Ok(Criterion::Words),
"typo" => Ok(Criterion::Typo),
2020-12-04 12:02:22 +01:00
"proximity" => Ok(Criterion::Proximity),
"attribute" => Ok(Criterion::Attribute),
"exactness" => Ok(Criterion::Exactness),
2021-08-20 18:09:17 +02:00
text => {
let caps = ASC_DESC_REGEX
.captures(text)
.ok_or_else(|| UserError::InvalidCriterionName { name: text.to_string() })?;
let order = caps.get(1).unwrap().as_str();
let field_name = caps.get(2).unwrap().as_str();
match order {
"asc" => Ok(Criterion::Asc(field_name.to_string())),
"desc" => Ok(Criterion::Desc(field_name.to_string())),
text => {
return Err(
UserError::InvalidCriterionName { name: text.to_string() }.into()
)
}
}
}
}
}
}
2020-08-12 10:43:02 +02:00
pub fn default_criteria() -> Vec<Criterion> {
vec![
Criterion::Words,
Criterion::Typo,
2020-08-12 10:43:02 +02:00
Criterion::Proximity,
Criterion::Attribute,
Criterion::Exactness,
]
}
impl fmt::Display for Criterion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Criterion::*;
match self {
2021-06-16 18:33:33 +02:00
Words => f.write_str("words"),
Typo => f.write_str("typo"),
Proximity => f.write_str("proximity"),
Attribute => f.write_str("attribute"),
Exactness => f.write_str("exactness"),
2021-08-20 18:09:17 +02:00
Asc(attr) => write!(f, "asc({})", attr),
Desc(attr) => write!(f, "desc({})", attr),
}
}
}