Home >Backend Development >Golang >Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?
Search Entities with Prefixed Names in Google App Engine Datastore
Question:
Is it feasible to retrieve entities from Datastore where a specific field begins with a given string? Despite attempting the following query, I am unable to obtain the desired results:
q = datastore.NewQuery("Places").Filter("Name >", "a")
Answer:
Querying entities based on a field prefix is indeed supported in Datastore, but it necessitates the conjunction of two inequality filters.
For instance, to retrieve Places with names starting with "li," the query should specify that the Name field is greater than (or equal to) "li" and less than "lj." This is because "lj" is the lexicographically subsequent prefix.
In GQL, this query would appear as:
SELECT * FROM Places WHERE Name > 'li' AND Name < 'lj'
In Go code, the query takes the form:
q = datastore.NewQuery("Places").Filter("Name >", "li").Filter("Name <", "lj")
This query will yield Places with names like:
liam lisotto lizst
However, it will exclude names resembling:
abc ljoi lj qwerty
Note that upper- and lowercase letters have distinct lexicographical orders. Thus, "List" precedes "li," while "list" follows "li."
The above is the detailed content of Is it Possible to Query for Entities with Prefix-Matched Names in App Engine Datastore?. For more information, please follow other related articles on the PHP Chinese website!