英文:
Ancestor Query causes API error 4 (datastore_v3: NEED_INDEX): no matching index found error
问题
我遇到了一个祖先查询的巨大困难。
以下是可行的代码:
...
uk := datastore.NewKey(c, config.DatastoreDuelIdKind, did, 0, nil)
_, err := datastore.NewQuery(config.DatastoreQuestionInDuelKind).Ancestor(uk).GetAll(c, &roundsPlayedInDuel)
...
上述代码可以得到正确的结果。现在,如果我在config.DatastoreQuestionInDuelKind的属性上添加一个Order过滤器,查询将失败并显示NEED_INDEX错误。
但是下面的代码会失败:
_, err := datastore.NewQuery(config.DatastoreQuestionInDuelKind).Order("RoundNumber").Ancestor(uk).GetAll(c, &roundsPlayedInDuel)
唯一的区别是添加了Order过滤器。
现在,我已经在index.yaml中定义了一个索引,如下所示:
indexes:
- kind: gcx_Round_Id_Duel_Id
properties:
- name: DId
- name: RoundNumber
从App Engine仪表板上我可以看到它正在服务。但仍然收到索引错误。有什么想法吗?
英文:
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?
答案1
得分: 2
你的索引缺少"ancestor"属性,并且你的查询似乎没有使用"DId",所以不需要包含它:
- 类型:gcx_Round_Id_Duel_Id
祖先:是
属性:- 名称:RoundNumber
你必须明确定义这个索引,因为你将祖先查询与排序结合在一起 - 自动索引只允许你单独执行这两个操作。所以,考虑到你要获取所有实体,你可能更高效地放弃查询排序,然后使用代码对返回的实体进行排序。
英文:
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论