Skip to content

Commit 72be8f9

Browse files
committed
Implement exact match search and rewrite for operator ordering
1 parent d3c64c1 commit 72be8f9

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

readthedocs/search/faceted_search.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from elasticsearch_dsl import FacetedSearch, TermsFacet
2+
from elasticsearch_dsl.query import SimpleQueryString, Bool
23

34

45
class RTDFacetedSearch(FacetedSearch):
@@ -29,3 +30,24 @@ class FileSearch(RTDFacetedSearch):
2930
'project': TermsFacet(field='project'),
3031
'version': TermsFacet(field='version')
3132
}
33+
34+
def query(self, search, query):
35+
"""
36+
Add query part to ``search``.
37+
"""
38+
39+
if query:
40+
all_queries = []
41+
42+
# Need to search for both 'AND' and 'OR' operations
43+
# The score of AND should be higher as it comes first
44+
for operator in ['AND', 'OR']:
45+
query_string = SimpleQueryString(query=query, fields=self.fields,
46+
default_operator=operator)
47+
all_queries.append(query_string)
48+
49+
# Run bool query with should, so it returns result where either of the query matches
50+
bool_query = Bool(should=all_queries)
51+
search = search.query(bool_query)
52+
53+
return search

readthedocs/search/tests/test_views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ def test_file_search_case_insensitive(self, client, project, case):
101101
# Check the actual text is in the result, not the cased one
102102
assert query_text in result.text()
103103

104+
def test_file_search_exact_match(self, client, project):
105+
"""Check quoted query match exact phrase
106+
107+
Making a query with quoted text like *"foo bar"* should match
108+
exactly *foo bar* phrase.
109+
"""
110+
111+
# `Github` word is present both in `kuma` and `pipeline` files
112+
# But the phrase Github can is available only in kuma docs.
113+
# So search with this phrase to check
114+
query = r'"GitHub can"'
115+
116+
result, _ = self._get_search_result(url=self.url, client=client,
117+
search_params={'q': query, 'type': 'file'})
118+
119+
assert len(result) == 1
120+
104121
def test_page_search_not_return_removed_page(self, client, project):
105122
"""Check removed page are not in the search index"""
106123
query = get_search_query_from_project_file(project_slug=project.slug)

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ setenv =
1515
DJANGO_SETTINGS_MODULE=readthedocs.settings.test
1616
LANG=C
1717
LC_CTYPE=C.UTF-8
18-
DJANGO_SETTINGS_SKIP_LOCAL=True
1918
deps = -r{toxinidir}/requirements/testing.txt
2019
changedir = {toxinidir}/readthedocs
2120
commands =

0 commit comments

Comments
 (0)