Skip to content

Commit

Permalink
Remove the 'search_normalized' feature flag
Browse files Browse the repository at this point in the history
This is now deployed and toggled on everywhere. Removing it simplifies
the relevant query code and reduces the number of possible configuration
states of our application.
  • Loading branch information
nickstenning committed Oct 9, 2015
1 parent 81ca666 commit c403f2d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 69 deletions.
2 changes: 1 addition & 1 deletion h/api/search/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def search(request, params):
builder = query.Builder()

builder.append_filter(query.AuthFilter(request))
builder.append_filter(query.UriFilter(request))
builder.append_filter(query.UriFilter())
builder.append_filter(lambda _: \
nipsa.nipsa_filter(request.authenticated_userid))
builder.append_filter(query.GroupFilter(request))
Expand Down
16 changes: 0 additions & 16 deletions h/api/search/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,14 @@ class UriFilter(object):
A filter that selects only annotations where the 'uri' parameter matches.
"""

def __init__(self, request):
self.request = request

def __call__(self, params):
uristr = params.pop('uri', None)
if uristr is None:
return None

if self.request.feature('search_normalized'):
return self.term_filter(uristr)
return self.match_filter(uristr)

def term_filter(self, uristr):
scopes = [uri.normalize(u) for u in uri.expand(uristr)]
return {"terms": {"target.scope": scopes}}

def match_filter(self, uristr):
uristrs = uri.expand(uristr)
clauses = [{"match": {"uri": u}} for u in uristrs]

if len(clauses) == 1:
return {"query": clauses[0]}
return {"query": {"bool": {"should": clauses}}}


class AnyMatcher(object):

Expand Down
54 changes: 3 additions & 51 deletions h/api/search/test/query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,75 +316,27 @@ def test_urifilter_inactive_when_no_uri_param():
"""
When there's no `uri` parameter, return None.
"""
request = mock.Mock()
urifilter = query.UriFilter(request)
urifilter = query.UriFilter()

assert urifilter({"foo": "bar"}) is None


@pytest.mark.usefixtures('uri')
def test_urifilter_when_search_normalized_is_off():
request = mock.Mock()
request.feature.return_value = False
urifilter = query.UriFilter(request)

r1 = urifilter({"uri": "http://example.com/"})
r2 = urifilter({"uri": "http://whitehouse.gov/"})

assert r1 == {"query": {"match": {"uri": "http://example.com/"}}}
assert r2 == {"query": {"match": {"uri": "http://whitehouse.gov/"}}}


def test_urifilter_expands_to_all_uris_when_search_normalized_is_off(uri):
"""
It should expand the search to all URIs.
If h.api.uri.expand returns multiple documents for the URI then return a
matcher that finds annotations that match one or more of these documents'
URIs.
"""
request = mock.Mock()
request.feature.return_value = False
results = ["http://example.com/",
"http://example2.com/",
"http://example3.com/"]
uri.expand.side_effect = lambda x: results
urifilter = query.UriFilter(request)

result = urifilter({"uri": "http://example.com"})

assert result == {
"query": {
"bool": {
"should": [
{"match": {"uri": "http://example.com/"}},
{"match": {"uri": "http://example2.com/"}},
{"match": {"uri": "http://example3.com/"}}
],
},
},
}


def test_urifilter_when_search_normalized_is_on(uri):
def test_urifilter_expands_and_normalizes_into_terms_filter(uri):
"""
Uses a `terms` filter against target.scope to filter for URI.
When querying for a URI with the search_normalized feature flag enabled,
UriFilter should use a `terms` filter against the normalized version of the
target source field, which we store in `target.scope`.
It should expand the input URI before searching, and normalize the results
of the expansion.
"""
request = mock.Mock()
request.feature.return_value = True
uri.expand.side_effect = lambda x: [
"http://giraffes.com/",
"https://elephants.com/",
]
uri.normalize.side_effect = lambda x: x[:-1] # Strip the trailing slash
urifilter = query.UriFilter(request)
urifilter = query.UriFilter()

result = urifilter({"uri": "http://example.com/"})

Expand Down
1 change: 0 additions & 1 deletion h/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
'notification': "Send email notifications?",
'queue': "Enable dispatch of annotation events to NSQ?",
'streamer': "Enable 'live streaming' for annotations via the websocket?",
'search_normalized': "Assume all data has normalized URI fields?",
'show_unanchored_annotations': "Show annotations that fail to anchor?",
}

Expand Down

0 comments on commit c403f2d

Please sign in to comment.