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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use super::MoreLikeThis;
use crate::query::{Query, Weight};
use crate::schema::{Field, Value};
use crate::{DocAddress, Result, Searcher};
#[derive(Debug, Clone)]
pub struct MoreLikeThisQuery {
mlt: MoreLikeThis,
target: TargetDocument,
}
#[derive(Debug, PartialEq, Clone)]
enum TargetDocument {
DocumentAdress(DocAddress),
DocumentFields(Vec<(Field, Vec<Value>)>),
}
impl MoreLikeThisQuery {
pub fn builder() -> MoreLikeThisQueryBuilder {
MoreLikeThisQueryBuilder::default()
}
}
impl Query for MoreLikeThisQuery {
fn weight(&self, searcher: &Searcher, scoring_enabled: bool) -> Result<Box<dyn Weight>> {
match &self.target {
TargetDocument::DocumentAdress(doc_address) => self
.mlt
.query_with_document(searcher, *doc_address)?
.weight(searcher, scoring_enabled),
TargetDocument::DocumentFields(doc_fields) => self
.mlt
.query_with_document_fields(searcher, doc_fields)?
.weight(searcher, scoring_enabled),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct MoreLikeThisQueryBuilder {
mlt: MoreLikeThis,
}
impl MoreLikeThisQueryBuilder {
#[must_use]
pub fn with_min_doc_frequency(mut self, value: u64) -> Self {
self.mlt.min_doc_frequency = Some(value);
self
}
#[must_use]
pub fn with_max_doc_frequency(mut self, value: u64) -> Self {
self.mlt.max_doc_frequency = Some(value);
self
}
#[must_use]
pub fn with_min_term_frequency(mut self, value: usize) -> Self {
self.mlt.min_term_frequency = Some(value);
self
}
#[must_use]
pub fn with_max_query_terms(mut self, value: usize) -> Self {
self.mlt.max_query_terms = Some(value);
self
}
#[must_use]
pub fn with_min_word_length(mut self, value: usize) -> Self {
self.mlt.min_word_length = Some(value);
self
}
#[must_use]
pub fn with_max_word_length(mut self, value: usize) -> Self {
self.mlt.max_word_length = Some(value);
self
}
#[must_use]
pub fn with_boost_factor(mut self, value: f32) -> Self {
self.mlt.boost_factor = Some(value);
self
}
#[must_use]
pub fn with_stop_words(mut self, value: Vec<String>) -> Self {
self.mlt.stop_words = value;
self
}
pub fn with_document(self, doc_address: DocAddress) -> MoreLikeThisQuery {
MoreLikeThisQuery {
mlt: self.mlt,
target: TargetDocument::DocumentAdress(doc_address),
}
}
pub fn with_document_fields(self, doc_fields: Vec<(Field, Vec<Value>)>) -> MoreLikeThisQuery {
MoreLikeThisQuery {
mlt: self.mlt,
target: TargetDocument::DocumentFields(doc_fields),
}
}
}
#[cfg(test)]
mod tests {
use super::{MoreLikeThisQuery, TargetDocument};
use crate::collector::TopDocs;
use crate::schema::{Schema, STORED, TEXT};
use crate::{DocAddress, Index};
fn create_test_index() -> crate::Result<Index> {
let mut schema_builder = Schema::builder();
let title = schema_builder.add_text_field("title", TEXT);
let body = schema_builder.add_text_field("body", TEXT | STORED);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
let mut index_writer = index.writer_for_tests().unwrap();
index_writer.add_document(doc!(title => "aaa", body => "the old man and the sea"))?;
index_writer.add_document(doc!(title => "bbb", body => "an old man sailing on the sea"))?;
index_writer.add_document(doc!(title => "ccc", body=> "send this message to alice"))?;
index_writer.add_document(doc!(title => "ddd", body=> "a lady was riding and old bike"))?;
index_writer.add_document(doc!(title => "eee", body=> "Yes, my lady."))?;
index_writer.commit()?;
Ok(index)
}
#[test]
fn test_more_like_this_query_builder() {
let query = MoreLikeThisQuery::builder().with_document_fields(vec![]);
assert_eq!(query.mlt.min_doc_frequency, Some(5));
assert_eq!(query.mlt.max_doc_frequency, None);
assert_eq!(query.mlt.min_term_frequency, Some(2));
assert_eq!(query.mlt.max_query_terms, Some(25));
assert_eq!(query.mlt.min_word_length, None);
assert_eq!(query.mlt.max_word_length, None);
assert_eq!(query.mlt.boost_factor, Some(1.0));
assert_eq!(query.mlt.stop_words, Vec::<String>::new());
assert_eq!(query.target, TargetDocument::DocumentFields(vec![]));
let query = MoreLikeThisQuery::builder()
.with_min_doc_frequency(2)
.with_max_doc_frequency(5)
.with_min_term_frequency(2)
.with_min_word_length(2)
.with_max_word_length(4)
.with_boost_factor(0.5)
.with_stop_words(vec!["all".to_string(), "for".to_string()])
.with_document(DocAddress::new(1, 2));
assert_eq!(query.mlt.min_doc_frequency, Some(2));
assert_eq!(query.mlt.max_doc_frequency, Some(5));
assert_eq!(query.mlt.min_term_frequency, Some(2));
assert_eq!(query.mlt.min_word_length, Some(2));
assert_eq!(query.mlt.max_word_length, Some(4));
assert_eq!(query.mlt.boost_factor, Some(0.5));
assert_eq!(
query.mlt.stop_words,
vec!["all".to_string(), "for".to_string()]
);
assert_eq!(
query.target,
TargetDocument::DocumentAdress(DocAddress::new(1, 2))
);
}
#[test]
fn test_more_like_this_query() -> crate::Result<()> {
let index = create_test_index()?;
let reader = index.reader()?;
let searcher = reader.searcher();
let query = MoreLikeThisQuery::builder()
.with_min_doc_frequency(1)
.with_max_doc_frequency(10)
.with_min_term_frequency(1)
.with_min_word_length(2)
.with_max_word_length(5)
.with_boost_factor(1.0)
.with_stop_words(vec!["old".to_string()])
.with_document(DocAddress::new(0, 0));
let top_docs = searcher.search(&query, &TopDocs::with_limit(5))?;
let mut doc_ids: Vec<_> = top_docs.iter().map(|item| item.1.doc_id).collect();
doc_ids.sort_unstable();
assert_eq!(doc_ids.len(), 3);
assert_eq!(doc_ids, vec![0, 1, 3]);
let query = MoreLikeThisQuery::builder()
.with_min_doc_frequency(1)
.with_max_doc_frequency(10)
.with_min_term_frequency(1)
.with_min_word_length(2)
.with_max_word_length(5)
.with_boost_factor(1.0)
.with_document(DocAddress::new(0, 4));
let top_docs = searcher.search(&query, &TopDocs::with_limit(5))?;
let mut doc_ids: Vec<_> = top_docs.iter().map(|item| item.1.doc_id).collect();
doc_ids.sort_unstable();
assert_eq!(doc_ids.len(), 2);
assert_eq!(doc_ids, vec![3, 4]);
Ok(())
}
}