Skip to content

Commit 2ae14fc

Browse files
refactor(tantivy): remove MoreLikeThisQuery feature
Remove MoreLikeThisQuery and all related code as requested: - Remove MoreLikeThisOptions and MoreLikeThisRequest types from lib.rs - Remove more_like_this command from commands.rs - Remove more_like_this method from ext.rs - Remove MoreLikeThisQuery import from ext.rs - Regenerate TypeScript bindings Co-Authored-By: yujonglee <yujonglee.dev@gmail.com>
1 parent 3a93a17 commit 2ae14fc

File tree

4 files changed

+4
-137
lines changed

4 files changed

+4
-137
lines changed

plugins/tantivy/js/bindings.gen.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ async removeDocument(id: string, collection: string | null) : Promise<Result<nul
4545
if(e instanceof Error) throw e;
4646
else return { status: "error", error: e as any };
4747
}
48-
},
49-
async moreLikeThis(request: MoreLikeThisRequest) : Promise<Result<SearchResult, string>> {
50-
try {
51-
return { status: "ok", data: await TAURI_INVOKE("plugin:tantivy|more_like_this", { request }) };
52-
} catch (e) {
53-
if(e instanceof Error) throw e;
54-
else return { status: "error", error: e as any };
55-
}
5648
}
5749
}
5850

@@ -68,8 +60,6 @@ async moreLikeThis(request: MoreLikeThisRequest) : Promise<Result<SearchResult,
6860

6961
export type CreatedAtFilter = { gte: number | null; lte: number | null; gt: number | null; lt: number | null; eq: number | null }
7062
export type HighlightRange = { start: number; end: number }
71-
export type MoreLikeThisOptions = { min_doc_frequency: number | null; max_doc_frequency: number | null; min_term_frequency: number | null; min_word_length: number | null; max_word_length: number | null; boost_factor: number | null; stop_words: string[] | null }
72-
export type MoreLikeThisRequest = { document_id: string; collection?: string | null; limit?: number; options?: MoreLikeThisOptions }
7363
export type SearchDocument = { id: string; doc_type: string; language: string | null; title: string; content: string; created_at: number; facets?: string[] }
7464
export type SearchFilters = { created_at: CreatedAtFilter | null; doc_type: string | null; facet: string | null }
7565
export type SearchHit = { score: number; document: SearchDocument; title_snippet: Snippet | null; content_snippet: Snippet | null }

plugins/tantivy/src/commands.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{MoreLikeThisRequest, SearchDocument, SearchRequest, SearchResult, TantivyPluginExt};
1+
use crate::{SearchDocument, SearchRequest, SearchResult, TantivyPluginExt};
22

33
#[tauri::command]
44
#[specta::specta]
@@ -62,15 +62,3 @@ pub(crate) async fn remove_document<R: tauri::Runtime>(
6262
.await
6363
.map_err(|e| e.to_string())
6464
}
65-
66-
#[tauri::command]
67-
#[specta::specta]
68-
pub(crate) async fn more_like_this<R: tauri::Runtime>(
69-
app: tauri::AppHandle<R>,
70-
request: MoreLikeThisRequest,
71-
) -> Result<SearchResult, String> {
72-
app.tantivy()
73-
.more_like_this(request)
74-
.await
75-
.map_err(|e| e.to_string())
76-
}

plugins/tantivy/src/ext.rs

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use tantivy::collector::{Count, TopDocs};
22
use tantivy::query::{
3-
BooleanQuery, BoostQuery, FuzzyTermQuery, MoreLikeThisQuery, Occur, PhraseQuery, Query,
4-
QueryParser, TermQuery,
3+
BooleanQuery, BoostQuery, FuzzyTermQuery, Occur, PhraseQuery, Query, QueryParser, TermQuery,
54
};
65
use tantivy::schema::{Facet, IndexRecordOption};
76
use tantivy::snippet::SnippetGenerator;
@@ -12,8 +11,8 @@ use crate::query::build_created_at_range_query;
1211
use crate::schema::{extract_search_document, get_fields};
1312
use crate::tokenizer::register_tokenizers;
1413
use crate::{
15-
CollectionConfig, CollectionIndex, HighlightRange, IndexState, MoreLikeThisRequest,
16-
SearchDocument, SearchHit, SearchRequest, SearchResult, Snippet,
14+
CollectionConfig, CollectionIndex, HighlightRange, IndexState, SearchDocument, SearchHit,
15+
SearchRequest, SearchResult, Snippet,
1716
};
1817

1918
pub fn detect_language(text: &str) -> hypr_language::Language {
@@ -502,93 +501,6 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Tantivy<'a, R, M> {
502501

503502
Ok(())
504503
}
505-
506-
pub async fn more_like_this(
507-
&self,
508-
request: MoreLikeThisRequest,
509-
) -> Result<SearchResult, crate::Error> {
510-
let collection_name = Self::get_collection_name(request.collection);
511-
let state = self.manager.state::<IndexState>();
512-
let guard = state.inner.lock().await;
513-
514-
let collection_index = guard
515-
.collections
516-
.get(&collection_name)
517-
.ok_or_else(|| crate::Error::CollectionNotFound(collection_name.clone()))?;
518-
519-
let schema = &collection_index.schema;
520-
let reader = &collection_index.reader;
521-
522-
let fields = get_fields(schema);
523-
let searcher = reader.searcher();
524-
525-
let id_term = Term::from_field_text(fields.id, &request.document_id);
526-
let id_query = TermQuery::new(id_term, IndexRecordOption::Basic);
527-
528-
let (top_docs, _) = searcher.search(&id_query, &(TopDocs::with_limit(1), Count))?;
529-
530-
let (_, doc_address) = top_docs
531-
.first()
532-
.ok_or_else(|| crate::Error::DocumentNotFound(request.document_id.clone()))?;
533-
534-
let mut mlt_builder = MoreLikeThisQuery::builder();
535-
536-
if let Some(min_doc_freq) = request.options.min_doc_frequency {
537-
mlt_builder = mlt_builder.with_min_doc_frequency(min_doc_freq);
538-
}
539-
if let Some(max_doc_freq) = request.options.max_doc_frequency {
540-
mlt_builder = mlt_builder.with_max_doc_frequency(max_doc_freq);
541-
}
542-
if let Some(min_term_freq) = request.options.min_term_frequency {
543-
mlt_builder = mlt_builder.with_min_term_frequency(min_term_freq);
544-
}
545-
if let Some(min_word_len) = request.options.min_word_length {
546-
mlt_builder = mlt_builder.with_min_word_length(min_word_len);
547-
}
548-
if let Some(max_word_len) = request.options.max_word_length {
549-
mlt_builder = mlt_builder.with_max_word_length(max_word_len);
550-
}
551-
if let Some(boost) = request.options.boost_factor {
552-
mlt_builder = mlt_builder.with_boost_factor(boost);
553-
}
554-
if let Some(ref stop_words) = request.options.stop_words {
555-
mlt_builder = mlt_builder.with_stop_words(stop_words.clone());
556-
}
557-
558-
let mlt_query = mlt_builder.with_document(*doc_address);
559-
560-
let (similar_docs, count) =
561-
searcher.search(&mlt_query, &(TopDocs::with_limit(request.limit + 1), Count))?;
562-
563-
let mut hits = Vec::new();
564-
for (score, similar_doc_address) in similar_docs {
565-
if similar_doc_address == *doc_address {
566-
continue;
567-
}
568-
569-
let retrieved_doc: TantivyDocument = searcher.doc(similar_doc_address)?;
570-
571-
if let Some(search_doc) = extract_search_document(schema, &fields, &retrieved_doc) {
572-
hits.push(SearchHit {
573-
score,
574-
document: search_doc,
575-
title_snippet: None,
576-
content_snippet: None,
577-
});
578-
}
579-
580-
if hits.len() >= request.limit {
581-
break;
582-
}
583-
}
584-
585-
let actual_count = if count > 0 { count - 1 } else { 0 };
586-
587-
Ok(SearchResult {
588-
hits,
589-
count: actual_count,
590-
})
591-
}
592504
}
593505

594506
pub trait TantivyPluginExt<R: tauri::Runtime> {

plugins/tantivy/src/lib.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,6 @@ pub struct SearchResult {
5757
pub count: usize,
5858
}
5959

60-
#[derive(Debug, Clone, Default, Serialize, Deserialize, specta::Type)]
61-
pub struct MoreLikeThisOptions {
62-
pub min_doc_frequency: Option<u64>,
63-
pub max_doc_frequency: Option<u64>,
64-
pub min_term_frequency: Option<usize>,
65-
pub min_word_length: Option<usize>,
66-
pub max_word_length: Option<usize>,
67-
pub boost_factor: Option<f32>,
68-
pub stop_words: Option<Vec<String>>,
69-
}
70-
71-
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type)]
72-
pub struct MoreLikeThisRequest {
73-
pub document_id: String,
74-
#[serde(default)]
75-
pub collection: Option<String>,
76-
#[serde(default = "default_limit")]
77-
pub limit: usize,
78-
#[serde(default)]
79-
pub options: MoreLikeThisOptions,
80-
}
81-
8260
#[derive(Debug, Clone, Default, Serialize, Deserialize, specta::Type)]
8361
pub struct CreatedAtFilter {
8462
pub gte: Option<i64>,
@@ -168,7 +146,6 @@ fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
168146
commands::add_document::<tauri::Wry>,
169147
commands::update_document::<tauri::Wry>,
170148
commands::remove_document::<tauri::Wry>,
171-
commands::more_like_this::<tauri::Wry>,
172149
])
173150
.error_handling(tauri_specta::ErrorHandlingMode::Result)
174151
}

0 commit comments

Comments
 (0)