2
votes

I am new to Google Datastore and following this google documentation.

https://cloud.google.com/datastore/docs/datastore-api-tutorial

I have done authorization for calling Google APIs by reading this doc.

https://developers.google.com/identity/protocols/application-default-credentials#callingpython

I have only two files:

1. client_secret.json
2. datastoreConnection.py

and this is my datastoreConnection.py till now:

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/client_secret.json'

from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from google.cloud import datastore

client = datastore.Client(project ="xyz",namespace = "staging")

def list_tasks(client):
    query = client.query(kind='testData')
    query.projection = ['patientName']
    return list(query.fetch(limit=10))

print(list_tasks(client))

This code is working fine and returning me the required data.

Problem is arising when I apply Projection with multiple properties. e.g

query.projection = ['patientName','age']

code is giving me the error:

google.cloud.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:<br/>- kind: testData<br/>  properties:<br/>  - name: age<br/>  - name: patientName<br/>

For making projection query I read this. https://cloud.google.com/datastore/docs/concepts/queries#datastore-projection-query-python

I have cross checked the property names but still having the same error. How can I solve this ?

I have seen index.yaml file in other questions related to this kind of problem. Is this necessary to use and what are its benefit?

1

1 Answers

1
votes

Any time you need to retrieve more than one property, but not all properties (*), you need to have a matching composite index. In this case you need a composite index on patient name and age properties. You can create a composite index using index.yaml file or gcloud command line tool. The documentation below should help you:

https://cloud.google.com/datastore/docs/concepts/indexes.