Google App Engine (python) : Search API : String Search

后端 未结 1 986
栀梦
栀梦 2020-12-08 17:19

i am using the Google App Engine Search API (https://developers.google.com/appengine/docs/python/search/). I have indexed all of the entities and the search is working fine.

相关标签:
1条回答
  • 2020-12-08 17:59

    App Engine's full text search API does not support substring matching.

    However, I needed this behavior myself to support search suggestions as the user types. Here is my solution for that:

    """ Takes a sentence and returns the set of all possible prefixes for each word.
        For instance "hello world" becomes "h he hel hell hello w wo wor worl world" """
    def build_suggestions(str):
        suggestions = []
        for word in str.split():
            prefix = ""
            for letter in word:
                prefix += letter
                suggestions.append(prefix)
        return ' '.join(suggestions)
    
    # Example use
    document = search.Document(
        fields=[search.TextField(name='name', value=object_name),
                search.TextField(name='suggest', value=build_suggestions(object_name))])
    

    The basic idea is to manually generate separate keywords for every possible substring. This is only practical for short sentences, but it works great for my purposes.

    0 讨论(0)
提交回复
热议问题