Skip to content

Commit 189de3e

Browse files
committed
feat(search): adjusted default details mapping and optimized all field info handling in search results #1627
1 parent adc4be7 commit 189de3e

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

src/timeline/search.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
},
1414
options::profile::Profile,
1515
};
16+
use aho_corasick::AhoCorasickBuilder;
1617
use chrono::{TimeZone, Utc};
1718
use compact_str::CompactString;
1819
use csv::{QuoteStyle, Writer, WriterBuilder};
@@ -122,6 +123,24 @@ impl EventSearch {
122123
})
123124
}
124125

126+
fn get_default_details_mapping_table(
127+
&self,
128+
stored_static: &StoredStatic,
129+
) -> HashMap<CompactString, CompactString> {
130+
let mut default_details_abbr: HashMap<CompactString, CompactString> = HashMap::new();
131+
for detail_values in stored_static.default_details.values() {
132+
detail_values.split(" ¦ ").for_each(|x| {
133+
let abbr_k_v = x.split(": ").collect_vec();
134+
if abbr_k_v.len() == 2 {
135+
let abbr: CompactString = abbr_k_v[0].into();
136+
let full: CompactString = abbr_k_v[1].replace("%", "").trim().into();
137+
default_details_abbr.insert(full, abbr);
138+
}
139+
});
140+
}
141+
default_details_abbr
142+
}
143+
125144
// check if a record contains the keywords specified in a search command option or not.
126145
fn search_keyword(
127146
&mut self,
@@ -146,6 +165,15 @@ impl EventSearch {
146165
Some(Action::Search(opt)) => (opt.ignore_case, opt.and_logic),
147166
_ => (false, false),
148167
};
168+
let default_details_abbr = self.get_default_details_mapping_table(stored_static);
169+
let all_field_info_abbr = AhoCorasickBuilder::new()
170+
.ascii_case_insensitive(true)
171+
.build(default_details_abbr.keys().map(|x| x.as_str()))
172+
.unwrap();
173+
let all_field_info_abbr_value = default_details_abbr
174+
.values()
175+
.map(|x| x.as_str())
176+
.collect_vec();
149177
for record in records.iter() {
150178
// filtering
151179
if !self.filter_record(record, &filter_rule, &stored_static.eventkey_alias) {
@@ -192,11 +220,16 @@ impl EventSearch {
192220
&stored_static.eventkey_alias,
193221
stored_static.output_option.as_ref().unwrap(),
194222
);
195-
let allfieldinfo_newline_splited = ALLFIELDINFO_SPECIAL_CHARS
196-
.replace_all(&allfieldinfo, &["🦅", "🦅", "🦅"])
197-
.split('🦅')
198-
.filter(|x| !x.is_empty())
199-
.join(" ");
223+
let allfieldinfo_newline_splited = all_field_info_abbr.replace_all(
224+
ALLFIELDINFO_SPECIAL_CHARS
225+
.replace_all(&allfieldinfo, &["🦅", "🦅", "🦅"])
226+
.split('🦅')
227+
.filter(|x| !x.is_empty())
228+
.join(" ")
229+
.as_str(),
230+
&all_field_info_abbr_value,
231+
);
232+
200233
if search_option.sort_events {
201234
// we cannot sort all the records unless we get all the records; so we just collect the hit record at this code and we'll sort them later.
202235
self.search_result.insert((
@@ -247,6 +280,16 @@ impl EventSearch {
247280
return;
248281
}
249282

283+
let default_details_abbr = self.get_default_details_mapping_table(stored_static);
284+
let all_field_info_abbr = AhoCorasickBuilder::new()
285+
.ascii_case_insensitive(true)
286+
.build(default_details_abbr.keys().map(|x| x.as_str()))
287+
.unwrap();
288+
let all_field_info_abbr_value = default_details_abbr
289+
.values()
290+
.map(|x| x.as_str())
291+
.collect_vec();
292+
250293
let filter_rule = create_filter_rule(&search_option.filter);
251294
let mut wtr = ResultWriter::new(search_option);
252295
for record in records.iter() {
@@ -268,11 +311,15 @@ impl EventSearch {
268311
&stored_static.eventkey_alias,
269312
stored_static.output_option.as_ref().unwrap(),
270313
);
271-
let allfieldinfo_newline_splited = ALLFIELDINFO_SPECIAL_CHARS
272-
.replace_all(&allfieldinfo, &["🦅", "🦅", "🦅"])
273-
.split('🦅')
274-
.filter(|x| !x.is_empty())
275-
.join(" ");
314+
let allfieldinfo_newline_splited = all_field_info_abbr.replace_all(
315+
ALLFIELDINFO_SPECIAL_CHARS
316+
.replace_all(&allfieldinfo, &["🦅", "🦅", "🦅"])
317+
.split('🦅')
318+
.filter(|x| !x.is_empty())
319+
.join(" ")
320+
.as_str(),
321+
&all_field_info_abbr_value,
322+
);
276323
if search_option.sort_events {
277324
// we cannot sort all the records unless we get all the records; so we just collect the hit record at this code and we'll sort them later.
278325
self.search_result.insert((

0 commit comments

Comments
 (0)