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

相關問題