Ralf
Ralf

Reputation: 2632

Ancestor Query causes API error 4 (datastore_v3: NEED_INDEX): no matching index found error

I'm having huge difficulties with an ancestor query.

Here is the code that works:

...
uk := datastore.NewKey(c, config.DatastoreDuelIdKind, did, 0, nil)
_, err := datastore.NewQuery(config.DatastoreQuestionInDuelKind).Ancestor(uk).GetAll(c, &roundsPlayedInDuel)
...

The above code yields the correct results. Now, if I add an Order filter on a property of config.DatastoreQuestionInDuelKind the query fails with the NEED_INDEX error.

But this one fails:

_, err := datastore.NewQuery(config.DatastoreQuestionInDuelKind).Order("RoundNumber").Ancestor(uk).GetAll(c, &roundsPlayedInDuel)

The only difference being the added Order filter.

Now, I have defined an index in index.yaml like so:

indexes:

- kind: gcx_Round_Id_Duel_Id
  properties:
  - name: DId
  - name: RoundNumber

From App Engine Dashboard I can see, that it is serving. Still getting the index error. Any ideas?

Upvotes: 0

Views: 612

Answers (1)

Greg
Greg

Reputation: 10360

Your index is missing the 'ancestor' property, and the query you've doesn't appear to be using 'DId', so no need to include that:

- kind: gcx_Round_Id_Duel_Id
  ancestor: yes
  properties:
  - name: RoundNumber

You have to explicitly define this index because you're combining the ancestor query with a sort - the automatic indexes will allow you to do either of those individually. (So given you're fetching all of the entities, it would probably be more efficient for you to forego the query-ordering and then sort the returned entities with code.)

Upvotes: 2

Related Questions