fix(registry): ignore search query casing

This commit is contained in:
daimond113 2024-11-06 21:22:57 +01:00
parent ab9124e02c
commit e2fe1c50b8
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
3 changed files with 18 additions and 16 deletions

View file

@ -28,23 +28,12 @@ pub async fn search_packages(
let id = schema.get_field("id").unwrap();
let scope = schema.get_field("scope").unwrap();
let name = schema.get_field("name").unwrap();
let description = schema.get_field("description").unwrap();
let query = request.query.as_deref().unwrap_or_default().trim();
let query = if query.is_empty() {
Box::new(AllQuery)
} else {
let mut query_parser = tantivy::query::QueryParser::for_index(
searcher.index(),
vec![scope, name, description],
);
query_parser.set_field_boost(scope, 2.0);
query_parser.set_field_boost(name, 3.5);
query_parser.parse_query(query)?
app_state.query_parser.parse_query(query)?
};
let (count, top_docs) = searcher

View file

@ -45,6 +45,7 @@ pub struct AppState {
pub search_reader: tantivy::IndexReader,
pub search_writer: std::sync::Mutex<tantivy::IndexWriter>,
pub query_parser: tantivy::query::QueryParser,
}
#[macro_export]
@ -105,7 +106,7 @@ async fn run() -> std::io::Result<()> {
.await
.expect("failed to refresh source");
let (search_reader, search_writer) = make_search(&project, &source);
let (search_reader, search_writer, query_parser) = make_search(&project, &source);
let app_data = web::Data::new(AppState {
storage: {
@ -124,6 +125,7 @@ async fn run() -> std::io::Result<()> {
search_reader,
search_writer: std::sync::Mutex::new(search_writer),
query_parser,
});
let publish_governor_config = GovernorConfigBuilder::default()

View file

@ -6,11 +6,16 @@ use pesde::{
};
use tantivy::{
doc,
query::QueryParser,
schema::{IndexRecordOption, TextFieldIndexing, TextOptions, FAST, STORED, STRING},
tokenizer::TextAnalyzer,
DateTime, IndexReader, IndexWriter, Term,
};
pub fn make_search(project: &Project, source: &PesdePackageSource) -> (IndexReader, IndexWriter) {
pub fn make_search(
project: &Project,
source: &PesdePackageSource,
) -> (IndexReader, IndexWriter, QueryParser) {
let mut schema_builder = tantivy::schema::SchemaBuilder::new();
let field_options = TextOptions::default().set_indexing_options(
@ -28,7 +33,9 @@ pub fn make_search(project: &Project, source: &PesdePackageSource) -> (IndexRead
let search_index = tantivy::Index::create_in_ram(schema_builder.build());
search_index.tokenizers().register(
"ngram",
tantivy::tokenizer::NgramTokenizer::all_ngrams(1, 12).unwrap(),
TextAnalyzer::builder(tantivy::tokenizer::NgramTokenizer::all_ngrams(1, 12).unwrap())
.filter(tantivy::tokenizer::LowerCaser)
.build(),
);
let search_reader = search_index
@ -56,7 +63,11 @@ pub fn make_search(project: &Project, source: &PesdePackageSource) -> (IndexRead
search_writer.commit().unwrap();
search_reader.reload().unwrap();
(search_reader, search_writer)
let mut query_parser = QueryParser::for_index(&search_index, vec![scope, name, description]);
query_parser.set_field_boost(scope, 2.0);
query_parser.set_field_boost(name, 3.5);
(search_reader, search_writer, query_parser)
}
pub fn update_version(app_state: &AppState, name: &PackageName, entry: IndexFileEntry) {