Skip to content

Commit

Permalink
Add ip fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tontinton committed Jun 1, 2024
1 parent 3c46ec4 commit 67a3e57
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/commands/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ pub async fn run_index(args: IndexArgs, pool: PgPool) -> Result<()> {
Box::new(move |value| options.formats.try_parse(value)),
));
}
FieldType::Ip(options) => {
let field = schema_builder.add_ip_addr_field(&name, options);
field_parsers.push((name, field, Box::new(common_parse)));
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/config/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use serde::{Deserialize, Serialize};
use tantivy::schema::IpAddrOptions;

use super::default_true;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IpFieldConfig {
#[serde(default = "default_true")]
pub stored: bool,

#[serde(default)]
pub fast: bool,

#[serde(default = "default_true")]
pub indexed: bool,
}

impl From<IpFieldConfig> for IpAddrOptions {
fn from(config: IpFieldConfig) -> Self {
let mut options = IpAddrOptions::default();
if config.stored {
options = options.set_stored();
}
if config.indexed {
options = options.set_indexed();
}
if config.fast {
options = options.set_fast();
}
options
}
}
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod boolean;
pub mod datetime;
pub mod ip;
pub mod number;
pub mod text;

Expand All @@ -12,6 +13,7 @@ use tokio::fs::read_to_string;
use self::{
boolean::BooleanFieldConfig,
datetime::DateTimeFieldConfig,
ip::IpFieldConfig,
number::NumberFieldConfig,
text::{IndexedTextFieldType, TextFieldConfig},
};
Expand All @@ -33,6 +35,7 @@ pub enum FieldType {
Number(NumberFieldConfig),
Boolean(BooleanFieldConfig),
Datetime(DateTimeFieldConfig),
Ip(IpFieldConfig),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -49,6 +52,7 @@ impl FieldsConfig {
Number(config) => config.indexed,
Boolean(config) => config.indexed,
Datetime(config) => config.indexed,
Ip(config) => config.indexed,
}
}
}
Expand Down

0 comments on commit 67a3e57

Please sign in to comment.