Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an API function to get documents count in index. #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lnx-engine/search-index/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ impl Index {
self.0.clear_documents().await
}

/// Gets count of searchable documents in the index.
pub async fn get_documents_count(&self) -> Result<usize> {
self.0.get_documents_count().await
}

/// Adds a set of stop words to the indexes' stop word manager.
///
/// This function is semi-asynchronous in the sense that there is a buffer of
Expand Down Expand Up @@ -231,6 +236,18 @@ impl InternalIndex {
self.writer.send_op(WriterOp::DeleteAll).await
}

/// Gets count of searchable documents in the index.
async fn get_documents_count(&self) -> Result<usize> {
if let Ok(metas) = self._ctx.index.searchable_segment_metas() {
let mut count = 0usize;
for m in metas {
count += m.num_docs() as usize;
}
return Ok(count);
}
Ok(0)
}

/// Deletes a specific document
pub async fn delete_document(&self, document_id: DocumentId) -> Result<()> {
self.writer
Expand Down
12 changes: 12 additions & 0 deletions lnx-server/src/routes/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,15 @@ pub async fn clear_documents(req: LnxRequest) -> LnxResponse {

json_response(200, "changes registered")
}

/// Gets count of searchable documents in the index.
pub async fn get_documents_count(req: LnxRequest) -> LnxResponse {
let state = req.data::<State>().expect("get state");
let index = get_or_400!(req.param("index"));
let index: Index =
get_or_400!(state.engine.get_index(index), "index does not exist");

let count = index.get_documents_count().await?;

json_response(200, &format!("{}", count))
}
1 change: 1 addition & 0 deletions lnx-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn get_router(state: State) -> Router<Body, LnxError> {
.post("/indexes/:index/search", index::search_index)
.post("/indexes/:index/hint", index::get_corrected_query_hint)
.post("/indexes/:index/documents", index::add_documents)
.get("/indexes/:index/documents/count", index::get_documents_count)
.get("/indexes/:index/stopwords", index::get_stop_words)
.post("/indexes/:index/stopwords", index::add_stop_words)
.delete("/indexes/:index/stopwords", index::remove_stop_words)
Expand Down