Commit 70a7e3fc authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

When no query is given, sort issuers by number of securities.

parent dea71bf2
......@@ -14,6 +14,7 @@ fn run_index(data_dir: &Path) -> tantivy::Result<()> {
let id_field = schema.get_field("id").unwrap();
let name_autocomplete_field = schema.get_field("name_autocomplete").unwrap();
let name_field = schema.get_field("name").unwrap();
let securities_count_field = schema.get_field("securities_count").unwrap();
for entry in fs::read_dir(issuers_dir).unwrap() {
let json_file_path = entry.unwrap().path();
......@@ -24,11 +25,12 @@ fn run_index(data_dir: &Path) -> tantivy::Result<()> {
.expect("An error occurred while reading file");
let issuer = json::parse(&json_string).expect("Invalid JSON");
let mut issuer_doc = Document::default();
issuer_doc.add_i64(id_field, issuer["id"].as_i64().unwrap());
issuer_doc.add_u64(id_field, issuer["id"].as_u64().unwrap());
for name in issuer["names"].members() {
issuer_doc.add_text(name_autocomplete_field, &name.to_string());
issuer_doc.add_text(name_field, &name.to_string());
}
issuer_doc.add_u64(securities_count_field, issuer["securities"].len() as u64);
index_writer.add_document(issuer_doc);
}
index_writer.commit()?;
......
......@@ -3,12 +3,9 @@ use issuers;
use std::convert::From;
use std::net::SocketAddr;
use std::path::Path;
use tantivy::collector;
use tantivy::collector::CountCollector;
use tantivy::collector::TopCollector;
use tantivy::query::{AllQuery, QueryParser};
use tantivy::schema::NamedFieldDocument;
use tantivy::schema::Schema;
use tantivy::collector::{self, CountCollector, TopFieldCollector, TopScoreCollector};
use tantivy::query::{AllQuery, Query, QueryParser};
use tantivy::schema::{NamedFieldDocument, Schema};
use tantivy::DocAddress;
use tantivy::Document;
use tantivy::Index;
......@@ -73,24 +70,38 @@ impl_web!{
let q = query_string.q.replace("'", " ");
let q = q.replace("\"", " ");
let q = q.trim();
let query = if q.is_empty() {
Box::new(AllQuery)
} else {
self .list_query_parser
.parse_query(q)
.expect("Parsing the query failed")
};
let searcher = self.index.searcher();
let mut count_collector = CountCollector::default();
let mut top_collector = TopCollector::with_limit(query_string.offset + query_string.limit);
{
let mut chained_collector = collector::chain()
.push(&mut top_collector)
.push(&mut count_collector);
query.search(&searcher, &mut chained_collector).unwrap();
}
let hits: Vec<Hit> = {
let hits: Vec<Hit> = if q.is_empty() {
let mut top_collector = TopFieldCollector::<u64>::with_limit(self.schema.get_field("securities_count").unwrap(), query_string.offset + query_string.limit);
{
let mut chained_collector = collector::chain()
.push(&mut top_collector)
.push(&mut count_collector);
let query = Box::new(AllQuery);
query.search(&searcher, &mut chained_collector).unwrap();
}
top_collector
.docs()
.iter()
.skip(query_string.offset)
.map(|doc_address| {
let doc: Document = searcher.doc(*doc_address).unwrap();
self.create_hit(&doc, doc_address)
})
.collect()
} else {
let mut top_collector = TopScoreCollector::with_limit(query_string.offset + query_string.limit);
{
let mut chained_collector = collector::chain()
.push(&mut top_collector)
.push(&mut count_collector);
let query = self.list_query_parser
.parse_query(q)
.expect("Parsing the query failed");
query.search(&searcher, &mut chained_collector).unwrap();
}
top_collector
.docs()
.iter()
......@@ -114,16 +125,17 @@ impl_web!{
let q = query_string.q.replace("'", " ");
let q = q.replace("\"", " ");
let q = q.trim();
let query = if q.is_empty() {
Box::new(AllQuery)
} else {
self .autocomplete_query_parser
self.autocomplete_query_parser
.parse_query(q)
.expect("Parsing the query failed")
};
let searcher = self.index.searcher();
let mut count_collector = CountCollector::default();
let mut top_collector = TopCollector::with_limit(query_string.offset + query_string.limit);
let mut top_collector = TopScoreCollector::with_limit(query_string.offset + query_string.limit);
{
let mut chained_collector = collector::chain()
.push(&mut top_collector)
......
......@@ -11,7 +11,7 @@ pub fn build_index(data_dir: &Path) -> tantivy::Result<tantivy::Index> {
fs::create_dir_all(&index_dir).expect("Directory creation failed");
let mut schema_builder = SchemaBuilder::default();
schema_builder.add_i64_field("id", INT_STORED);
schema_builder.add_u64_field("id", INT_STORED);
{
let text_field_indexing = TextFieldIndexing::default()
......@@ -33,6 +33,8 @@ pub fn build_index(data_dir: &Path) -> tantivy::Result<tantivy::Index> {
schema_builder.add_text_field("name_autocomplete", text_options);
}
schema_builder.add_u64_field("securities_count", FAST);
let schema = schema_builder.build();
let index = Index::create_in_dir(&index_dir, schema.clone()).unwrap();
register_tokenizers(&index)?;
......
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