Skip to content

Commit

Permalink
Improve merging crates with keywords and categories
Browse files Browse the repository at this point in the history
(cherry picked from commit 3aad767)
  • Loading branch information
Dalvany committed Jun 2, 2023
1 parent 8bea9b2 commit fdd1c9a
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions crates/alexandrie/src/fts/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::convert::TryFrom;
use std::num::NonZeroUsize;
use std::sync::RwLock;

use diesel::prelude::*;
use log::{debug, error, info, warn};
use tantivy::collector::{Count, TopDocs};
use tantivy::directory::MmapDirectory;
Expand All @@ -17,11 +18,9 @@ use tantivy::{
use tantivy_analysis_contrib::commons::EdgeNgramTokenFilter;

use crate::config::SearchConfig;
use crate::db::Database;
use diesel::prelude::*;

use crate::db::models::Crate;
use crate::db::schema::*;
use crate::db::Database;
use crate::error::Error;
use crate::fts::TantivyDocument;

Expand Down Expand Up @@ -376,8 +375,8 @@ impl Tantivy {

if let Some((krates, keywords, categories)) = result {
start = start + krates.len() as i64;
let mut keywords_iterator = keywords.into_iter();
let mut categories_iterator = categories.into_iter();
let mut keywords_iterator = keywords.into_iter().peekable();
let mut categories_iterator = categories.into_iter().peekable();

let mut current_keyword: Option<(i64, String)> = keywords_iterator.next();
let mut current_category: Option<(i64, String)> = categories_iterator.next();
Expand All @@ -393,19 +392,23 @@ impl Tantivy {
doc.set_description(description.clone());
}

// Add all keywords
// Skip keywords that might be orphan and add keywords that match ids
while current_keyword.is_some()
&& current_keyword.as_ref().unwrap().0 == krate.id
&& current_keyword.as_ref().unwrap().0 <= krate.id
{
doc.add_keyword(current_keyword.unwrap().1);
if current_keyword.as_ref().unwrap().0 == krate.id {
doc.add_keyword(current_keyword.unwrap().1);
}
current_keyword = keywords_iterator.next();
}

// Add all cateogries
// Skip keywords that might be orphan and add keywords that match ids
while current_category.is_some()
&& current_category.as_ref().unwrap().0 == krate.id
&& current_category.as_ref().unwrap().0 <= krate.id
{
doc.add_category(current_category.unwrap().1);
if current_category.as_ref().unwrap().0 == krate.id {
doc.add_keyword(current_category.unwrap().1);
}
current_category = categories_iterator.next();
}

Expand Down

0 comments on commit fdd1c9a

Please sign in to comment.