Searching for Prefixed Strings in Google App Engine Datastore
Querying Google App Engine Datastore to retrieve entities based on a prefix can be achieved through a combination of inequality filters.
To search for all entities where the "Name" property begins with a specific string, use a GQL query as follows:
SELECT * FROM Places WHERE Name > 'prefix' AND Name < 'prefix' + '\xFF'
Alternatively, in Go code, the query can be expressed as:
q := datastore.NewQuery("Places").Filter("Name >", "prefix").Filter("Name <", "prefix" + "\xFF")
This approach ensures that the query retrieves only entities with names greater than (or equal to) the specified prefix and less than the lexicographically next string in sequence. For example, for the prefix "li," it will match names like "liam," "lisotto," and "lizst" but exclude names like "abc," "ljoi," or "qwerty."
Note that the query is case-sensitive, meaning that "List" and "li" are considered distinct values in the lexicographical ordering.
The above is the detailed content of How to Query Datastore for Prefixed Strings in Google App Engine?. For more information, please follow other related articles on the PHP Chinese website!