【问题标题】:GAE NeedIndexError:GAE 需要索引错误:
【发布时间】:2017-04-21 19:08:07
【问题描述】:

当正在运行的应用程序处理对 ViewDogs 的获取请求时,返回的错误是 500 内部服务器错误。在 GAE Stack Driver 控制台我得到:

NeedIndexError: no matching index found - recommended index is: 
- kind:Dog 
  ancestor: yes 
  properties: - 
     name: id 
     direction: desc".

我确认加载了一个名为 Dog 的索引,它当前显示“正在服务”

同时确认成功创建了 4 条狗,它们可以从 DataStore Entities 选项卡中单独查询,也可以使用 SELECT * FROM Dog 进行查询。另请注意注释掉的行 self.response.write('You dog!') 在未注释时按预期返回,因此可能不是路线问题。我可以通过 GET 返回一条狗(省略此代码)类 ViewAllDogs 中的代码可能有问题,我已尽力使用 GAE 文档。

index.yaml 文件,我确认已上传并且状态为:“serving”

indexes:
- kind: Dog
  ancestor: yes
  properties:- name: id
    direction: desc
  - name: name
    direction: desc
  - name: type
    direction: desc
  - name: weight
    direction: desc
  - name: boarded
    direction: desc

app.yaml 文件:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app

main.py

import webapp2
from datetime import datetime
from google.appengine.ext import ndb
import webbrowser
import json

def gql_json_parser(query_obj):
    result = []
    for entry in query_obj:
        result.append(dict([(p, unicode(getattr(entry, p))) for p in entry.properties()]))
    return result

class Dog(ndb.Model):
    """ Models an individual Dog """
    id = ndb.StringProperty(required = True, indexed = True)
    name = ndb.StringProperty(required = True, indexed = True)
    type = ndb.StringProperty(required = True, indexed = True)
    weight = ndb.IntegerProperty(required = True, indexed = True)
    boarded = ndb.BooleanProperty(required = True, indexed = True)

    @classmethod
    def query_dog(cls, ancestor_key):
        return cls.query(ancestor=ancestor_key).order(-cls.id)

class ViewAllDogs(webapp2.RequestHandler):
    def get(self):
#       self.response.write('You dog!')
        parent_dog = self.request.get('parent_dog')
        ancestor_key = ndb.Key("Dog", parent_dog or '*noDogs*')
        query_data = Dog.query_dog(ancestor_key).fetch(10)
        json_query_data = gql_json_parser(query_data)
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(json_query_data))

app = webapp2.WSGIApplication([
    ('/ViewDogs', ViewAllDogs)
], debug=True)

类似的问题比比皆是,包括Query google app engine datastore for all entities,但没有一个能解决我的问题。谢谢。

【问题讨论】:

  • 请注意缺少的索引与您定义的索引不同。您需要的索引必须完全符合要求。
  • 您是否在几分钟后再次尝试?当您对查询或数据结构进行更改时,数据存储区索引在部署后需要几分钟来更新(可能需要数分钟,具体取决于数据的大小)。在生产环境中,您可能希望使用--no-promote 标志,等待索引更新,然后将流量引导到部署的新版本。

标签: google-app-engine google-cloud-datastore gae-python27


【解决方案1】:

像往常一样,我让它变得比需要的复杂得多......

import webapp2, json
from datetime import datetime
from google.appengine.ext import ndb

MAXDOGS = 100

class Dog(ndb.Model):
    """ Models an individual Dog """
    id = ndb.StringProperty(required = True, indexed = True)
    name = ndb.StringProperty(required = True, indexed = True)
    type = ndb.StringProperty(required = True, indexed = True)
    weight = ndb.IntegerProperty(required = True, indexed = True)
    boarded = ndb.BooleanProperty(required = True, indexed = True)

class ViewAllDogs(webapp2.RequestHandler):
    def get(self):
        query_data = Dog.query()
        results = query.fetch(limit = MAX_SLIPS)
        aList = []
        for match in results:
            aList.append({'id': match.id, 'name': match.name, 'type': match.type, 
                          'weight': match.weight, 'boarded': match.boarded})
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(aList))

app = webapp2.WSGIApplication([
    ('/ViewDogs', ViewAllDogs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2019-11-24
    相关资源
    最近更新 更多