rlucene 0.1.0-alpha.3

A Rust port of Apache Lucene, a full-text search engine library
Documentation
/*
 * 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::util::bits::Bits;
use crate::core::util::error::lucene_error::Result;

/// This trait is used to score a range of documents at once, and is returned by [`Weight::bulk_scorer`](crate::core::search::weight::Weight::bulk_scorer).
///
/// Only queries that have a more optimized means of scoring across a range of
/// documents need to provide this method. Otherwise, a default implementation is
/// wrapped around the [`Scorer`](crate::core::search::scorer::Scorer) returned by [`Weight::scorer`](crate::core::search::weight::Weight::scorer).
pub trait BulkScorer {
  /// Collects matching documents in a range and returns an estimation of the
  /// next matching document which is on or after `max`.
  ///
  /// # Return value
  ///
  /// - `>= max`
  /// - [`NO_MORE_DOCS`](crate::core::search::doc_id_set_iterator::NO_MORE_DOCS) if there are no more matches
  /// - `<=` the first matching document that is `>= max` otherwise
  ///
  /// # Parameters
  ///
  /// - `collector`: The collector to which all matching documents are passed.
  /// - `accept_docs`: [`Bits`] that represents the allowed documents to match,
  ///   or `None` if all are allowed to match.
  /// - `min`: Score starting at, including, this document.
  /// - `max`: Score up to, but not including, this doc.
  ///
  /// # Notes
  ///
  /// - `min` is the minimum document to be considered for matching. All documents
  ///   strictly before this value must be ignored.
  /// - Although `max` would be a legal return value for this method, higher values
  ///   might help callers skip more efficiently over non-matching portions of the
  ///   docID space.
  ///
  /// # Returns
  ///
  /// An under-estimation of the next matching doc after `max`.
  fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
    &mut self,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    min: i32,
    max: i32,
  ) -> Result<i32>;

  /// Same as [`DocIdSetIterator::cost`](crate::core::search::doc_id_set_iterator::DocIdSetIterator::cost) for bulk scorers.
  fn cost(&mut self) -> Result<i64>;

  #[cfg(test)]
  fn kind(&self) -> BulkScorerKind {
    unimplemented!("")
  }
}
#[cfg(test)]
pub enum BulkScorerKind {
  Asserting,
  Default,
  ReqExcl,
}
macro_rules! either_bulk_scorer {
    ($vis:vis $name:ident { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
        $vis enum $name<$( $T ),+> {
            $( $Variant($T), )+
        }

        impl<$( $T ),+> BulkScorer for $name<$( $T ),+>
        where
            $( $T: BulkScorer ),+
        {
            #[inline]
            fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
                &mut self,
                collector: &mut CLC,
                accept_docs: Option<&AB>,
                min: i32,
                max: i32,
            ) -> Result<i32> {
                match self {
                    $( Self::$Variant(inner) => inner.score(collector, accept_docs, min, max), )+
                }
            }

            #[inline]
            fn cost(&mut self) -> Result<i64> {
                match self {
                    $( Self::$Variant(inner) => inner.cost(), )+
                }
            }
             #[cfg(test)]
            fn kind(&self) -> BulkScorerKind{
                match self { $( Self::$Variant(inner) => inner.kind(), )+ }
            }
        }
    };
}
either_bulk_scorer!(pub BulkScorerEnum2 { A: A1, B: B1});
either_bulk_scorer!(pub BulkScorerEnum4 { A: A1, B: B1, C: C1, D: D1});
impl<T> BulkScorer for Box<T>
where
  T: BulkScorer + ?Sized,
{
  fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
    &mut self,
    collector: &mut CLC,
    accept_docs: Option<&AB>,
    min: i32,
    max: i32,
  ) -> Result<i32> {
    (**self).score(collector, accept_docs, min, max)
  }

  fn cost(&mut self) -> Result<i64> {
    (**self).cost()
  }
  #[cfg(test)]
  fn kind(&self) -> BulkScorerKind {
    (**self).kind()
  }
}