Make searching for multiple words be treated as the boolean AND of those words

(imported from commit d9e47dd25553cc31eeda615e3a5709436e883ab3)
This commit is contained in:
Zev Benjamin 2013-01-31 17:05:47 -05:00
parent 6d5424c910
commit d5fdfd7be2
2 changed files with 14 additions and 7 deletions

View File

@ -152,10 +152,14 @@ function build_filter(operators_mixed_case) {
break;
case 'search':
if (message.content.toLowerCase().indexOf(operand) === -1) {
if ((message.type !== 'stream') ||
(message.subject.toLowerCase().indexOf(operand) === -1)) {
return false;
var words = operand.trim().split(/\s+/);
var j;
for (j = 0; j < words.length; ++j) {
if (message.content.toLowerCase().indexOf(words[j]) === -1) {
if ((message.type !== 'stream') ||
(message.subject.toLowerCase().indexOf(words[j]) === -1)) {
return false;
}
}
}
break;

View File

@ -559,12 +559,15 @@ class NarrowBuilder(object):
(Q(sender=self.user_profile) & Q(recipient=narrow_recipient)))
def do_search(self, query, operand):
words = operand.split()
if "postgres" in settings.DATABASES["default"]["ENGINE"]:
sql = "to_tsvector('english', subject || ' ' || content) @@ to_tsquery('english', %s)"
return query.extra(where=[sql], params=[operand])
return query.extra(where=[sql], params=[" & ".join(words)])
else:
return query.filter(Q(content__icontains=operand) |
Q(subject__icontains=operand))
for word in words:
query = query.filter(Q(content__icontains=word) |
Q(subject__icontains=word))
return query
def narrow_parameter(json):