Skip to content

Commit b118df0

Browse files
committed
Adressed comments
1 parent aa3be8a commit b118df0

File tree

1 file changed

+23
-51
lines changed

1 file changed

+23
-51
lines changed

src/query/phrase_query/phrase_scorer.rs

+23-51
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ fn intersection(left: &mut [u32], right: &[u32]) -> usize {
132132
}
133133

134134
/// Intersect twos sorted arrays `left` and `right` and outputs the
135-
/// resulting array inline in both `left` and `right`.
135+
/// resulting array in left.
136136
///
137137
/// Condition for match is that the value stored in left is less than or equal to
138138
/// the value in right and that the distance to the previous token is lte to the slop.
139139
///
140140
/// Returns the length of the intersection
141-
fn intersection_with_distance(left: &mut [u32], right: &[u32], slop: u32) -> usize {
141+
fn intersection_with_slop(left: &mut [u32], right: &[u32], slop: u32) -> usize {
142142
let mut left_index = 0;
143143
let mut right_index = 0;
144144
let mut count = 0;
@@ -235,80 +235,52 @@ impl<TPostings: Postings> PhraseScorer<TPostings> {
235235
}
236236

237237
fn phrase_exists(&mut self) -> bool {
238-
let intersection_len = if self.has_slop() {
239-
self.compute_match_with_slop()
240-
} else {
241-
self.compute_match()
242-
};
238+
let intersection_len = self.compute_phrase_match();
243239
intersection_exists(&self.left[..intersection_len], &self.right[..])
244240
}
245241

246242
fn compute_phrase_count(&mut self) -> u32 {
247-
let intersection_len = if self.has_slop() {
248-
self.compute_match_with_slop()
249-
} else {
250-
self.compute_match()
251-
};
243+
let intersection_len = self.compute_phrase_match();
252244
intersection_count(&self.left[..intersection_len], &self.right[..]) as u32
253245
}
254246

255-
/// Computes match without slop.
256-
fn compute_match(&mut self) -> usize {
247+
fn compute_phrase_match(&mut self) -> usize {
257248
{
258249
self.intersection_docset
259250
.docset_mut_specialized(0)
260251
.positions(&mut self.left);
261252
}
262253
let mut intersection_len = self.left.len();
263-
for i in 1..self.num_terms - 1 {
254+
let end_term = if self.has_slop() {
255+
self.num_terms
256+
} else {
257+
self.num_terms - 1
258+
};
259+
for i in 1..end_term {
264260
{
265261
self.intersection_docset
266262
.docset_mut_specialized(i)
267263
.positions(&mut self.right);
268264
}
269-
intersection_len = intersection(&mut self.left[..intersection_len], &self.right[..]);
265+
intersection_len = if self.has_slop() {
266+
intersection_with_slop(
267+
&mut self.left[..intersection_len],
268+
&self.right[..],
269+
self.slop,
270+
)
271+
} else {
272+
intersection(&mut self.left[..intersection_len], &self.right[..])
273+
};
270274
if intersection_len == 0 {
271275
return 0;
272276
}
273277
}
274-
275278
self.intersection_docset
276279
.docset_mut_specialized(self.num_terms - 1)
277280
.positions(&mut self.right);
278281
intersection_len
279282
}
280283

281-
// Computes match with slop.
282-
fn compute_match_with_slop(&mut self) -> usize {
283-
{
284-
self.intersection_docset
285-
.docset_mut_specialized(0)
286-
.positions(&mut self.left);
287-
}
288-
let mut intersection_len = self.left.len();
289-
// We'll increment the values to be equal to the next match in the right array to achieve ordered slop.
290-
for i in 1..self.num_terms {
291-
{
292-
self.intersection_docset
293-
.docset_mut_specialized(i)
294-
.positions(&mut self.right);
295-
}
296-
intersection_len = intersection_with_distance(
297-
&mut self.left[..intersection_len],
298-
&self.right[..],
299-
self.slop,
300-
);
301-
// Update the left to be equal to the right. Merge the initial left.
302-
if intersection_len == 0 {
303-
return 0;
304-
}
305-
}
306-
self.intersection_docset
307-
.docset_mut_specialized(self.num_terms - 1)
308-
.positions(&mut self.left);
309-
intersection_len
310-
}
311-
312284
fn has_slop(&self) -> bool {
313285
self.slop > 0
314286
}
@@ -353,7 +325,7 @@ impl<TPostings: Postings> Scorer for PhraseScorer<TPostings> {
353325

354326
#[cfg(test)]
355327
mod tests {
356-
use super::{intersection, intersection_count, intersection_with_distance};
328+
use super::{intersection, intersection_count, intersection_with_slop};
357329

358330
fn test_intersection_sym(left: &[u32], right: &[u32], expected: &[u32]) {
359331
test_intersection_aux(left, right, expected, 0);
@@ -372,7 +344,7 @@ mod tests {
372344
}
373345
let mut right_vec = Vec::from(right);
374346
let right_mut = &mut right_vec[..];
375-
let count = intersection_with_distance(left_mut, right_mut, slop);
347+
let count = intersection_with_slop(left_mut, right_mut, slop);
376348
assert_eq!(&left_mut[..count], expected);
377349
}
378350

@@ -403,7 +375,7 @@ mod tests {
403375
let left_mut = &mut left_vec[..];
404376
let mut right_vec = Vec::from(right);
405377
let right_mut = &mut right_vec[..];
406-
let count = intersection_with_distance(left_mut, right_mut, slop);
378+
let count = intersection_with_slop(left_mut, right_mut, slop);
407379
assert_eq!(&left_mut[..count], expected_left);
408380
}
409381

0 commit comments

Comments
 (0)