Skip to content

Instantly share code, notes, and snippets.

@dmishe
Created August 6, 2009 09:18
Show Gist options
  • Save dmishe/163207 to your computer and use it in GitHub Desktop.
Save dmishe/163207 to your computer and use it in GitHub Desktop.
def word_search(request):
match = request.GET.get('q')
result = []
if match is not None and len(match) >= 2:
# We search different variants of spelling
variants = Variant.objects.filter(
Q(name__istartswith=match) | Q(downcoded__istartswith=match)
).values_list("entry__pk").order_by("name")
words = PhraseTranslation.objects.filter(
Q(pk__in=variants) | \
Q(name__istartswith=match) | Q(downcoded__istartswith=match)
).values_list(
'id', 'name').order_by('name')
for word in words[:200]:
result.append({'v': '%d' % word[0], 'l': word[1]})
if not result:
result.append({'v': '', 'l': 'No matching results.'})
return json_response(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment