Created
March 13, 2012 06:34
-
-
Save y-ogi/2027258 to your computer and use it in GitHub Desktop.
model for full text search on google app engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvertedNameIndex(db.Model): | |
""" | |
転置Index | |
名前用の転置Index | |
""" | |
word = db.StringProperty(required=True) | |
keys = db.StringListProperty() | |
key_template = 'inverted_name_index/%s/%s' | |
@classmethod | |
def add(cls, word, key, count=0): | |
key_name = cls.key_template % (count, word, ) | |
index = cls.get_by_key_name(key_name) | |
if not index: | |
index = InvertedNameIndex(word=word, key_name=key_name) | |
if len(index.keys) > 1000: | |
cls.add(cls, word, key, count + 1) | |
return | |
# 追加 | |
if not key in index.keys: | |
index.keys.append(key) | |
index.put() | |
@classmethod | |
def indexing(cls, key): | |
""" | |
転置Indexの生成を行う | |
今は、名前を想定しているために、以下の様に区切り、それぞれのIndexとする。 | |
例: | |
豊臣秀吉 | |
-> /豊/豊臣/豊臣秀/豊臣秀吉/ | |
名前と名字で区切りたかったりした場合は、Unigramを使う必要があるかも。 | |
要件に合わせて、Indexの生成方法を変える必要がある。 | |
""" | |
model = db.get(key) | |
try: | |
getattr(model, 'searchable_prop') | |
except: | |
pass | |
else: | |
value = getattr(model, model.searchable_prop) | |
if value: | |
words = [value[0:i + 1] for i in range(0, len(value))] | |
for word in words: | |
cls.add(word, key) | |
@classmethod | |
def search(cls, word): | |
models = [] | |
count = 0 | |
while True: | |
key_name = cls.key_template % (count, word,) | |
index = cls.get_by_key_name(key_name) | |
if not index: break | |
models = models + db.get(index.keys) | |
count = count + 1 | |
return models |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment