1

I'm using following query to retrieve some entities from google Datastore:

var query  = datastore.createQuery(namespace,tableName);
query.select(['displayName','username']);
datastore.getEntitySet(query,function(err,data){
 if(err){
    res.status(500).end();
   }
   else{
    res.send(data);
   }
 });

The above code works fine if I select only one property i.e.

query.select('username');

But with multiple select its throwing 412 'Precondition Failed' error. my entity looks like the following: Entity properties

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • Only selecting a few properties from an entity implies your using a projection and your must have a supporting index. An index is created for each individual property, but you will need to define your own indexes for projection with multiple properties – Tim Hoffman Jan 22 '16 at 11:55
  • Dear @TimHoffman, Thanks a lot for your input, I'm a beginner with Google DataStore, Could you please help me with, how to create supporting index. I'm using gcloud-node API to do Datastore operations. Thanks in advance for your help :) – Ashish Anand Jan 23 '16 at 10:12
  • I'm using google compute engine (not APPEngine) and NodeJS as my runtime. – Ashish Anand Jan 23 '16 at 10:20
  • Have you read any of the documentation - every thing you need is there - https://cloud.google.com/datastore/docs/concepts/indexes – Tim Hoffman Jan 23 '16 at 11:24
  • Dear @TimHoffman, I have already through the doc for which you have shared link. But I'm confused where I can find or create my WEB-INF/datastore-indexes.xml file. If you can help me with this specific question. – Ashish Anand Jan 23 '16 at 12:42

1 Answers1

1

You need to create a multi-property index in order to use multi-property queries.

Because you are not using App Engine, these indexes need to be manually created.

I have a tutorial here that covers this.

Here are the steps:

  1. Install Java 7 Runtime (or later version) http://java.com/
    • I recommend using Cloud Shell which has Java already installed and configured
  2. Create a folder called WEB-INF
  3. Inside that folder, you need three files:
  4. In the datastore-indexes.xml file, you need to define your multi-property indexes. Follow the documentation.
  5. Install the gcd tool
  6. Finally, run the gcd tool (one directory above the WEB-INF folder)
    • Linux/Mac path/to/gcd.sh updateindexes --auth_mode=oauth2 .
    • Windows path/to/gcd.cmd updateindexes --auth_mode=oauth2 .

After a few minutes, your indexes should be created.

Sandeep Dinesh
  • 2,035
  • 19
  • 19