attribute_search_engine/query.rs
1/// Query is a recursive datatype that describes a
2/// query to a search index or engine.
3///
4/// The query only takes strings as parameters. It is up to the
5/// search index to parse this string into the expected type.
6///
7/// # Example
8/// ```
9/// use attribute_search_engine::Query;
10///
11/// let q = Query::Exclude(
12/// Box::new(Query::And(vec![
13/// Query::Or(vec![
14/// Query::InRange("a".into(), "32".into(), "128".into()),
15/// Query::Prefix("b".into(), "hello".into()),
16/// ]),
17/// Query::Minimum("c".into(), "42".into()),
18/// ])),
19/// vec![
20/// Query::Exact("b".into(), "hello world".into()),
21/// ],
22/// );
23/// ```
24#[derive(Debug, PartialEq)]
25pub enum Query {
26 /// Only matches if the attribute has exactly the value as the query.
27 Exact(String, String),
28
29 /// Matches if the attribute starts with the value of the query.
30 Prefix(String, String),
31
32 /// Matches if the attribute is in the range of the two query values.
33 /// First is minimum, seconds is maximum, both inclusive.
34 InRange(String, String, String),
35
36 /// Matches if the attribute is NOT in the range of the two query values.
37 /// First is the start of the range, seconds is the end. A value that is
38 /// equal to the start or the end is considered in range and will not be
39 /// returned.
40 OutRange(String, String, String),
41
42 /// Matches if the attribute is at least as high/big as the query value.
43 Minimum(String, String),
44
45 /// Matches if the attribute is at most as high/big as the query value.
46 Maximum(String, String),
47
48 /// Matches if at least one of the subqueries matches.
49 Or(Vec<Query>),
50
51 /// Only matches if all subqueries match.
52 And(Vec<Query>),
53
54 /// Removed all matches from the first query that appear in at least
55 /// on of the matches of the query vector.
56 Exclude(Box<Query>, Vec<Query>),
57}
58
59/// Bitmask type for queries that are supported by an index.
60pub type SupportedQueries = u8;
61
62/// Signals that an index supports [Exact queries](Query::Exact).
63pub const SUPPORTS_EXACT: SupportedQueries = 1 << 0;
64
65/// Signals that an index supports [Prefix queries](Query::Prefix).
66pub const SUPPORTS_PREFIX: SupportedQueries = 1 << 1;
67
68/// Signals that an index supports [InRange queries](Query::InRange).
69pub const SUPPORTS_INRANGE: SupportedQueries = 1 << 2;
70
71/// Signals that an index supports [OutRange queries](Query::OutRange).
72pub const SUPPORTS_OUTRANGE: SupportedQueries = 1 << 3;
73
74/// Signals that an index supports [Minimum queries](Query::Minimum).
75pub const SUPPORTS_MINIMUM: SupportedQueries = 1 << 4;
76
77/// Signals that an index supports [Maximum queries](Query::Maximum).
78pub const SUPPORTS_MAXIMUM: SupportedQueries = 1 << 5;