Commit 0db900b9 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Initial commit

parents
Pipeline #43 failed with stages
in 16 seconds
/target
**/*.rs.bk
This diff is collapsed.
[package]
name = "dfih-search"
version = "0.1.0"
authors = ["Emmanuel Raviart <emmanuel@raviart.com>"]
[dependencies]
json = "0.11"
tantivy = "0.7"
// #[macro_use]
extern crate json;
extern crate tantivy;
use std::fs::{self, File};
use std::io::prelude::*;
use std::path::Path;
use tantivy::collector::TopCollector;
use tantivy::query::QueryParser;
use tantivy::schema::*;
use tantivy::Index;
fn main() -> tantivy::Result<()> {
let data_dir = Path::new("../dfih-ui/data");
let index_dir = data_dir.join("index");
let mut schema_builder = SchemaBuilder::default();
schema_builder.add_text_field("name", TEXT | STORED);
let schema = schema_builder.build();
let index = Index::create_in_dir(&index_dir, schema.clone())?;
let mut index_writer = index.writer(50_000_000)?;
let issuers_dir = data_dir.join("issuers");
let name_field = schema.get_field("name").unwrap();
for entry in fs::read_dir(issuers_dir).unwrap() {
let json_file_path = entry.unwrap().path();
let mut json_file = File::open(json_file_path).expect("JSON file not found");
let mut json_string = String::new();
json_file
.read_to_string(&mut json_string)
.expect("Something went wrong reading the file");
let issuer = json::parse(&json_string).expect("Invalid JSON");
let mut issuer_doc = Document::default();
for name in issuer["names"].members() {
println!("{}", name);
issuer_doc.add_text(name_field, &name.to_string());
}
index_writer.add_document(issuer_doc);
}
index_writer.commit()?;
index.load_searchers()?;
let searcher = index.searcher();
let query_parser = QueryParser::for_index(&index, vec![name_field]);
let query = query_parser.parse_query("forges")?;
let mut top_collector = TopCollector::with_limit(10);
searcher.search(&*query, &mut top_collector)?;
let doc_addresses = top_collector.docs();
for doc_address in doc_addresses {
let retrieved_doc = searcher.doc(doc_address)?;
println!("{}", schema.to_json(&retrieved_doc));
}
Ok(())
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment