Search code examples
phpgoogle-cloud-datastoregql

Equality operators not working in GQL datastore for php


Here is a query in GQL which is not working at modified > timestamp

        $query = $ds->query()
           ->kind('Video')
           ->filter('email', '=', $email)
           ->filter('lat', '=', $lat)
           ->filter('lng', '=', $lng)
           ->filter('modified', '>', 1505807001);

        $result = $ds->runQuery($query);

The query works alright if the greater than timestamp is skipped. Otherwise does not work.

Returns exception. An excerpt is:

{
  "error": {
   "code": 400,
    "message": "no matching index found. recommended index is:\n- kind: 
      Video\n  propert (truncated...)

Any help with this will be much appreciated.


Solution

  • You need to add an explicit composite index (https://cloud.google.com/datastore/docs/concepts/indexes) for your query.

    Without the inequality, Cloud Datastore can use the built-in indexes to do your query, but with the timestamp inequality it isn't possible for Cloud Datastore to do your query.

    You'll probably want an index definition like:

    indexes:
    - kind: Video
      properties:
      - name: email
        direction: desc
      - name: lat
        direction: desc
      - name: lng
        direction: desc
      - name: modified
        direction: asc
    

    As an offhand, if lat & lg are geo points, you'll probably want to use something like geohashing (http://hitching.net/2009/11/10/scalable-fast-accurate-geo-apps-using-google-app-engine-geohash-faultline-correction/) .