Search a string beginning with a prefix in Google App Engine Datastore

橙三吉。 提交于 2019-12-04 03:52:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!