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

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

问题描述

  NeedIndexError:没有匹配的数据找到索引。 
这个查询的建议索引是:

- kind:Message
属性:
- name:author
- name:ref
- name:date

如果我不尝试按日期排序,查询运行时没有错误。数据存储索引下的appengine控制台说:

 作者▲,ref▲,日期▼
服务

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



以下是我的实体定义:

  from google。 appengine.ext import ndb 
$ b $ 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 ,用户=无):
query = Message.query()
query = query.filter(Message.ref == ref)
如果用户:
query = query.filter (Message.author == user.key)
query = query.order(Message.date)

#转换为列表,以便像索引数组一样索引
return [消息在查询中的消息]

我的index.yaml包含:

 索引:

- kind:Message
属性:
- name:author
- name:ref
- name:date
direction:desc


解决方案

您还需要指定方向,因为在编写索引以加快Google风格时,排序完成。



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

 索引:

- kind:Message
属性:
- name:author
- name:ref
- name:date
direction:desc

下面是关于订单的官方描述:


无论是asc升序还是降序
降序。这仅适用于查询的排序顺序
中使用的属性,并且必须与查询使用的方向相匹配。
的默认值是asc。


我希望这有助于。


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

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!

Here is my entity definition:

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)

and this is the query that fails:

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 ]

My index.yaml contains:

indexes:

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

解决方案

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

So, your index.yaml should be like:

indexes:

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

Here's Google's official description about order:

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.

I hope this helps.

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

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