google-cloud-datastoregql

Google Cloud Firestore in Datastore Mode - projection query and indexes


I've read all the docs, but am struggling with this query in GCP Firestore in Datastore Mode:

SELECT id, title FROM `Book` WHERE author = 'Twain'

I've tried all the permutations of properties and indexes and where clauses that I can think of... What works:

SELECT id, title FROM `Book`

because I have created this index.yaml and built it with gcloud datastore indexes create index.yaml

indexes:

- kind: Book
  ancestor: no
  properties:
  - name: id
  - name: title

I can also

SELECT * FROM `Book` WHERE author = 'Twain'

because author is an indexed property of the Book entity.

I've tried adding author to the index (in addition to title and id), but Datastore still complains "GQL Query error: Your Datastore does not have the composite index (developer-supplied) required for this query."

What am I missing? The entity is quite large, and I just want to retrieve a list of the titles to populate the web page, not every property! I also don't want to retrieve the entire set of entities, since obviously only a few of them have been written by a particular author.


Solution

  • If you open up the 'Developer Tools' in your favourite browser, you can see the full error returned to your query is:

    "no matching index found. recommended index is:\n- kind: Book\n properties:\n - name: author\n - name: id\n - name: title\n",

    Pretty printing that, we get:

    - kind: Book
      properties:
      - name: author
      - name: id
      - name: title
    

    Which is the expected index because the filtering happens on author, and the id and title fields are pulled from the index for your query.