GAE Simple Searching + Autocomplete

2019-03-22 07:58发布

I'm looking to create a search function for my flash game website.

One of the problems with the site is that it is difficult to find a specific game you want, as users must go to the alphabetical list to find one they want.

It's run with Google App Engine written in python, using the webapp framework.

At the very least I need a simple way to search games by their name. It might be easier to do searching in Javascript from the looks of it. I would prefer an autocomplete functionality. I've tried to figure out how to go about this and it seems that the only way is to create a huge index with each name broken up into various stages of being typed ("S", "Sh", "Sho" ... "Shopping Cart Hero").

Is there anyway to do this simply and easily? I'm beginning to think I'll have to create a web service on a PHP+MySql server and search using it.

2条回答
三岁会撩人
2楼-- · 2019-03-22 08:34

Have you looked at gae-search? I believe the Django + jQuery "autocomplete" feature is not part of the free version (it's just in the for-pay premium version), but maybe it's worth a little money to you.

查看更多
爷、活的狠高调
3楼-- · 2019-03-22 08:35

I have written the code below to handle this. Basically, I save all the possible word "starts" in a list instead of whole sentences. That's how the jquery autocomplete of this site works.

import unicodedata
import re

splitter = re.compile(r'[\s|\-|\)|\(|/]+')

def remove_accents(text):
    nkfd_form = unicodedata.normalize('NFKD', unicode(text))
    return u"".join([c for c in nkfd_form if not unicodedata.combining(c)])

def get_words(text):    
    return [s.lower() for s in splitter.split(remove_accents(text)) if s!= '']

def get_unique_words(text):
    word_set = set(get_words(text))
    return word_set

def get_starts(text):
    word_set = get_unique_words(text)
    starts = set()
    for word in word_set:
        for i in range(len(word)):
            starts.add(word[:i+1])
    return sorted(starts)
查看更多
登录 后发表回答