|
| 1 | +use tantivy::{ |
| 2 | + collector::{Count, TopDocs}, |
| 3 | + schema::{Schema, INDEXED, STORED, TEXT}, |
| 4 | +}; |
| 5 | + |
| 6 | +use crate::{poi::AirmailPoi, query::AirmailQuery}; |
| 7 | + |
| 8 | +// Field name keys. |
| 9 | +pub const FIELD_NAME: &str = "name"; |
| 10 | +pub const FIELD_CATEGORY: &str = "category"; |
| 11 | +pub const FIELD_HOUSE_NUMBER: &str = "house_number"; |
| 12 | +pub const FIELD_ROAD: &str = "road"; |
| 13 | +pub const FIELD_UNIT: &str = "unit"; |
| 14 | +pub const FIELD_LOCALITY: &str = "locality"; |
| 15 | +pub const FIELD_REGION: &str = "region"; |
| 16 | +pub const FIELD_S2CELL: &str = "s2cell"; |
| 17 | + |
| 18 | +pub struct AirmailIndex { |
| 19 | + tantivy_index: tantivy::Index, |
| 20 | +} |
| 21 | + |
| 22 | +impl AirmailIndex { |
| 23 | + fn schema() -> tantivy::schema::Schema { |
| 24 | + let mut schema_builder = Schema::builder(); |
| 25 | + let _ = schema_builder.add_text_field(FIELD_NAME, TEXT | STORED); |
| 26 | + let _ = schema_builder.add_text_field(FIELD_CATEGORY, TEXT | STORED); |
| 27 | + let _ = schema_builder.add_text_field(FIELD_HOUSE_NUMBER, TEXT | STORED); |
| 28 | + let _ = schema_builder.add_text_field(FIELD_ROAD, TEXT | STORED); |
| 29 | + let _ = schema_builder.add_text_field(FIELD_UNIT, TEXT | STORED); |
| 30 | + let _ = schema_builder.add_text_field(FIELD_LOCALITY, TEXT | STORED); |
| 31 | + let _ = schema_builder.add_text_field(FIELD_REGION, TEXT | STORED); |
| 32 | + let _ = schema_builder.add_u64_field(FIELD_S2CELL, INDEXED | STORED); |
| 33 | + schema_builder.build() |
| 34 | + } |
| 35 | + |
| 36 | + pub fn field_name(&self) -> tantivy::schema::Field { |
| 37 | + self.tantivy_index.schema().get_field(FIELD_NAME).unwrap() |
| 38 | + } |
| 39 | + |
| 40 | + pub fn field_house_number(&self) -> tantivy::schema::Field { |
| 41 | + self.tantivy_index |
| 42 | + .schema() |
| 43 | + .get_field(FIELD_HOUSE_NUMBER) |
| 44 | + .unwrap() |
| 45 | + } |
| 46 | + |
| 47 | + pub fn field_road(&self) -> tantivy::schema::Field { |
| 48 | + self.tantivy_index.schema().get_field(FIELD_ROAD).unwrap() |
| 49 | + } |
| 50 | + |
| 51 | + pub fn field_unit(&self) -> tantivy::schema::Field { |
| 52 | + self.tantivy_index.schema().get_field(FIELD_UNIT).unwrap() |
| 53 | + } |
| 54 | + |
| 55 | + pub fn field_locality(&self) -> tantivy::schema::Field { |
| 56 | + self.tantivy_index |
| 57 | + .schema() |
| 58 | + .get_field(FIELD_LOCALITY) |
| 59 | + .unwrap() |
| 60 | + } |
| 61 | + |
| 62 | + pub fn field_region(&self) -> tantivy::schema::Field { |
| 63 | + self.tantivy_index.schema().get_field(FIELD_REGION).unwrap() |
| 64 | + } |
| 65 | + |
| 66 | + pub fn create(index_dir: &str) -> Result<Self, Box<dyn std::error::Error>> { |
| 67 | + let schema = Self::schema(); |
| 68 | + let tantivy_index = tantivy::Index::create_in_dir(index_dir, schema)?; |
| 69 | + Ok(Self { tantivy_index }) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn new(index_dir: &str) -> Result<Self, Box<dyn std::error::Error>> { |
| 73 | + let tantivy_index = tantivy::Index::open_in_dir(index_dir)?; |
| 74 | + Ok(Self { tantivy_index }) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn writer(&mut self) -> Result<AirmailIndexWriter, Box<dyn std::error::Error>> { |
| 78 | + let tantivy_writer = self.tantivy_index.writer(50_000_000)?; |
| 79 | + let writer = AirmailIndexWriter { tantivy_writer }; |
| 80 | + Ok(writer) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn search(&self, query: AirmailQuery) -> Result<Vec<String>, Box<dyn std::error::Error>> { |
| 84 | + let tantivy_reader = self.tantivy_index.reader()?; |
| 85 | + let searcher = tantivy_reader.searcher(); |
| 86 | + let results = searcher.search(&query, &(TopDocs::with_limit(5), Count))?; |
| 87 | + let strings = results |
| 88 | + .0 |
| 89 | + .iter() |
| 90 | + .map(|s| format!("{:?}", searcher.doc(s.1).unwrap())) |
| 91 | + .collect(); |
| 92 | + Ok(strings) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub struct AirmailIndexWriter { |
| 97 | + tantivy_writer: tantivy::IndexWriter, |
| 98 | +} |
| 99 | + |
| 100 | +impl AirmailIndexWriter { |
| 101 | + pub fn add_poi(&mut self, poi: AirmailPoi) -> Result<(), Box<dyn std::error::Error>> { |
| 102 | + let mut document = tantivy::Document::new(); |
| 103 | + let schema = self.tantivy_writer.index().schema(); |
| 104 | + if let Some(name) = poi.name { |
| 105 | + document.add_text(schema.get_field(FIELD_NAME)?, name); |
| 106 | + } |
| 107 | + if let Some(category) = poi.category { |
| 108 | + document.add_text(schema.get_field(FIELD_CATEGORY)?, category); |
| 109 | + } |
| 110 | + document.add_u64(schema.get_field(FIELD_S2CELL)?, poi.s2cell); |
| 111 | + self.tantivy_writer.add_document(document)?; |
| 112 | + Ok(()) |
| 113 | + } |
| 114 | + |
| 115 | + pub fn commit(mut self) -> Result<(), Box<dyn std::error::Error>> { |
| 116 | + self.tantivy_writer.commit()?; |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
0 commit comments