【问题标题】:Using multiple filters in Google App Engine Query在 Google App Engine 查询中使用多个过滤器
【发布时间】:2016-02-28 18:29:08
【问题描述】:

我正在尝试在App Engine 中执行查询,此选择查询的 sql 版本将如下所示

"WHERE (personId ="+personId+" AND uploadedDate >"+lastCheckDate+") OR ("+personId ="+personId+" AND updatedDate >"+lastCheckDate+")"

在我的应用引擎后端,我创建了多个过滤器并使用CompositeFilterOperator 使用Objectify 组合过滤器

Filter f1 = new FilterPredicate("personId", FilterOperator.EQUAL,personId);
Filter f2 = new FilterPredicate("uploadDate", FilterOperator.GREATER_THAN,lastCheckDate);
Filter f3 = new FilterPredicate("updatedDate", FilterOperator.GREATER_THAN,lastCheckDate);

Filter final1 = CompositeFilterOperator.and(f1,f2);
Filter final2 = CompositeFilterOperator.and(f1,f3);

Filter qFilter = CompositeFilterOperator.or(final1,final2);
Query<CloudRecord> query = ofy().load().type(CloudRecord.class).filter(qFilter).limit(limit);

但是当我在我的 android 应用程序中调用它时出现此错误

com.google.api.client.googleapis.json.GoogleJsonResponseException: 503 Service Unavailable
 {
   "code": 503,
   "errors": [
     {
       "domain": "global",
       "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found.\nThe suggested index for this query is:\n    <datastore-index kind=\"CloudRecord\" ancestor=\"false\" source=\"manual\">\n        <property name=\"personId\" direction=\"asc\"/>\n        <property name=\"uploadDate\" direction=\"asc\"/>\n    </datastore-index>\n\n",
       "reason": "backendError"
     }
   ],
   "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found.\nThe suggested index for this query is:\n    <datastore-index kind=\"CloudRecord\" ancestor=\"false\" source=\"manual\">\n        <property name=\"personId\" direction=\"asc\"/>\n        <property name=\"uploadDate\" direction=\"asc\"/>\n    </datastore-index>\n\n"
 }

所有这些字段都在类中索引

@Entity public class CloudRecord {

    @Id
    Long id;

    @Index
    long uploadDate;
    @Index
    long updatedDate;
    @Index
    String personId;

我不确定我在这里做错了什么,我知道这是过滤器的问题,因为如果我将它们注释掉,查询就会正常执行。那我做错了什么?

【问题讨论】:

标签: android google-app-engine objectify


【解决方案1】:

GAE 有两种索引:单属性索引,您使用@Index 声明并在保存实体时创建,以及多属性索引,在 datastore-indexes.xml(或 yml)中定义并且是自动构建的。您正在发出一个需要多属性索引的查询。更多文档可以在这里找到:

https://cloud.google.com/appengine/docs/java/config/indexconfig

请注意,为了让特定实体实例参与多属性索引,它必须与索引的相关单个字段一起保存。

【讨论】:

  • 我刚刚阅读了链接的文档,但我仍然不明白我必须做什么,我的项目中没有文件datastore-indexes.xml,如果它是自动生成的,那么我该如何更改它?您有示例或其他内容的链接吗?
  • 在您的 WEB-INF 目录中创建文件。不幸的是,我没有更好的链接。
猜你喜欢
  • 2010-12-12
  • 2012-04-27
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
相关资源
最近更新 更多