1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::search::query::{IntoQuery, Query};
/// A clause in a BooleanQuery.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct BooleanClause {
pub query: Query,
pub occur: Occur,
}
// for padding
impl Default for BooleanClause {
fn default() -> Self {
Self {
query: Query::default(),
occur: Occur::Should,
}
}
}
impl BooleanClause {
/// Constructs a BooleanClause.
///
/// Accepts a concrete query or a `Query` value through `IntoQuery`.
pub fn new<Q>(query: Q, occur: Occur) -> Self
where
Q: IntoQuery,
{
Self {
query: query.into_query(),
occur,
}
}
pub fn is_prohibited(&self) -> bool {
self.occur == Occur::MustNot
}
pub fn is_required(&self) -> bool {
matches!(self.occur, Occur::Must | Occur::Filter)
}
pub fn is_scoring(&self) -> bool {
matches!(self.occur, Occur::Must | Occur::Should)
}
pub fn occur(&self) -> &Occur {
&self.occur
}
pub fn take_query(self) -> Query {
self.query
}
}
/// Specifies how clauses are to occur in matching documents.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Occur {
/// Use this operator for clauses that *must* appear in the matching documents.
Must,
/// Like [`Occur::Must`] except that these clauses do not participate in scoring.
Filter,
/// Use this operator for clauses that *should* appear in the matching documents.
///
/// For a BooleanQuery with no `MUST` clauses one or more `SHOULD` clauses must match
/// a document for the BooleanQuery to match.
///
/// See also: [`Builder::set_minimum_number_should_match`](crate::core::search::boolean_query::Builder::set_minimum_number_should_match).
Should,
/// Use this operator for clauses that *must not* appear in the matching documents.
///
/// Note that it is not possible to search for queries that only consist of a `MUST_NOT`
/// clause. These clauses do not contribute to the score of documents.
MustNot,
}
impl core::fmt::Display for Occur {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Occur::Must => write!(f, "+"),
Occur::Filter => write!(f, "#"),
Occur::Should => write!(f, ""),
Occur::MustNot => write!(f, "-"),
}
}
}
impl Occur {
pub fn is_required(self) -> bool {
matches!(self, Occur::Must | Occur::Filter)
}
pub fn is_scoring(self) -> bool {
matches!(self, Occur::Must | Occur::Should)
}
pub fn is_prohibited(self) -> bool {
matches!(self, Occur::MustNot)
}
#[cfg(test)]
pub const fn values() -> &'static [Occur] {
&[Occur::Must, Occur::Filter, Occur::Should, Occur::MustNot]
}
}