bradley bradley - 10 months ago 77
Python Question

Partial matching GAE search API

Using the GAE search API is it possible to search for a partial match?

I'm trying to create autocomplete functionality where the term would be a partial word. eg.

> b

> bui

> build

would all return "building".

How is this possible with GAE?


Though LIKE statement (partial match) is not supported in Full Text Search, but you could hack around it.

First, tokenize the data string for all possible substrings (hello = h, he, hel, lo, etc.)

def tokenize_autocomplete(phrase):
    a = []
    for word in phrase.split():
        j = 1
        while True:
            for i in range(len(word) - j + 1):
                a.append(word[i:i + j])
            if j == len(word):
            j += 1
    return a

Build an index + document (Search API) using the tokenized strings

index = search.Index(name='item_autocomplete')
for item in items:  # item = ndb.model
    name = ','.join(tokenize_autocomplete(
    document = search.Document(
        fields=[search.TextField(name='name', value=name)])

Perform search, and walah!

results = search.Index(name="item_autocomplete").search("name:elo")

Refer to: