查询 GAE 数据存储时如何修复索引错误? [英] How to fix index error when querying GAE datastore?

查看:30
本文介绍了查询 GAE 数据存储时如何修复索引错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对按日期排序的数据存储区运行查询时,出现以下错误:

When I try to run a query on the datastore ordered by date I get the following error:

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

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

如果我不尝试按日期排序,则查询运行不会出错.数据存储索引下的 appengine 控制台说:

The query runs without error if I don't try to order by date. The appengine console under datastore indexes says:

author ▲ , ref ▲ , date ▼   
Serving

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

What am I doing wrong? How can I run my query ordered by date? Thanks!

这是我的实体定义:

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 包含:

My index.yaml contains:

indexes:

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

推荐答案

您还需要指定方向",因为排序"是在写入索引时完成的,以 Google 的风格加快速度.

You need to specify the "direction" as well because "ordering" is done when index is written to speed things up in Google's style.

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

So, your index.yaml should be like:

indexes:

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

以下是 Google 关于订单的官方说明:

Here's Google's official description about order:

排序的方向,asc表示升序或desc表示下降.这仅适用于排序顺序中使用的属性的查询,并且必须与查询使用的方向匹配.这默认为 asc.

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.

我希望这会有所帮助.

这篇关于查询 GAE 数据存储时如何修复索引错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆