google-app-enginegoogle-cloud-datastoregoogle-app-engine-go

Can't get Datastore composite index to work from App Engine Go


I have a Google App Engine Go app that uses Datastore. I am attempting to implement a query that involves inequalities for two properties. I cannot get the composite index to work.

Here is the schema for the kind:

type LicenseKeyUsage struct {
    First      time.Time
    Last       time.Time
    Count      int
    Days       int
}

Here is a snippet from the query (lastMin is set to a time and d is set to an integer):

aggregationCountQuery := datastore.NewQuery("LicenseKeyUsage").
    FilterField("Last", ">=", lastMin).
    FilterField("Days", ">=", d).
    NewAggregationQuery().
    WithCount("active_count")

Here is my index.yaml located in the same directory as app.yaml:

indexes:

- kind: LicenseKeyUsage
  properties:
  - name: Last
  - name: Days

I deploy the index file using the command gcloud app deploy index.yaml, then I go to the datastore console to wait until the index is ready, then I update an entity that should get indexed.

From the console, the index is showing no entities.

If I attempt to run my query, I get the following error:

rpc error: code = FailedPrecondition desc = The query contains range and inequality filters on multiple fields, please refer to the documentation for index selection best practices: https://cloud.google.com/datastore/docs/multiple-range-fields. One possible index to serve the query is:
- kind: LicenseKeyUsage
  properties:
  - name: Days
  - name: Last

First of all, the order of the properties in the recommended index do not match the order of my field filters in the query. I switched to that index, but I still get the same error.

Any idea what is going wrong here?


Solution

  • I discovered the cause of this problem. The index was getting created for the default database, but the queries were targeting a non-default database.