Search code examples
pythongoogle-app-enginesearchautocompletewebapp2

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?


Solution

  • 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):
                    break
                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(item.name))
        document = search.Document(
            doc_id=item.key.urlsafe(),
            fields=[search.TextField(name='name', value=name)])
        index.put(document)
    

    Perform search, and walah!

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

    https://code.luasoftware.com/tutorials/google-app-engine/partial-search-on-gae-with-search-api/