Skip to content

Commit

Permalink
Use a simple_query_string for search remainders
Browse files Browse the repository at this point in the history
Specifying multiple "any" terms in a search was resulting in only the
last term being applied, since "multi_match" takes a string. Instead,
change to the "simple_query_string" query and join the terms. This
allows multiple terms to be specified and enables the user to use
some advanced, but fairly safe, constructions.
  • Loading branch information
tilgovi committed Aug 18, 2015
1 parent 49dcbef commit ce47515
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
5 changes: 2 additions & 3 deletions h/api/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ def build(request_params, userid=None, search_normalized_uris=False):

if "any" in request_params:
matches.append({
"multi_match": {
"simple_query_string": {
"fields": ["quote", "tags", "text", "uri.parts", "user"],
"query": request_params.getall("any"),
"type": "cross_fields"
"query": ' '.join(request_params.getall("any"))
}
})
del request_params["any"]
Expand Down
14 changes: 6 additions & 8 deletions h/api/search/test/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,14 @@ def test_build_with_combined_user_and_tag_query():
{"user": "bob", "tags": "foo"}))

assert q["query"]["filtered"]["query"] == {
"bool": {"must": [
"bool": {"should": [
{"match": {"user": "bob"}},
{"match": {"tags": "foo"}},
]}}


def test_build_with_keyword():
"""Keywords are returned in the query dict in a "multi_match" clause."""
"""Keywords are returned in a "simple_query_string" clause."""
params = multidict.MultiDict()
params.add("any", "howdy")

Expand All @@ -192,11 +192,10 @@ def test_build_with_keyword():
"bool": {
"should": [
{
"multi_match": {
"simple_query_string": {
"fields": ["quote", "tags", "text", "uri.parts",
"user"],
"query": ["howdy"],
"type": "cross_fields"
"query": "howdy",
}
}
]
Expand All @@ -213,10 +212,9 @@ def test_build_with_multiple_keywords():
q = query.build(request_params=params)

assert q["query"]["filtered"]["query"] == {
"bool": {"should": [{"multi_match": {
"bool": {"should": [{"simple_query_string": {
"fields": ["quote", "tags", "text", "uri.parts", "user"],
"query": ["howdy", "there"],
"type": "cross_fields"
"query": "howdy there",
}}]}
}

Expand Down

0 comments on commit ce47515

Please sign in to comment.