rewrite live search results to still be pertinent (#4570)

This commit is contained in:
Frédéric Péters 2014-03-26 16:11:32 +01:00
parent 8de1915000
commit d20b6b4e5c
1 changed files with 19 additions and 19 deletions

View File

@ -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: