8

当我尝试运行日期我收到以下错误下令数据存储查询:查询GAE数据存储时如何解决索引错误?

NeedIndexError: no matching index found. 
The suggested index for this query is: 

- kind: Message 
    properties: 
    - name: author 
    - name: ref 
    - name: date 

查询运行没有错误,如果我不尝试通过日期命令。数据存储区索引下的appengine控制台说:

author ▲ , ref ▲ , date ▼ 
Serving 

我在做什么错?我如何运行按日期排序的查询?谢谢!

这里是我的实体定义:

from google.appengine.ext import ndb 

class Message(ndb.Model): 
    subject = ndb.StringProperty() 
    body = ndb.TextProperty() 
    date = ndb.DateTimeProperty(auto_now_add=True) 
    ref = ndb.StringProperty(required=True) 
    author = ndb.KeyProperty(required=True) 

,这是失败的查询:

def readMessages(ref, user = None): 
    query = Message.query() 
    query = query.filter(Message.ref == ref) 
    if user: 
     query = query.filter(Message.author == user.key) 
    query = query.order(Message.date) 

# convert to a list so we can index like an array 
return [ message for message in query ] 

我index.yaml中包含:

indexes: 

- kind: Message 
    properties: 
    - name: author 
    - name: ref 
    - name: date 
    direction: desc 

回答

4

你需要指定“方向”,因为“订购”是在编写索引时完成的,以加速Google的风格。

所以,你的index.yaml中应该是这样的:

indexes: 

- kind: Message 
    properties: 
    - name: author 
    - name: ref 
    - name: date 
    direction: desc 

下面是谷歌官方的介绍有关订单:

The direction to sort, either asc for ascending or desc for descending. This is only required for properties used in sort orders of the query, and must match the direction used by the query. The default is asc.

我希望这有助于。

+0

谢谢。其实,我忘了复制索引定义的最后一行。应用引擎控制台指示索引已创建为参考▲,作者▲,日期▼。所以我不认为这是我的问题,但我已经更新了问题中的索引定义。 – deltanine 2013-04-22 03:07:27

3

感谢劳伦斯,你让我走上了正轨 - 我想我找到了答案。查询必须完全匹配索引定义。

我通过在数据存储管理器框中尝试不同的GQL查询发现了这一点。

例如,以下2个查询:

SELECT * FROM Message where ref='' and author='' order by date 
SELECT * FROM Message where ref='' and author='' order by date asc 

都失败:

no matching index found. 

The suggested index for this query is: 
- kind: Message 
    properties: 
    - name: author 
    - name: ref 
    - name: date 

然而,

SELECT * FROM Message where ref='' and author='' order by date desc 

成功。同样地,其中有少参数比索引包含查询也将失败,例如:

SELECT * FROM Message where ref='' order by date DESC 

失败:

no matching index found. 

The suggested index for this query is: 
- kind: Message 
    properties: 
    - name: ref 
    - name: date 
    direction: desc 

所以,问题是我的查询,该行:

query = query.order(Message.date) 

实际上是按升序排序,但我的索引是DESCENDING命令。解决办法是:

query = query.order(-Message.date) 
+0

不客气!是的,这必须与您的查询和索引顺序完全匹配。 – 2013-05-25 02:09:12

相关问题