Skip to content

Commit

Permalink
feat(seach-index): build search index
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Sep 12, 2024
1 parent 56127ad commit 8323644
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/rari-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod percent;
pub mod reader;
pub mod redirects;
pub mod resolve;
pub mod search_index;
pub mod sidebars;
pub mod specs;
pub mod templ;
Expand Down
63 changes: 63 additions & 0 deletions crates/rari-doc/src/search_index.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufWriter;

use rari_types::globals::{build_out_root, content_root};
use rari_types::locale::Locale;
use rari_types::Popularities;
use rari_utils::io::read_to_string;
use serde::Serialize;

use crate::error::DocError;
use crate::pages::page::{Page, PageLike};

#[derive(Debug, Serialize)]
pub struct SearchItem<'a> {
title: &'a str,
url: &'a str,
}

pub fn build_search_index(docs: &[Page]) -> Result<(), DocError> {
let in_file = content_root().join("en-US").join("popularities.json");
let json_str = read_to_string(in_file)?;
let popularities: Popularities = serde_json::from_str(&json_str)?;

let mut all_indices: HashMap<Locale, Vec<(&Page, f64)>> = HashMap::new();

for doc in docs {
let entry = all_indices.entry(doc.locale()).or_default();
entry.push((
doc,
popularities
.popularities
.get(doc.url())
.cloned()
.unwrap_or_default(),
));
}

for (locale, mut index) in all_indices.into_iter() {
if !index.is_empty() {
index.sort_by(|(da, a), (db, b)| match b.partial_cmp(a) {
None | Some(Ordering::Equal) => da.title().cmp(db.title()),
Some(ord) => ord,
});
let out = index
.into_iter()
.map(|(doc, _)| SearchItem {
title: doc.title(),
url: doc.url(),
})
.collect::<Vec<_>>();
let out_file = build_out_root()?
.join(locale.as_folder_str())
.join("search-index.json");
let file = File::create(out_file)?;
let buffed = BufWriter::new(file);

serde_json::to_writer(buffed, &out)?;
}
}
Ok(())
}
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rari_doc::build::{
use rari_doc::cached_readers::{read_and_cache_doc_pages, CACHED_DOC_PAGE_FILES};
use rari_doc::pages::types::doc::Doc;
use rari_doc::reader::read_docs_parallel;
use rari_doc::search_index::build_search_index;
use rari_doc::utils::TEMPL_RECORDER_SENDER;
use rari_tools::history::gather_history;
use rari_tools::popularities::update_popularities;
Expand Down Expand Up @@ -70,6 +71,8 @@ struct BuildArgs {
#[arg(long)]
skip_contributors: bool,
#[arg(long)]
skip_search_index: bool,
#[arg(long)]
skip_blog: bool,
#[arg(long)]
skip_curriculum: bool,
Expand Down Expand Up @@ -189,6 +192,11 @@ fn main() -> Result<(), anyhow::Error> {
urls.extend(build_docs(&docs)?);
println!("Took: {: >10.3?} to build content", start.elapsed());
}
if !args.skip_search_index && args.files.is_empty() {
let start = std::time::Instant::now();
build_search_index(&docs)?;
println!("Took: {: >10.3?} to build search index", start.elapsed());
}
if !args.skip_curriculum && args.files.is_empty() {
let start = std::time::Instant::now();
urls.extend(build_curriculum_pages()?);
Expand Down

0 comments on commit 8323644

Please sign in to comment.