Skip to content

web search done right

Score goggles as a const (while taking boost into account). Also added a goggles scale to prioritize goggles more

Mikkel Denker78b238d2bb08
diff --git a/src/ranking/goggles/const_query.rs b/src/ranking/goggles/const_query.rs
new file mode 100644
index 00000000..3a519b23
--- /dev/null
+++ b/src/ranking/goggles/const_query.rs
@@ -0,0 +1,108 @@
+// Cuely is an open source web search engine.
+// Copyright (C) 2022 Cuely ApS
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+use tantivy::{
+ query::{Explanation, Query, Scorer, Weight},
+ DocSet, Score, Searcher,
+};
+
+#[derive(Debug)]
+pub struct ConstQuery {
+ subquery: Box<dyn Query>,
+ score: Score,
+}
+
+impl ConstQuery {
+ pub fn new(subquery: Box<dyn Query>, score: Score) -> Self {
+ Self { subquery, score }
+ }
+}
+
+impl Clone for ConstQuery {
+ fn clone(&self) -> Self {
+ Self {
+ subquery: self.subquery.box_clone(),
+ score: self.score,
+ }
+ }
+}
+
+impl Query for ConstQuery {
+ fn weight(
+ &self,
+ searcher: &Searcher,
+ scoring_enabled: bool,
+ ) -> tantivy::Result<Box<dyn Weight>> {
+ Ok(Box::new(ConstWeight {
+ subweight: self.subquery.weight(searcher, scoring_enabled)?,
+ score: self.score,
+ }))
+ }
+}
+
+struct ConstWeight {
+ subweight: Box<dyn Weight>,
+ score: Score,
+}
+
+impl Weight for ConstWeight {
+ fn scorer(
+ &self,
+ reader: &tantivy::SegmentReader,
+ boost: Score,
+ ) -> tantivy::Result<Box<dyn Scorer>> {
+ let subscorer = self.subweight.scorer(reader, boost)?;
+ Ok(Box::new(ConstScorer {
+ subscorer,
+ score: boost,
+ }))
+ }
+
+ fn explain(
+ &self,
+ reader: &tantivy::SegmentReader,
+ doc: tantivy::DocId,
+ ) -> tantivy::Result<tantivy::query::Explanation> {
+ let mut expl = Explanation::new("Constant score for query", self.score);
+ expl.add_detail(self.subweight.explain(reader, doc)?);
+ Ok(expl)
+ }
+}
+
+struct ConstScorer {
+ subscorer: Box<dyn Scorer>,
+ score: Score,
+}
+
+impl Scorer for ConstScorer {
+ fn score(&mut self) -> Score {
+ self.score
+ }
+}
+
+impl DocSet for ConstScorer {
+ fn advance(&mut self) -> tantivy::DocId {
+ self.subscorer.advance()
+ }
+
+ fn doc(&self) -> tantivy::DocId {
+ self.subscorer.doc()
+ }
+
+ fn size_hint(&self) -> u32 {
+ self.subscorer.size_hint()
+ }
+}
diff --git a/src/ranking/goggles/mod.rs b/src/ranking/goggles/mod.rs
index a18c88c0..d7a7bf69 100644
--- a/src/ranking/goggles/mod.rs
+++ b/src/ranking/goggles/mod.rs
@@ -14,7 +14,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+const SCALE: f32 = 1000.0;
+
pub mod ast;
+mod const_query;
mod pattern_query;
use std::convert::TryFrom;
@@ -28,6 +31,7 @@ use tantivy::{
use self::{
ast::{RawAction, RawGoggle, RawInstruction, RawPatternOption, RawPatternPart},
+ const_query::ConstQuery,
pattern_query::PatternQuery,
};
@@ -253,11 +257,19 @@ impl Instruction {
match action {
Action::Boost(boost) => Some((
Occur::Should,
- BoostQuery::new(subquery, boost as f32 + 1.0).box_clone(),
+ BoostQuery::new(
+ ConstQuery::new(subquery, 1.0).box_clone(),
+ boost as f32 * SCALE,
+ )
+ .box_clone(),
)),
Action::Downrank(boost) => Some((
Occur::Should,
- BoostQuery::new(subquery, 1.0 / (boost as f32 + 1.0)).box_clone(),
+ BoostQuery::new(
+ ConstQuery::new(subquery, 1.0).box_clone(),
+ boost as f32 * -SCALE,
+ )
+ .box_clone(),
)),
Action::Discard => Some((Occur::MustNot, subquery)),
}