在 Datastore GQL 中执行通配符查询

Do a wildcard query in Datastore GQL

我正在试用 Google Cloud 的数据存储,并且 运行 进入了一个我无法理解的场景。

我有两个类型为 searchterm 的实体,都带有 searchterm 属性,一个 "pink chicken",另一个带有 "red duck"。

我正在尝试使用 GQL select * from searchterm where searchterm contains "chicken" 检索具有 "pink chicken" 的 searchterm 属性 的实体。但是,它似乎不允许我这样做。

我必须完整说明 select * from searchterm where searchterm contains "pink chicken" 才能获得相关回复。

GQL 中的contains 不是这个意思吗?我可以执行其中包含通配符的 GQL 查询来匹配字符串吗?

是的,我检查过,searchterm 属性 已编入索引。

谢谢! :D

Cloud Datastore 不支持此类查询,CONTAINScontains 也一样,但它会搜索子字符串。对于像您这样的情况,请使用 Search API.

你可以参考这个quote here:

Notice that the operator = is another name for the IN and CONTAINS operators. For example, <value> = <property-name> is the same as <value> IN <property-name>, and <property-name> = <value> is the same as <property-name> CONTAINS <value>. Also <property-name> IS NULL is the same as <property-name> = NULL.

关于数据存储不支持这种查询的事实请参考this link:

Restrictions on queries
The nature of the index query mechanism imposes certain restrictions on what a query can do. Cloud Datastore queries do not support substring matches, case-insensitive matches, or so-called full-text search. The NOT, OR, and != operators are not natively supported, but some client libraries may add support on top of Cloud Datastore.

您可以在 GQL 中使用“%”,因此请尝试过滤“%chicken%”的查询,应该会得到您要查找的结果。