8

Quando provo ad eseguire una query sul datastore ordinati per data ottengo il seguente errore:Come correggere l'errore di indice quando si esegue una query sul datastore GAE?

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

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

la query viene eseguita senza errori se non cerco di ordinare per data. La console Appengine sotto gli indici del datastore dice:

author ▲ , ref ▲ , date ▼ 
Serving 

Cosa sto facendo male? Come posso eseguire la mia richiesta ordinata per data? Grazie!

Qui è la mia definizione di entità:

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) 

e questa è la query che non riesce:

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 ] 

mio index.yaml contiene:

indexes: 

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

risposta

4

È necessario specificare il "direction", anche perché "ordering" viene eseguito quando l'indice viene scritto per velocizzare le cose nello stile di Google.

Così, il vostro index.yaml dovrebbe essere come:

indexes: 

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

Ecco descrizione ufficiale di Google su ordine:

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.

Spero che questo aiuta.

+0

Grazie. In realtà, ho dimenticato di copiare l'ultima riga della mia definizione dell'indice.La console del motore dell'app indica che l'indice è stato creato come riferimento ▲, autore ▲, data ▼. Quindi non penso che questo sia il mio problema, ma ho aggiornato la definizione dell'indice nella domanda. – deltanine

3

Grazie Lawrence, mi hai messo sulla strada giusta, penso di aver trovato la risposta. La query deve ESATTAMENTE corrispondere alla definizione dell'indice.

L'ho scoperto provando diverse query GQL nella casella di amministrazione del datastore.

Ad esempio, i seguenti 2 domande:

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

sia sicuro con:

no matching index found. 

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

Tuttavia,

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

riesce. Allo stesso modo, le query che hanno meno parametri rispetto l'indice contiene anche fallire, ad esempio:

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

fallisce con:

no matching index found. 

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

Quindi il problema era nella mia interrogazione, la linea:

query = query.order(Message.date) 

sta effettivamente ordinando in ordine crescente, ma il mio indice dice ordine decrescente. La correzione è:

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

prego! sì, deve essere una corrispondenza esatta della query e dell'ordine dell'indice. –

Problemi correlati