text_search_core/
field_info.rs1
2use tantivy::schema::{NumericOptions, SchemaBuilder, TextOptions, INDEXED as TavtivyINDEXED, STRING, TEXT};
3
4use crate::{index_type::IndexType, FieldType};
5
6pub struct FieldInfo {
7 pub is_id: bool,
8 pub field_type: FieldType,
9 pub field_name: String,
10 pub index_type: IndexType,
11 pub stored: bool,
12}
13
14impl FieldInfo {
15 pub fn new(field_name: String, field_type: FieldType, index_type: Option<IndexType>, stored: bool) -> Self {
16 Self {
17 is_id: false,
18 field_type,
19 field_name,
20 index_type: index_type.unwrap_or(IndexType::not_indexed),
21 stored,
22 }
23 }
24
25 pub fn new_id_field(field_name: String, field_type: FieldType) -> Self {
26 Self {
27 is_id: true,
28 field_type,
29 field_name,
30 index_type: IndexType::indexed,
31 stored: true
32 }
33 }
34
35 pub fn add_to_schema(&self, schema_builder: &mut SchemaBuilder) {
36 match self.field_type {
37 FieldType::String => {
38 let mut text_options:TextOptions = match self.index_type {
39 IndexType::indexed_string => STRING,
40 IndexType::indexed_text => TEXT,
41 IndexType::indexed => STRING,
42 IndexType::not_indexed => Default::default(),
43 };
44
45 if self.stored {
46 text_options = text_options.set_stored();
47 }
48
49 schema_builder.add_text_field(&self.field_name, text_options);
50 },
51 FieldType::I32 => {
52 let mut numeric_options: NumericOptions = match self.index_type {
53 IndexType::indexed_string => panic!("`indexed_string` is not supported on numeric fields."),
54 IndexType::indexed_text => panic!("`indexed_text` is not supported on numeric fields."),
55 IndexType::indexed => TavtivyINDEXED.into(),
56 IndexType::not_indexed => Default::default(),
57 };
58
59 if self.stored {
60 numeric_options = numeric_options.set_stored();
61 }
62
63 schema_builder.add_i64_field(&self.field_name, numeric_options);
64 },
65 FieldType::Unhandled => panic!("Unhandled field type")
66 }
67 }
68}
69