Skip to main content

hermes_core/query/
boolean.rs

1//! Boolean query with MUST, SHOULD, and MUST_NOT clauses
2
3use std::sync::Arc;
4
5use crate::segment::SegmentReader;
6use crate::structures::TERMINATED;
7use crate::{DocId, Score};
8
9use super::planner::{
10    build_combined_bitset, build_sparse_bmp_results, build_sparse_bmp_results_filtered,
11    build_sparse_maxscore_executor, chain_predicates, combine_sparse_results, compute_idf,
12    extract_all_sparse_infos, finish_text_maxscore, prepare_per_field_grouping,
13    prepare_text_maxscore,
14};
15use super::{CountFuture, EmptyScorer, GlobalStats, Query, Scorer, ScorerFuture};
16
17/// Boolean query with MUST, SHOULD, and MUST_NOT clauses
18///
19/// When all clauses are SHOULD term queries on the same field, automatically
20/// uses MaxScore optimization for efficient top-k retrieval.
21#[derive(Default, Clone)]
22pub struct BooleanQuery {
23    pub must: Vec<Arc<dyn Query>>,
24    pub should: Vec<Arc<dyn Query>>,
25    pub must_not: Vec<Arc<dyn Query>>,
26    /// Optional global statistics for cross-segment IDF
27    global_stats: Option<Arc<GlobalStats>>,
28}
29
30impl std::fmt::Debug for BooleanQuery {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        f.debug_struct("BooleanQuery")
33            .field("must_count", &self.must.len())
34            .field("should_count", &self.should.len())
35            .field("must_not_count", &self.must_not.len())
36            .field("has_global_stats", &self.global_stats.is_some())
37            .finish()
38    }
39}
40
41impl std::fmt::Display for BooleanQuery {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(f, "Boolean(")?;
44        let mut first = true;
45        for q in &self.must {
46            if !first {
47                write!(f, " ")?;
48            }
49            write!(f, "+{}", q)?;
50            first = false;
51        }
52        for q in &self.should {
53            if !first {
54                write!(f, " ")?;
55            }
56            write!(f, "{}", q)?;
57            first = false;
58        }
59        for q in &self.must_not {
60            if !first {
61                write!(f, " ")?;
62            }
63            write!(f, "-{}", q)?;
64            first = false;
65        }
66        write!(f, ")")
67    }
68}
69
70impl BooleanQuery {
71    pub fn new() -> Self {
72        Self::default()
73    }
74
75    pub fn must(mut self, query: impl Query + 'static) -> Self {
76        self.must.push(Arc::new(query));
77        self
78    }
79
80    pub fn should(mut self, query: impl Query + 'static) -> Self {
81        self.should.push(Arc::new(query));
82        self
83    }
84
85    pub fn must_not(mut self, query: impl Query + 'static) -> Self {
86        self.must_not.push(Arc::new(query));
87        self
88    }
89
90    /// Set global statistics for cross-segment IDF
91    pub fn with_global_stats(mut self, stats: Arc<GlobalStats>) -> Self {
92        self.global_stats = Some(stats);
93        self
94    }
95}
96
97/// Build a SHOULD-only scorer from a vec of optimized scorers.
98fn build_should_scorer<'a>(scorers: Vec<Box<dyn Scorer + 'a>>) -> Box<dyn Scorer + 'a> {
99    if scorers.is_empty() {
100        return Box::new(EmptyScorer);
101    }
102    if scorers.len() == 1 {
103        return scorers.into_iter().next().unwrap();
104    }
105    let mut scorer = BooleanScorer {
106        must: vec![],
107        should: scorers,
108        must_not: vec![],
109        current_doc: 0,
110    };
111    scorer.current_doc = scorer.find_next_match();
112    Box::new(scorer)
113}
114
115// ── Planner macro ────────────────────────────────────────────────────────
116//
117// Unified planner for both async and sync paths.  Parameterised on:
118//   $scorer_fn      – scorer | scorer_sync
119//   $get_postings_fn – get_postings | get_postings_sync
120//   $execute_fn     – execute | execute_sync
121//   $($aw)*         – .await  (present for async, absent for sync)
122//
123// Decision order:
124//   1. Single-clause unwrap
125//   2. Pure OR → text MaxScore | sparse MaxScore | per-field MaxScore
126//   3. Filter push-down → predicate-aware sparse MaxScore | PredicatedScorer
127//   4. Standard BooleanScorer fallback
128macro_rules! boolean_plan {
129    ($must:expr, $should:expr, $must_not:expr, $global_stats:expr,
130     $reader:expr, $limit:expr,
131     $scorer_fn:ident, $get_postings_fn:ident, $execute_fn:ident
132     $(, $aw:tt)*) => {{
133        let must: &[Arc<dyn Query>] = &$must;
134        let should_all: &[Arc<dyn Query>] = &$should;
135        let must_not: &[Arc<dyn Query>] = &$must_not;
136        let global_stats: Option<&Arc<GlobalStats>> = $global_stats;
137        let reader: &SegmentReader = $reader;
138        let limit: usize = $limit;
139
140        // Cap SHOULD clauses to MAX_QUERY_TERMS, but only count queries that need
141        // posting-list cursors. Fast-field predicates (O(1) per doc) are exempt.
142        let should_capped: Vec<Arc<dyn Query>>;
143        let should: &[Arc<dyn Query>] = if should_all.len() > super::MAX_QUERY_TERMS {
144            let is_predicate: Vec<bool> = should_all
145                .iter()
146                .map(|q| q.is_filter() || q.as_doc_predicate(reader).is_some())
147                .collect();
148            let cursor_count = is_predicate.iter().filter(|&&p| !p).count();
149
150            if cursor_count > super::MAX_QUERY_TERMS {
151                let mut kept = Vec::with_capacity(should_all.len());
152                let mut cursor_kept = 0usize;
153                for (q, &is_pred) in should_all.iter().zip(is_predicate.iter()) {
154                    if is_pred {
155                        kept.push(q.clone());
156                    } else if cursor_kept < super::MAX_QUERY_TERMS {
157                        kept.push(q.clone());
158                        cursor_kept += 1;
159                    }
160                }
161                log::debug!(
162                    "BooleanQuery: capping cursor SHOULD from {} to {} ({} fast-field predicates exempt)",
163                    cursor_count,
164                    super::MAX_QUERY_TERMS,
165                    kept.len() - cursor_kept,
166                );
167                should_capped = kept;
168                &should_capped
169            } else {
170                log::debug!(
171                    "BooleanQuery: {} SHOULD clauses OK ({} need cursors, {} fast-field predicates)",
172                    should_all.len(),
173                    cursor_count,
174                    should_all.len() - cursor_count,
175                );
176                should_all
177            }
178        } else {
179            should_all
180        };
181
182        // ── 1. Single-clause optimisation ────────────────────────────────
183        if must_not.is_empty() {
184            if must.len() == 1 && should.is_empty() {
185                return must[0].$scorer_fn(reader, limit) $(.  $aw)* ;
186            }
187            if should.len() == 1 && must.is_empty() {
188                return should[0].$scorer_fn(reader, limit) $(. $aw)* ;
189            }
190        }
191
192        // ── 2. Pure OR → MaxScore optimisations ──────────────────────────
193        if must.is_empty() && must_not.is_empty() && should.len() >= 2 {
194            // 2a. Text MaxScore (single-field, all term queries)
195            if let Some((mut infos, _field, avg_field_len, num_docs)) =
196                prepare_text_maxscore(should, reader, global_stats)
197            {
198                let mut posting_lists = Vec::with_capacity(infos.len());
199                for info in infos.drain(..) {
200                    if let Some(pl) = reader.$get_postings_fn(info.field, &info.term)
201                        $(. $aw)* ?
202                    {
203                        let idf = compute_idf(&pl, info.field, &info.term, num_docs, global_stats);
204                        posting_lists.push((pl, idf));
205                    }
206                }
207                return finish_text_maxscore(posting_lists, avg_field_len, limit, reader);
208            }
209
210            // 2b. Sparse (single-field, all sparse term queries)
211            // Auto-detect: BMP executor if field has BMP index, else MaxScore
212            if let Some(infos) = extract_all_sparse_infos(should) {
213                if let Some((raw, info)) =
214                    build_sparse_bmp_results(&infos, reader, limit)
215                {
216                    return Ok(combine_sparse_results(raw, info.combiner, info.field, limit));
217                }
218                if let Some((executor, info)) =
219                    build_sparse_maxscore_executor(&infos, reader, limit, None)
220                {
221                    let raw = executor.$execute_fn() $(. $aw)* ?;
222                    return Ok(combine_sparse_results(raw, info.combiner, info.field, limit));
223                }
224            }
225
226            // 2c. Per-field text MaxScore (multi-field term grouping)
227            if let Some(grouping) = prepare_per_field_grouping(should, reader, limit, global_stats)
228            {
229                let mut scorers: Vec<Box<dyn Scorer + '_>> = Vec::new();
230                for (field, avg_field_len, infos) in &grouping.multi_term_groups {
231                    let mut posting_lists = Vec::with_capacity(infos.len());
232                    for info in infos {
233                        if let Some(pl) = reader.$get_postings_fn(info.field, &info.term)
234                            $(. $aw)* ?
235                        {
236                            let idf = compute_idf(
237                                &pl, *field, &info.term, grouping.num_docs, global_stats,
238                            );
239                            posting_lists.push((pl, idf));
240                        }
241                    }
242                    if !posting_lists.is_empty() {
243                        scorers.push(finish_text_maxscore(
244                            posting_lists,
245                            *avg_field_len,
246                            grouping.per_field_limit,
247                            reader,
248                        )?);
249                    }
250                }
251                for &idx in &grouping.fallback_indices {
252                    scorers.push(should[idx].$scorer_fn(reader, limit) $(. $aw)* ?);
253                }
254                return Ok(build_should_scorer(scorers));
255            }
256        }
257
258        // ── 3. Filter push-down (MUST + SHOULD) ─────────────────────────
259        if !should.is_empty() && !must.is_empty() && limit < usize::MAX / 4 {
260            // Pre-check: is SHOULD all-sparse? This determines whether we can
261            // use bitset fallback for MUST clauses that lack fast-field predicates.
262            // For sparse SHOULD, the predicate is pushed into BMP/MaxScore traversal
263            // so all qualifying docs are found. For text SHOULD, we must NOT convert
264            // MUST to a predicate (PredicatedScorer would drop MUST-only docs that
265            // don't match SHOULD), so those go to verifier → BooleanScorer.
266            let should_is_sparse = extract_all_sparse_infos(should).is_some();
267
268            // 3a. Compile MUST → predicates (O(1)) vs verifier scorers (seek)
269            //
270            // Priority: as_doc_predicate (fast-field O(1)) > as_doc_bitset
271            // (posting-list materialization, O(1) lookup, sparse-SHOULD only)
272            // > verifier scorer (seek).
273            let mut predicates: Vec<super::DocPredicate<'_>> = Vec::new();
274            let mut must_verifiers: Vec<Box<dyn super::Scorer + '_>> = Vec::new();
275            for q in must {
276                if let Some(pred) = q.as_doc_predicate(reader) {
277                    log::debug!("BooleanQuery planner 3a: MUST clause → predicate ({})", q);
278                    predicates.push(pred);
279                } else if should_is_sparse {
280                    if let Some(bitset) = q.as_doc_bitset(reader) {
281                        log::debug!("BooleanQuery planner 3a: MUST clause → bitset predicate ({})", q);
282                        predicates.push(Box::new(move |doc_id| bitset.contains(doc_id)));
283                    } else {
284                        log::debug!("BooleanQuery planner 3a: MUST clause → verifier scorer ({})", q);
285                        must_verifiers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
286                    }
287                } else {
288                    log::debug!("BooleanQuery planner 3a: MUST clause → verifier scorer ({})", q);
289                    must_verifiers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
290                }
291            }
292            // Compile MUST_NOT → negated predicates vs verifier scorers
293            let mut must_not_verifiers: Vec<Box<dyn super::Scorer + '_>> = Vec::new();
294            for q in must_not {
295                if let Some(pred) = q.as_doc_predicate(reader) {
296                    let negated: super::DocPredicate<'_> =
297                        Box::new(move |doc_id| !pred(doc_id));
298                    predicates.push(negated);
299                } else if should_is_sparse {
300                    if let Some(bitset) = q.as_doc_bitset(reader) {
301                        log::debug!("BooleanQuery planner 3a: MUST_NOT clause → bitset predicate ({})", q);
302                        predicates.push(Box::new(move |doc_id| !bitset.contains(doc_id)));
303                    } else {
304                        must_not_verifiers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
305                    }
306                } else {
307                    must_not_verifiers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
308                }
309            }
310
311            // 3b. Fast path: pure predicates + sparse SHOULD → BMP or MaxScore w/ predicate
312            if must_verifiers.is_empty()
313                && must_not_verifiers.is_empty()
314                && !predicates.is_empty()
315            {
316                if let Some(infos) = extract_all_sparse_infos(should) {
317                    // Try BMP with bitset first: build compact bitset from MUST/MUST_NOT
318                    // posting lists (O(M) for term queries) for fast per-slot lookup.
319                    let bitset_result = build_combined_bitset(must, must_not, reader);
320                    if let Some(ref bitset) = bitset_result {
321                        let bitset_pred = |doc_id: crate::DocId| bitset.contains(doc_id);
322                        if let Some((raw, info)) =
323                            build_sparse_bmp_results_filtered(&infos, reader, limit, &bitset_pred)
324                        {
325                            log::debug!(
326                                "BooleanQuery planner: bitset-aware sparse BMP, {} dims, {} matching docs",
327                                infos.len(),
328                                bitset.count()
329                            );
330                            return Ok(combine_sparse_results(raw, info.combiner, info.field, limit));
331                        }
332                    }
333
334                    // Fallback: closure predicate (for queries that don't support bitsets)
335                    let combined = chain_predicates(predicates);
336                    if let Some((raw, info)) =
337                        build_sparse_bmp_results_filtered(&infos, reader, limit, &*combined)
338                    {
339                        log::debug!(
340                            "BooleanQuery planner: predicate-aware sparse BMP, {} dims",
341                            infos.len()
342                        );
343                        return Ok(combine_sparse_results(raw, info.combiner, info.field, limit));
344                    }
345                    // Try MaxScore with predicate
346                    if let Some((executor, info)) =
347                        build_sparse_maxscore_executor(&infos, reader, limit, Some(combined))
348                    {
349                        log::debug!(
350                            "BooleanQuery planner: predicate-aware sparse MaxScore, {} dims",
351                            infos.len()
352                        );
353                        let raw = executor.$execute_fn() $(. $aw)* ?;
354                        return Ok(combine_sparse_results(raw, info.combiner, info.field, limit));
355                    }
356                    // predicates consumed — cannot fall through; rebuild them
357                    // (this path only triggers if neither sparse index exists)
358                    // should_is_sparse is true here (we're inside extract_all_sparse_infos)
359                    predicates = Vec::new();
360                    for q in must {
361                        if let Some(pred) = q.as_doc_predicate(reader) {
362                            predicates.push(pred);
363                        } else if let Some(bitset) = q.as_doc_bitset(reader) {
364                            predicates.push(Box::new(move |doc_id| bitset.contains(doc_id)));
365                        }
366                    }
367                    for q in must_not {
368                        if let Some(pred) = q.as_doc_predicate(reader) {
369                            let negated: super::DocPredicate<'_> =
370                                Box::new(move |doc_id| !pred(doc_id));
371                            predicates.push(negated);
372                        } else if let Some(bitset) = q.as_doc_bitset(reader) {
373                            predicates.push(Box::new(move |doc_id| !bitset.contains(doc_id)));
374                        }
375                    }
376                }
377            }
378
379            // 3c. PredicatedScorer fallback (over-fetch 4x when any filter is present)
380            let has_filters = !predicates.is_empty()
381                || !must_verifiers.is_empty()
382                || !must_not_verifiers.is_empty();
383            let should_limit = if has_filters { limit * 4 } else { limit };
384            let should_scorer = if should.len() == 1 {
385                should[0].$scorer_fn(reader, should_limit) $(. $aw)* ?
386            } else {
387                let sub = BooleanQuery {
388                    must: Vec::new(),
389                    should: should.to_vec(),
390                    must_not: Vec::new(),
391                    global_stats: global_stats.cloned(),
392                };
393                sub.$scorer_fn(reader, should_limit) $(. $aw)* ?
394            };
395
396            let use_predicated =
397                must_verifiers.is_empty() || should_scorer.size_hint() >= limit as u32;
398
399            if use_predicated {
400                log::debug!(
401                    "BooleanQuery planner: PredicatedScorer {} preds + {} must_v + {} must_not_v, \
402                     SHOULD size_hint={}, over_fetch={}",
403                    predicates.len(), must_verifiers.len(), must_not_verifiers.len(),
404                    should_scorer.size_hint(), should_limit
405                );
406                return Ok(Box::new(super::PredicatedScorer::new(
407                    should_scorer, predicates, must_verifiers, must_not_verifiers,
408                )));
409            }
410
411            // size_hint < limit with verifiers → BooleanScorer
412            log::debug!(
413                "BooleanQuery planner: BooleanScorer fallback, size_hint={} < limit={}, \
414                 {} must_v + {} must_not_v",
415                should_scorer.size_hint(), limit,
416                must_verifiers.len(), must_not_verifiers.len()
417            );
418            let mut scorer = BooleanScorer {
419                must: must_verifiers,
420                should: vec![should_scorer],
421                must_not: must_not_verifiers,
422                current_doc: 0,
423            };
424            scorer.current_doc = scorer.find_next_match();
425            return Ok(Box::new(scorer));
426        }
427
428        // ── 4. Standard BooleanScorer fallback ───────────────────────────
429        let mut must_scorers = Vec::with_capacity(must.len());
430        for q in must {
431            must_scorers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
432        }
433        let mut should_scorers = Vec::with_capacity(should.len());
434        for q in should {
435            should_scorers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
436        }
437        let mut must_not_scorers = Vec::with_capacity(must_not.len());
438        for q in must_not {
439            must_not_scorers.push(q.$scorer_fn(reader, limit) $(. $aw)* ?);
440        }
441        let mut scorer = BooleanScorer {
442            must: must_scorers,
443            should: should_scorers,
444            must_not: must_not_scorers,
445            current_doc: 0,
446        };
447        scorer.current_doc = scorer.find_next_match();
448        Ok(Box::new(scorer) as Box<dyn Scorer + '_>)
449    }};
450}
451
452impl Query for BooleanQuery {
453    fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a> {
454        let must = self.must.clone();
455        let should = self.should.clone();
456        let must_not = self.must_not.clone();
457        let global_stats = self.global_stats.clone();
458        Box::pin(async move {
459            boolean_plan!(
460                must,
461                should,
462                must_not,
463                global_stats.as_ref(),
464                reader,
465                limit,
466                scorer,
467                get_postings,
468                execute,
469                await
470            )
471        })
472    }
473
474    #[cfg(feature = "sync")]
475    fn scorer_sync<'a>(
476        &self,
477        reader: &'a SegmentReader,
478        limit: usize,
479    ) -> crate::Result<Box<dyn Scorer + 'a>> {
480        boolean_plan!(
481            self.must,
482            self.should,
483            self.must_not,
484            self.global_stats.as_ref(),
485            reader,
486            limit,
487            scorer_sync,
488            get_postings_sync,
489            execute_sync
490        )
491    }
492
493    fn as_doc_bitset(&self, reader: &SegmentReader) -> Option<super::DocBitset> {
494        if self.must.is_empty() && self.should.is_empty() {
495            return None;
496        }
497
498        let num_docs = reader.num_docs();
499
500        // MUST clauses: intersect bitsets (AND)
501        let mut result: Option<super::DocBitset> = None;
502        for q in &self.must {
503            let bs = q.as_doc_bitset(reader)?;
504            match result {
505                None => result = Some(bs),
506                Some(ref mut acc) => acc.intersect_with(&bs),
507            }
508        }
509
510        // SHOULD clauses: union bitsets (OR), then intersect with MUST result
511        if !self.should.is_empty() {
512            let mut should_union = super::DocBitset::new(num_docs);
513            for q in &self.should {
514                let bs = q.as_doc_bitset(reader)?;
515                should_union.union_with(&bs);
516            }
517            match result {
518                None => result = Some(should_union),
519                Some(ref mut acc) => {
520                    // When MUST clauses exist, SHOULD is optional (doesn't filter).
521                    // When no MUST clauses, at least one SHOULD must match.
522                    if self.must.is_empty() {
523                        *acc = should_union;
524                    }
525                }
526            }
527        }
528
529        // MUST_NOT clauses: subtract bitsets (ANDNOT)
530        if let Some(ref mut acc) = result {
531            for q in &self.must_not {
532                {
533                    let bs = q.as_doc_bitset(reader)?;
534                    acc.subtract(&bs);
535                }
536            }
537        }
538
539        result
540    }
541
542    fn as_doc_predicate<'a>(&self, reader: &'a SegmentReader) -> Option<super::DocPredicate<'a>> {
543        // Need at least some clauses
544        if self.must.is_empty() && self.should.is_empty() {
545            return None;
546        }
547
548        // Try converting all clauses to predicates; bail if any child can't
549        let must_preds: Vec<_> = self
550            .must
551            .iter()
552            .map(|q| q.as_doc_predicate(reader))
553            .collect::<Option<Vec<_>>>()?;
554        let should_preds: Vec<_> = self
555            .should
556            .iter()
557            .map(|q| q.as_doc_predicate(reader))
558            .collect::<Option<Vec<_>>>()?;
559        let must_not_preds: Vec<_> = self
560            .must_not
561            .iter()
562            .map(|q| q.as_doc_predicate(reader))
563            .collect::<Option<Vec<_>>>()?;
564
565        let has_must = !must_preds.is_empty();
566
567        Some(Box::new(move |doc_id| {
568            // All MUST predicates must pass
569            if !must_preds.iter().all(|p| p(doc_id)) {
570                return false;
571            }
572            // When there are no MUST clauses, at least one SHOULD must pass
573            if !has_must && !should_preds.is_empty() && !should_preds.iter().any(|p| p(doc_id)) {
574                return false;
575            }
576            // No MUST_NOT predicate should pass
577            must_not_preds.iter().all(|p| !p(doc_id))
578        }))
579    }
580
581    fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a> {
582        let must = self.must.clone();
583        let should = self.should.clone();
584
585        Box::pin(async move {
586            if !must.is_empty() {
587                let mut estimates = Vec::with_capacity(must.len());
588                for q in &must {
589                    estimates.push(q.count_estimate(reader).await?);
590                }
591                estimates
592                    .into_iter()
593                    .min()
594                    .ok_or_else(|| crate::Error::Corruption("Empty must clause".to_string()))
595            } else if !should.is_empty() {
596                let mut sum = 0u32;
597                for q in &should {
598                    sum = sum.saturating_add(q.count_estimate(reader).await?);
599                }
600                Ok(sum)
601            } else {
602                Ok(0)
603            }
604        })
605    }
606}
607
608struct BooleanScorer<'a> {
609    must: Vec<Box<dyn Scorer + 'a>>,
610    should: Vec<Box<dyn Scorer + 'a>>,
611    must_not: Vec<Box<dyn Scorer + 'a>>,
612    current_doc: DocId,
613}
614
615impl BooleanScorer<'_> {
616    fn find_next_match(&mut self) -> DocId {
617        if self.must.is_empty() && self.should.is_empty() {
618            return TERMINATED;
619        }
620
621        loop {
622            let candidate = if !self.must.is_empty() {
623                let mut max_doc = self
624                    .must
625                    .iter()
626                    .map(|s| s.doc())
627                    .max()
628                    .unwrap_or(TERMINATED);
629
630                if max_doc == TERMINATED {
631                    return TERMINATED;
632                }
633
634                loop {
635                    let mut all_match = true;
636                    for scorer in &mut self.must {
637                        let doc = scorer.seek(max_doc);
638                        if doc == TERMINATED {
639                            return TERMINATED;
640                        }
641                        if doc > max_doc {
642                            max_doc = doc;
643                            all_match = false;
644                            break;
645                        }
646                    }
647                    if all_match {
648                        break;
649                    }
650                }
651                max_doc
652            } else {
653                self.should
654                    .iter()
655                    .map(|s| s.doc())
656                    .filter(|&d| d != TERMINATED)
657                    .min()
658                    .unwrap_or(TERMINATED)
659            };
660
661            if candidate == TERMINATED {
662                return TERMINATED;
663            }
664
665            let excluded = self.must_not.iter_mut().any(|scorer| {
666                let doc = scorer.seek(candidate);
667                doc == candidate
668            });
669
670            if !excluded {
671                // Seek SHOULD scorers to candidate so score() can see their contributions
672                for scorer in &mut self.should {
673                    scorer.seek(candidate);
674                }
675                self.current_doc = candidate;
676                return candidate;
677            }
678
679            // Advance past excluded candidate
680            if !self.must.is_empty() {
681                for scorer in &mut self.must {
682                    scorer.advance();
683                }
684            } else {
685                // For SHOULD-only: seek all scorers past the excluded candidate
686                for scorer in &mut self.should {
687                    if scorer.doc() <= candidate && scorer.doc() != TERMINATED {
688                        scorer.seek(candidate + 1);
689                    }
690                }
691            }
692        }
693    }
694}
695
696impl super::docset::DocSet for BooleanScorer<'_> {
697    fn doc(&self) -> DocId {
698        self.current_doc
699    }
700
701    fn advance(&mut self) -> DocId {
702        if !self.must.is_empty() {
703            for scorer in &mut self.must {
704                scorer.advance();
705            }
706        } else {
707            for scorer in &mut self.should {
708                if scorer.doc() == self.current_doc {
709                    scorer.advance();
710                }
711            }
712        }
713
714        self.current_doc = self.find_next_match();
715        self.current_doc
716    }
717
718    fn seek(&mut self, target: DocId) -> DocId {
719        for scorer in &mut self.must {
720            scorer.seek(target);
721        }
722
723        for scorer in &mut self.should {
724            scorer.seek(target);
725        }
726
727        self.current_doc = self.find_next_match();
728        self.current_doc
729    }
730
731    fn size_hint(&self) -> u32 {
732        if !self.must.is_empty() {
733            self.must.iter().map(|s| s.size_hint()).min().unwrap_or(0)
734        } else {
735            self.should.iter().map(|s| s.size_hint()).sum()
736        }
737    }
738}
739
740impl Scorer for BooleanScorer<'_> {
741    fn score(&self) -> Score {
742        let mut total = 0.0;
743
744        for scorer in &self.must {
745            if scorer.doc() == self.current_doc {
746                total += scorer.score();
747            }
748        }
749
750        for scorer in &self.should {
751            if scorer.doc() == self.current_doc {
752                total += scorer.score();
753            }
754        }
755
756        total
757    }
758
759    fn matched_positions(&self) -> Option<super::MatchedPositions> {
760        let mut all_positions: super::MatchedPositions = Vec::new();
761
762        for scorer in &self.must {
763            if scorer.doc() == self.current_doc
764                && let Some(positions) = scorer.matched_positions()
765            {
766                all_positions.extend(positions);
767            }
768        }
769
770        for scorer in &self.should {
771            if scorer.doc() == self.current_doc
772                && let Some(positions) = scorer.matched_positions()
773            {
774                all_positions.extend(positions);
775            }
776        }
777
778        if all_positions.is_empty() {
779            None
780        } else {
781            Some(all_positions)
782        }
783    }
784}
785
786#[cfg(test)]
787mod tests {
788    use super::*;
789    use crate::dsl::Field;
790    use crate::query::{QueryDecomposition, TermQuery};
791
792    #[test]
793    fn test_maxscore_eligible_pure_or_same_field() {
794        // Pure OR query with multiple terms in same field should be MaxScore-eligible
795        let query = BooleanQuery::new()
796            .should(TermQuery::text(Field(0), "hello"))
797            .should(TermQuery::text(Field(0), "world"))
798            .should(TermQuery::text(Field(0), "foo"));
799
800        // All clauses should return term info
801        assert!(
802            query
803                .should
804                .iter()
805                .all(|q| matches!(q.decompose(), QueryDecomposition::TextTerm(_)))
806        );
807
808        // All should be same field
809        let infos: Vec<_> = query
810            .should
811            .iter()
812            .filter_map(|q| match q.decompose() {
813                QueryDecomposition::TextTerm(info) => Some(info),
814                _ => None,
815            })
816            .collect();
817        assert_eq!(infos.len(), 3);
818        assert!(infos.iter().all(|i| i.field == Field(0)));
819    }
820
821    #[test]
822    fn test_maxscore_not_eligible_different_fields() {
823        // OR query with terms in different fields should NOT use MaxScore
824        let query = BooleanQuery::new()
825            .should(TermQuery::text(Field(0), "hello"))
826            .should(TermQuery::text(Field(1), "world")); // Different field!
827
828        let infos: Vec<_> = query
829            .should
830            .iter()
831            .filter_map(|q| match q.decompose() {
832                QueryDecomposition::TextTerm(info) => Some(info),
833                _ => None,
834            })
835            .collect();
836        assert_eq!(infos.len(), 2);
837        // Fields are different, MaxScore should not be used
838        assert!(infos[0].field != infos[1].field);
839    }
840
841    #[test]
842    fn test_maxscore_not_eligible_with_must() {
843        // Query with MUST clause should NOT use MaxScore optimization
844        let query = BooleanQuery::new()
845            .must(TermQuery::text(Field(0), "required"))
846            .should(TermQuery::text(Field(0), "hello"))
847            .should(TermQuery::text(Field(0), "world"));
848
849        // Has MUST clause, so MaxScore optimization should not kick in
850        assert!(!query.must.is_empty());
851    }
852
853    #[test]
854    fn test_maxscore_not_eligible_with_must_not() {
855        // Query with MUST_NOT clause should NOT use MaxScore optimization
856        let query = BooleanQuery::new()
857            .should(TermQuery::text(Field(0), "hello"))
858            .should(TermQuery::text(Field(0), "world"))
859            .must_not(TermQuery::text(Field(0), "excluded"));
860
861        // Has MUST_NOT clause, so MaxScore optimization should not kick in
862        assert!(!query.must_not.is_empty());
863    }
864
865    #[test]
866    fn test_maxscore_not_eligible_single_term() {
867        // Single SHOULD clause should NOT use MaxScore (no benefit)
868        let query = BooleanQuery::new().should(TermQuery::text(Field(0), "hello"));
869
870        // Only one term, MaxScore not beneficial
871        assert_eq!(query.should.len(), 1);
872    }
873
874    #[test]
875    fn test_term_query_info_extraction() {
876        let term_query = TermQuery::text(Field(42), "test");
877        match term_query.decompose() {
878            QueryDecomposition::TextTerm(info) => {
879                assert_eq!(info.field, Field(42));
880                assert_eq!(info.term, b"test");
881            }
882            _ => panic!("Expected TextTerm decomposition"),
883        }
884    }
885
886    #[test]
887    fn test_boolean_query_no_term_info() {
888        // BooleanQuery itself should not return term info
889        let query = BooleanQuery::new().should(TermQuery::text(Field(0), "hello"));
890
891        assert!(matches!(query.decompose(), QueryDecomposition::Opaque));
892    }
893}