Search code examples
google-cloud-datastoregql

Do a wildcard query in Datastore GQL


I'm trying out Google Cloud's datastore, and have run into a scenario I can't figure out.

I've got two entities of kind searchterm, both with a searchterm property, one "pink chicken", and the other with "red duck".

I'm attempting to use the GQL select * from searchterm where searchterm contains "chicken" to retrieve the entity that has the searchterm property of "pink chicken". However, it doesn't seem to allow me to do that.

I have to fully state select * from searchterm where searchterm contains "pink chicken" to get the relevant response.

Does contains in GQL not mean what it implies? Would it be possible for me to perform a GQL query that has a wildcard in it to match strings?

Yes, I checked, that searchterm property IS indexed.

Thanks! :D


Solution

  • Cloud Datastore does not support such kind of queries and CONTAINS or contains is just there as equal but it does search substrings. For such cases like yours use the Search API.

    You can refer this 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.

    And about the fact that datastore does not support this kind of queries refer to 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.