2016-02-28 29 views
0

我想在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; 

索引我不知道我在做什么错在这里,我知道它是与过滤器有问题,因为如果我把它们注释掉查询执行罚款。那么我做错了什么?

+1

我建议您重新阅读文档https://cloud.google.com/appengine/docs/java/config/indexconfig - 有什么不清楚的是您可以为WEB-INF /数据存储库中的数据存储区索引指定配置-indexes.xml,在您的应用程序的war /目录中。 '其次,你还应该了解一下围绕平等过滤器的限制。 https://cloud.google.com/appengine/docs/java/datastore/queries#Java_Restrictions_on_queries - 即使您创建了索引,您也只能注意到“单个查询可能不会使用多于一个的不等式比较property' –

回答

1

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

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

注意的是,为了特定实体实例参加多人房地产指数,它必须与索引相关的各个字段保存。

+0

我刚刚通过链接文档阅读,我仍然不明白我必须做什么,我没有在我的项目中有一个文件'datastore-indexes.xml',如果它的自动生成,那么我怎么能改变它?你有链接到一个例子或什么? – tyczj

+0

在WEB-INF目录中创建文件。不幸的是,我没有更好的链接。 – stickfigure