在Google App Engine查询中使用多个过滤器

时间:2016-02-28 18:29:08

标签: android google-app-engine objectify

我正在尝试在App Engine中执行查询,此select查询的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;

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

1 个答案:

答案 0 :(得分:2)

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

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

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