Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle special characters in queries and filter chips #3168

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions timesketch/frontend-ng/src/components/Explore/EventDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ limitations under the License.
</v-btn>

<!-- Include field:value as filter chip -->
<v-btn @click.stop="applyFilterChip(key, value, 'must')" icon x-small class="mr-1">
<v-btn
v-if="!ignoreFilterChips.has(key)"
@click.stop="applyFilterChip(key, value, 'must')"
icon
x-small
class="mr-1">
<v-icon title="Filter for value">mdi-filter-plus-outline</v-icon>
</v-btn>

<!-- Exclude field:value as filter chip -->
<v-btn
v-if="!ignoreFilterChips.has(key)"
@click.stop="applyFilterChip(key, value, 'must_not')"
icon
x-small
Expand Down Expand Up @@ -197,9 +203,12 @@ export default {
'path_spec',
'strings',
'timestamp',
'timestamp_desc',
'xml_string',
]),
ignoreFilterChips: new Set([
'datetime',
'tag',
]),
eventKey: '',
eventValue: '',
eventTimestamp: 0,
Expand Down
36 changes: 31 additions & 5 deletions timesketch/lib/datastores/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,28 @@ def build_query(

query_dsl = {"query": {"bool": {"must": [], "must_not": [], "filter": []}}}

special_char_query = None
if query_string:
query_parts = query_string.split(":", 1)
if len(query_parts) == 2:
field_name, query_value = query_parts

# Special Character Check
if set(query_value) <= set('.+-=_&|><!(){}[]^"~?:\\/'):
# Construct the term query directly using the .keyword
special_char_query = {
"term": {f"{field_name}.keyword": query_value}
}
query_string = ""

if query_string:
query_dsl["query"]["bool"]["must"].append(
{"query_string": {"query": query_string, "default_operator": "AND"}}
)

if special_char_query:
query_dsl["query"]["bool"]["must"].append(special_char_query)

# New UI filters
if query_filter.get("chips", None):
labels = []
Expand All @@ -374,13 +391,22 @@ def build_query(
labels.append(chip["value"])

elif chip["type"] == "term":
term_filter = {
"match_phrase": {
"{}".format(chip["field"]): {
"query": "{}".format(chip["value"])
if isinstance(chip["value"], str):
term_filter = {
"match_phrase": {
"{}.keyword".format(chip["field"]): {
"query": "{}".format(chip["value"])
}
}
}
else:
term_filter = {
"match_phrase": {
"{}".format(chip["field"]): {
"query": "{}".format(chip["value"])
}
}
}
}

if chip["operator"] == "must":
must_filters.append(term_filter)
Expand Down
Loading