summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve McLellan <steven.j.mclellan@gmail.com>2016-03-01 15:28:30 -0600
committerSteve McLellan <steven.j.mclellan@gmail.com>2016-03-02 11:34:42 -0600
commite8328fd7c23ff2213488233fb3158428d3ec9664 (patch)
tree3d52b630a536a2904630acd25695f408887900f7
parentca9dc18deccd893dcc17bcf9150eb4e43951cc02 (diff)
Escape slashes in query_strings0.2.0
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
Notes
Notes (review): Code-Review+1: Rick Aulino <rick.aulino@hpe.com> Code-Review+2: Lakshmi N Sampath <lakshmi.sampath@hp.com> Code-Review+2: Travis Tripp <travis.tripp@hpe.com> Workflow+1: Travis Tripp <travis.tripp@hpe.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Wed, 02 Mar 2016 20:12:02 +0000 Reviewed-on: https://review.openstack.org/286850 Project: openstack/python-searchlightclient Branch: refs/heads/master
-rw-r--r--searchlightclient/osc/v1/search.py7
-rw-r--r--searchlightclient/tests/unit/osc/v1/test_search.py15
2 files changed, 21 insertions, 1 deletions
diff --git a/searchlightclient/osc/v1/search.py b/searchlightclient/osc/v1/search.py
index e4f1ffe..e33b2ef 100644
--- a/searchlightclient/osc/v1/search.py
+++ b/searchlightclient/osc/v1/search.py
@@ -105,7 +105,9 @@ class SearchResource(lister.Lister):
105 "a JSON object.") 105 "a JSON object.")
106 exit(1) 106 exit(1)
107 except Exception: 107 except Exception:
108 query = {"query_string": {"query": parsed_args.query}} 108 qs = self._modify_query_string(parsed_args.query)
109 query = {"query_string": {"query": qs}}
110
109 params['query'] = query 111 params['query'] = query
110 112
111 data = search_client.search.search(**params) 113 data = search_client.search.search(**params)
@@ -122,3 +124,6 @@ class SearchResource(lister.Lister):
122 converted["updated"] = v.get("updated_at") 124 converted["updated"] = v.get("updated_at")
123 result.append(utils.get_dict_properties(converted, columns)) 125 result.append(utils.get_dict_properties(converted, columns))
124 return (columns, result) 126 return (columns, result)
127
128 def _modify_query_string(self, query_string):
129 return query_string.replace('/', '\/')
diff --git a/searchlightclient/tests/unit/osc/v1/test_search.py b/searchlightclient/tests/unit/osc/v1/test_search.py
index 3931bcf..4e03510 100644
--- a/searchlightclient/tests/unit/osc/v1/test_search.py
+++ b/searchlightclient/tests/unit/osc/v1/test_search.py
@@ -85,6 +85,21 @@ class TestSearchResource(TestSearch):
85 _source=['id', 'name', 'updated_at'], 85 _source=['id', 'name', 'updated_at'],
86 all_projects=False, type=None) 86 all_projects=False, type=None)
87 87
88 def test_search_regexp_slashes_in_query_string(self):
89 """Escape slashes in querystrings so not to be treated as regexp"""
90 self._test_search(["this/has/some/slashes"],
91 query={"query_string": {"query": "this\/has\/some\/slashes"}},
92 _source=['id', 'name', 'updated_at'],
93 all_projects=False, type=None)
94
95 def test_search_regexp_slashes_in_query(self):
96 """Don't escape slashes in DSL queries"""
97 self._test_search(['--json',
98 '{"term": {"name": "this/has/some/slashes"}}'],
99 query={"term": {"name": "this/has/some/slashes"}},
100 _source=['id', 'name', 'updated_at'],
101 all_projects=False, type=None)
102
88 def test_search_query_dsl(self): 103 def test_search_query_dsl(self):
89 self._test_search(['--json', 104 self._test_search(['--json',
90 '{"term": {"status": "active"}}'], 105 '{"term": {"status": "active"}}'],