codeprose-sam
codeprose-sam

Reputation: 572

Google Cloud Datastore runQuery returning 412 "no matching index found"

** UPDATE **

Thanks to Alfred Fuller for pointing out that I need to create a manual index for this query.

Unfortunately, using the JSON API, from a .NET application, there does not appear to be an officially supported way of doing so. In fact, there does not officially appear to be a way to do this at all from an app outside of App Engine, which is strange since the Cloud Datastore API was designed to allow access to the Datastore outside of App Engine.

The closest hack I could find was to POST the index definition using RPC to http://appengine.google.com/api/datastore/index/add. Can someone give me the raw spec for how to do this exactly (i.e. URL parameters, what exactly should the body look like, etc), perhaps using Fiddler to inspect the call made by appcfg.cmd?

** ORIGINAL QUESTION **

According to the docs, "a query can combine equality (EQUAL) filters for different properties, along with one or more inequality filters on a single property".

However, this query fails:

{
 "query": {
  "kinds": [
   {
    "name": "CodeProse.Pogo.Tests.TestPerson"
   }
  ],
  "filter": {
   "compositeFilter": {
    "operator": "and",
    "filters": [
     {
      "propertyFilter": {
       "operator": "equal",
       "property": {
        "name": "DepartmentCode"
       },
       "value": {
        "integerValue": "123"
       }
      }
     },
     {
      "propertyFilter": {
       "operator": "greaterThan",
       "property": {
        "name": "HourlyRate"
       },
       "value": {
        "doubleValue": 50
       }
      }
     },
     {
      "propertyFilter": {
       "operator": "lessThan",
       "property": {
        "name": "HourlyRate"
       },
       "value": {
        "doubleValue": 100
       }
      }
     }
    ]
   }
  }
 }
}

with the following response:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "FAILED_PRECONDITION",
    "message": "no matching index found.",
    "locationType": "header",
    "location": "If-Match"
   }
  ],
  "code": 412,
  "message": "no matching index found."
 }
}

Upvotes: 5

Views: 3683

Answers (4)

rjhcnf
rjhcnf

Reputation: 1057

Try not to order the result. After removing orderby(), it worked for me.

Upvotes: 0

eloone
eloone

Reputation: 4364

For gcloud-node I fixed it with those 3 links:

and most important link:

As explained in the last link, an index is what allows complex queries to run faster by storing the result set of the queries in an index. When you get no matching index found it means that you tried to run a complex query involving order or filter. So to make your query work, you need to create your index on the google datastore indexes by creating a config file manually to define your indexes that represent the query you are trying to run. Here is how you fix:

  1. create an index.yaml file in a folder named for example indexes in your app directory by following the directives for the python conf file: https://cloud.google.com/appengine/docs/python/config/indexconfig#Python_About_index_yaml or get inspiration from the gcloud-node tests in https://github.com/GoogleCloudPlatform/gcloud-node/blob/master/system-test/data/index.yaml
  2. create the indexes from the config file with this command: gcloud preview datastore create-indexes indexes/index.yaml see https://cloud.google.com/sdk/gcloud/reference/preview/datastore/create-indexes
  3. wait for the indexes to serve on your developer console in Cloud Datastore/Indexes, the interface should display "serving" once the index is built
  4. once it is serving your query should work

For example for this query:

 var q = ds.createQuery('project')
  .filter('tags =', category)
  .order('-date');

index.yaml looks like:

indexes:

- kind: project
  ancestor: no
  properties:
  - name: tags
  - name: date
    direction: desc

Upvotes: 1

Max Ross - Google
Max Ross - Google

Reputation: 341

The JSON API does not yet support local index generation, but we've documented a process that you can follow to generate the xml definition of the index at https://developers.google.com/datastore/docs/tools/indexconfig#Datastore_Manual_index_configuration

Please give this a shot and let us know if it doesn't work.

This is a temporary solution that we hope to replace with automatic local index generation as soon as we can.

Upvotes: 6

Alfred Fuller
Alfred Fuller

Reputation: 783

The error "no matching index found." indicates that an index needs to be added for the query to work. See the auto index generation documentation.

In this case you need an index with the properties DepartmentCode and HourlyRate (in that order).

Upvotes: 2

Related Questions