Google App Engine (python) : Search API : String Search
Asked Answered
H

1

11

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. but only if i search for the exact matches else it returns 0 results. For Example:

from google.appengine.api import search

_INDEX_NAME = 'searchall'


query_string ="United Kingdom"
query = search.Query(query_string=query_string)
index = search.Index(name=_INDEX_NAME)

print index.search(query)

if i run the following script i do get results as follows :

search.SearchResults(results='[search.ScoredDocument(doc_id='c475fd24-34ba-42bd-a3b5-d9a48d880012', fields='[search.TextField(name='name', value='United Kingdom')]', language='en', order_id='45395666'), search.ScoredDocument(doc_id='5fa757d1-05bf-4012-93ff-79dd4b77a878', fields='[search.TextField(name='name', value='United Kingdom')]', language='en', order_id='45395201')]', number_found='2')

but if i change the query_string to "United Kin" or "United" it return 0 results as follows:

search.SearchResults(number_found='0')

I want to use this API for both normal search and AutoSuggest. What would be the best way to achieve this ?

Horwath answered 9/6, 2012 at 10:53 Comment(0)
N
18

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.

Northeasterly answered 9/6, 2012 at 15:25 Comment(2)
Thanks for your response nick, but i have quiet a large datastore, if i do that for each entity in the datastore would that not effect the size of the datastore ? i have like 4 different searchable models and each of them have more than 100,000 records on an average ..Horwath
Yes, you'd have to store these auto-generated keywords alongside the rest of your data.Northeasterly

© 2022 - 2024 — McMap. All rights reserved.