Escape slashes in query_strings

Slashes (/) in query strings are treated by Elasticsearch as delimiting
a regexp expression. This is confusing especially when operating on
swift object paths, and in general we feel that in general the confusion
caused by mismatched slashes isn't offset by the ability to use regexps.
If a user really needs to run a regexp they can use the query dsl; we
could also consider adding a flag later.

Change-Id: I892541820b077d8eb08ae635a843d8dc84a5f55a
Closes-Bug: #1551946
This commit is contained in:
Steve McLellan 2016-03-01 15:28:30 -06:00
parent ca9dc18dec
commit e8328fd7c2
2 changed files with 21 additions and 1 deletions

View File

@ -105,7 +105,9 @@ class SearchResource(lister.Lister):
"a JSON object.")
exit(1)
except Exception:
query = {"query_string": {"query": parsed_args.query}}
qs = self._modify_query_string(parsed_args.query)
query = {"query_string": {"query": qs}}
params['query'] = query
data = search_client.search.search(**params)
@ -122,3 +124,6 @@ class SearchResource(lister.Lister):
converted["updated"] = v.get("updated_at")
result.append(utils.get_dict_properties(converted, columns))
return (columns, result)
def _modify_query_string(self, query_string):
return query_string.replace('/', '\/')

View File

@ -85,6 +85,21 @@ class TestSearchResource(TestSearch):
_source=['id', 'name', 'updated_at'],
all_projects=False, type=None)
def test_search_regexp_slashes_in_query_string(self):
"""Escape slashes in querystrings so not to be treated as regexp"""
self._test_search(["this/has/some/slashes"],
query={"query_string": {"query": "this\/has\/some\/slashes"}},
_source=['id', 'name', 'updated_at'],
all_projects=False, type=None)
def test_search_regexp_slashes_in_query(self):
"""Don't escape slashes in DSL queries"""
self._test_search(['--json',
'{"term": {"name": "this/has/some/slashes"}}'],
query={"term": {"name": "this/has/some/slashes"}},
_source=['id', 'name', 'updated_at'],
all_projects=False, type=None)
def test_search_query_dsl(self):
self._test_search(['--json',
'{"term": {"status": "active"}}'],