Skip to content

Commit 9a55a7b

Browse files
committed
Fix SearchQuerySet to use all given words in db search, adding test.
1 parent b85d57a commit 9a55a7b

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

haystack/backends/simple_backend.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# encoding: utf-8
12
"""
23
A very basic, ORM-based backend for simple search during tests.
34
"""
@@ -35,7 +36,7 @@ def search(self, query_string, **kwargs):
3536
results = []
3637
result_class = SearchResult
3738
models = (
38-
connections[self.connection_alias].get_unified_index().get_indexed_models()
39+
connections[self.connection_alias].get_unified_index().get_indexed_models(),
3940
)
4041

4142
if kwargs.get("result_class"):
@@ -46,9 +47,8 @@ def search(self, query_string, **kwargs):
4647

4748
if query_string:
4849
for model in models:
49-
if query_string == "*":
50-
qs = model.objects.all()
51-
else:
50+
qs = model.objects.all()
51+
if query_string != "*":
5252
for term in query_string.split():
5353
queries = []
5454

@@ -66,19 +66,21 @@ def search(self, query_string, **kwargs):
6666
queries.append(Q(**{"%s__icontains" % field.name: term}))
6767

6868
if queries:
69-
qs = model.objects.filter(
70-
reduce(lambda x, y: x | y, queries)
71-
)
69+
qs = qs.filter(reduce(lambda x, y: x | y, queries))
7270
else:
73-
qs = []
71+
qs = qs.none()
7472

7573
hits += len(qs)
7674

7775
for match in qs:
7876
match.__dict__.pop("score", None)
7977
app_label, model_name = get_model_ct_tuple(match)
8078
result = result_class(
81-
app_label, model_name, match.pk, 0, **match.__dict__
79+
app_label,
80+
model_name,
81+
match.pk,
82+
0,
83+
**match.__dict__,
8284
)
8385
# For efficiency.
8486
result._model = match.__class__

test_haystack/core/fixtures/bulk_data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@
231231
"model": "core.mockmodel",
232232
"fields": {
233233
"author": "daniel3",
234-
"foo": "Each SearchIndex gets a prepare method, which handles collecting all the data. This method should return a dictionary that will be the final data used by the search backend. Overriding this method is useful if you need to collect more than one piece of data or need to incorporate additional data that is not well represented by a single SearchField. An example might look like:",
234+
"foo": "Uniqueness. Each SearchIndex gets a prepare method, which handles collecting all the data. This method should return a dictionary that will be the final data used by the search backend. Overriding this method is useful if you need to collect more than one piece of data or need to incorporate additional data that is not well represented by a single SearchField. An example might look like:",
235235
"pub_date": "2009-07-17 20:30:00",
236236
"tag": 1
237237
}

test_haystack/simple_tests/test_simple_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ def test_search(self):
111111
self.backend.search("should be a string")["results"][0].score, 0
112112
)
113113

114-
self.assertEqual(self.backend.search("index document")["hits"], 6)
114+
self.assertEqual(self.backend.search("index document")["hits"], 5)
115115
self.assertEqual(
116116
[result.pk for result in self.backend.search("index document")["results"]],
117-
[2, 3, 15, 16, 17, 18],
117+
[2, 3, 15, 17, 18],
118118
)
119119

120120
# Regression-ville
@@ -123,7 +123,7 @@ def test_search(self):
123123
result.object.id
124124
for result in self.backend.search("index document")["results"]
125125
],
126-
[2, 3, 15, 16, 17, 18],
126+
[2, 3, 15, 17, 18],
127127
)
128128
self.assertEqual(
129129
self.backend.search("index document")["results"][0].model, MockModel
@@ -193,6 +193,10 @@ def test_search(self):
193193
{"hits": 0, "results": []},
194194
)
195195

196+
# Search by string containing more than one word.
197+
self.assertEqual(self.backend.search("uniqueness index")["hits"], 1)
198+
self.assertEqual(self.backend.search("uniqueness index")["results"][0].pk, 23)
199+
196200
def test_filter_models(self):
197201
self.backend.update(self.index, self.sample_objs)
198202
self.assertEqual(self.backend.search("*", models=set([]))["hits"], 24)

0 commit comments

Comments
 (0)