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