向Objectify查询添加排序会导致空结果

时间:2014-02-11 00:45:02

标签: google-app-engine google-cloud-datastore objectify

我正在尝试使用App Engine,Datastore和Objectify 4.我有一个工作的Objectify查询,它从数据存储区中获取给定类型的所有实体。我正在尝试根据日期对结果进行排序:

List<MyEntity> entities = OfyService.ofy().load().type(MyEntity.class).order("-createdDate").list();

但是在我添加订单后,查询返回0条记录。这是我的实体类:

@Entity
public class MyEntity
{   
    @Id Long id;
    Long userID;
    @Ignore String username;
    @Index String name;
    String url;
    String description;
    @Index Date createdDate;
    @Index int viewCount;
}

我尝试按其他数据类型订购但没有成功。为什么会这样?

2 个答案:

答案 0 :(得分:2)

我们需要在WEB-INF / datastore-indexes.xml中手动定义索引。

<?xml version="1.0" encoding="utf-8"?>
<datastore-indexes
  autoGenerate="true">
    <datastore-index kind="AppMedia" ancestor="false">
        <property name="createdDate" direction="desc" />
    </datastore-index>
</datastore-indexes>

然后正确创建索引并且查询将起作用。 注意:查询仅适用于正确配置索引后添加的行。

答案 1 :(得分:0)

我通过cloud.google.com查看了数据存储区,在打开任何实体时,所有字段都标记为“未编入索引”,包括在我的实体类中使用@Index定义的字段。当我在数据存储区中手动标记“索引”字段时,带有排序的查询有效。当通过Objectify保存时,不应该在实体中使用@Index标记的所有字段都在数据存储区中编入索引吗?我在这里缺少什么?

相关问题