From d20b6b4e5cd2ed8fbaa5466eb9f8014b400af388 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Wed, 26 Mar 2014 16:11:32 +0100 Subject: [PATCH] rewrite live search results to still be pertinent (#4570) --- .../dms/thesaurus/browser/thesaurusview.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/collective/dms/thesaurus/browser/thesaurusview.py b/src/collective/dms/thesaurus/browser/thesaurusview.py index 9f8412d..1ddd386 100644 --- a/src/collective/dms/thesaurus/browser/thesaurusview.py +++ b/src/collective/dms/thesaurus/browser/thesaurusview.py @@ -128,42 +128,42 @@ class ListKeywordsView(BrowserView): query_string = unicode(self.request.form.get('q'), 'utf-8') query_terms = [normalizer.normalize(x) for x in query_string.split()] + absolute_startswith = [] startswith = [] intermediate = [] other = [] q = query_string.lower() regex = re.compile(r"[\s'()]") + q_words = regex.split(q) items = self.getItems(query_string) done = {} for normalized, title, id in items: + count_start = 0 + count_in = 0 for term in query_terms: - if not term in normalized.lower(): - break - else: - done[id] = True - item = '%s|%s' % (title, id) - if normalized.lower().startswith(q): - startswith.append((normalized, item)) - continue - for word in regex.split(normalized): - if word.lower().startswith(q): - intermediate.append((normalized, item)) - break - else: - other.append((normalized, item)) + for word in regex.split(normalized): + if word.lower().startswith(term): + count_start += 1 + elif term in word.lower(): + count_in += 1 - for normalized, title, id in items: - if done.get(id): - continue item = '%s|%s' % (title, id) - other.append((normalized, item)) + if count_start == len(query_terms): + absolute_startswith.append((normalized, item)) + elif count_start >= 1: + startswith.append((normalized, item)) + elif count_in: + intermediate.append((normalized, item)) + else: + other.append((normalized, item)) + absolute_startswith.sort() startswith.sort() intermediate.sort() other.sort() result = list() - for _list in (startswith, intermediate, other): + for _list in (absolute_startswith, startswith, intermediate, other): for item in _list: result.append(item[1]) if len(result) > 29: