From d5fdfd7be2fe4f54ec718cc282e60cca15b5db29 Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Thu, 31 Jan 2013 17:05:47 -0500 Subject: [PATCH] Make searching for multiple words be treated as the boolean AND of those words (imported from commit d9e47dd25553cc31eeda615e3a5709436e883ab3) --- zephyr/static/js/narrow.js | 12 ++++++++---- zephyr/views.py | 9 ++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/zephyr/static/js/narrow.js b/zephyr/static/js/narrow.js index 44ba66f314..4961973a65 100644 --- a/zephyr/static/js/narrow.js +++ b/zephyr/static/js/narrow.js @@ -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; diff --git a/zephyr/views.py b/zephyr/views.py index 32b5e841da..f0559bd57f 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -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):