Filter ranked search by public source and excluded text before pagination

This commit is contained in:
2026-12-03 03:20:18 +08:00
parent c0b5455582
commit 20d3d3fc62
7 changed files with 113 additions and 8 deletions
+1
View File
@@ -1,6 +1,7 @@
pub(crate) mod analysis;
pub(crate) mod rank;
pub(crate) mod retrieve;
mod scope;
use crate::{Document, Kind, Media};
+10 -6
View File
@@ -1,4 +1,4 @@
use super::{analysis, rank};
use super::{analysis, rank, scope::Scope};
use crate::{index::IndexReader, DocumentId, Kind, Query, SearchPage};
use std::{
cmp::{Ordering, Reverse},
@@ -37,7 +37,10 @@ pub(crate) fn retrieve<R: IndexReader>(reader: &R, query: &Query) -> Result<Sear
total: 0,
hits: Vec::new(),
};
let text = analysis::normalize(query.text);
let normalized = analysis::normalize(query.text);
let Some((scope, text)) = Scope::parse(&normalized) else {
return Ok(empty());
};
// Only closed quote pairs are operators; an unmatched trailing quote is punctuation.
let parts: Vec<_> = text.split('"').skip(1).collect();
let phrases: Vec<_> = parts
@@ -46,7 +49,7 @@ pub(crate) fn retrieve<R: IndexReader>(reader: &R, query: &Query) -> Result<Sear
.filter(|phrase| !phrase.is_empty())
.collect();
let mut lists = Vec::new();
for term in analysis::query_terms(&text) {
for term in analysis::query_terms(text) {
let Some(postings) = reader.postings(&term)? else {
return Ok(empty());
};
@@ -66,7 +69,8 @@ pub(crate) fn retrieve<R: IndexReader>(reader: &R, query: &Query) -> Result<Sear
continue;
}
let entry = reader.document(*id)?;
if query.kind.is_some_and(|kind| kind != entry.document.kind())
if !scope.accepts(&entry)
|| query.kind.is_some_and(|kind| kind != entry.document.kind())
|| query
.media
.is_some_and(|media| Some(media) != entry.document.media)
@@ -87,9 +91,9 @@ pub(crate) fn retrieve<R: IndexReader>(reader: &R, query: &Query) -> Result<Sear
.iter()
.map(|list| rank::term_score(list[id], entry.lengths, avg, n, list.len()))
.sum();
if entry.title.contains(&text) {
if entry.title.contains(text) {
score += 2.0;
} else if entry.body.contains(&text) {
} else if entry.body.contains(text) {
score += 0.5;
}
let exact = entry.document.kind() != Kind::Post
+42
View File
@@ -0,0 +1,42 @@
//! Leading source and exclusion filters in normalized search text.
use crate::{index::IndexedDocument, PublicName};
#[derive(Default)]
pub(super) struct Scope<'a> {
source: Option<PublicName>,
excluded: Vec<&'a str>,
}
impl<'a> Scope<'a> {
pub fn parse(mut text: &'a str) -> Option<(Self, &'a str)> {
let mut scope = Self::default();
loop {
let (token, rest) = text.split_once(' ').unwrap_or((text, ""));
if let Some(source) = token.strip_prefix("from:") {
let name = PublicName::new(source)?;
if scope.source.as_ref().is_some_and(|s| s != &name) {
return None;
}
scope.source = Some(name);
} else if let Some(fragment) = token.strip_prefix("exclude:") {
if fragment.is_empty() {
return None;
}
scope.excluded.push(fragment);
} else {
return Some((scope, text));
}
text = rest;
}
}
pub fn accepts(&self, entry: &IndexedDocument) -> bool {
self.source
.as_ref()
.is_none_or(|s| s == &entry.document.username)
&& !self
.excluded
.iter()
.any(|f| entry.title.contains(f) || entry.body.contains(f))
}
}