Skip to content

Commit

Permalink
Improved multi-engine ranking & fixed hot reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
frc4533-lincoln committed Oct 31, 2024
1 parent d3be0f2 commit 7cbd2ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/lua_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl PluginEngine {
ProvidersConfig::load("plugins/providers.toml")
};
#[cfg(not(feature = "hot_reload"))]
let providers = self.providers;
let providers = &self.providers;

if let Some(provider) = providers.0.get(&query.provider) {
let engine = provider.engine.clone().unwrap_or(query.provider.clone());
Expand Down
30 changes: 19 additions & 11 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,39 @@ pub struct SearchResults {

pub async fn ranked_results(
engine: PluginEngine,
provider_cfg: ProvidersConfig,
ProvidersConfig(provider_cfg): ProvidersConfig,
query: searched::Query,
) -> Vec<searched::Result> {
let mut set = JoinSet::new();
for provider in provider_cfg.0.keys() {
for provider in provider_cfg.keys().cloned() {
// Clone the query so we can switch the provider
// and safely pass between threads
let mut query = query.clone();
query.provider = provider.clone();
query.provider = provider;

let engine = engine.clone();

set.spawn(async move { engine.search(query).await });
}

let mut results = set.join_all().await.concat();

let ranking_tm = Instant::now();

results.sort_by_key(|x| x.url.clone());

let mut dedup = results.clone();
dedup.dedup_by_key(|x| x.url.clone());
let scores = dedup
let scores = results
.iter()
.map(|x| results.iter().filter(|y| y.url == x.url).count())
.collect::<Vec<usize>>();
.map(|x| results.iter().filter(|y| y.url == x.url).count());

let mut scored = results.iter().zip(scores).collect::<Vec<_>>();
scored.dedup_by_key(|x| x.0.url.clone());
scored.sort_by(|a, b| b.1.cmp(&a.1));

let mut dedup_scores = dedup.iter().zip(scores.iter()).collect::<Vec<_>>();
dedup_scores.sort_by(|a, b| b.1.cmp(a.1));
let ret = scored.iter().map(|x| x.0.clone()).collect();
debug!("ranking: {:?}", ranking_tm.elapsed());

dedup_scores.iter().map(|x| x.0.clone()).collect()
ret
}

#[axum_macros::debug_handler]
Expand Down

0 comments on commit 7cbd2ed

Please sign in to comment.