问题
I want to search all entities whose name starts with a specific string, is this possible in Datastore?
I've tried this:
q = datastore.NewQuery("Places").Filter("Name > ", "a")
But it doesn't work.
If this is not possible, what alternative solution can you suggest to me? BigQuery? BigTable or other services on App Engine?
回答1:
This is something that is possible, but with a combination of 2 inequality filters.
Let's say you want to list Places
that have the "li"
prefix. This can be described with a query that lists Places
that are greater than (or equal to) "li"
and less than a prefix that is the next string after "li"
in lexicographical order: "lj"
.
This is how the GQL looks like:
SELECT * FROM Places WHERE Name > 'li' AND Name < 'lj'
Coded in Go it looks like this:
q = datastore.NewQuery("Places").Filter("Name >", "li").Filter("Name <", "lj")
This will list Places
where name is for example:
liam
lisotto
lizst
But will exclude names like:
abc
ljoi
lj
qwerty
One thing to note: small and capital letters are different in lexicographical order, so for example "List"
is less than "li"
(even though "list"
is greater than "li"
)!
来源:https://stackoverflow.com/questions/39279779/search-a-string-beginning-with-a-prefix-in-google-app-engine-datastore