【发布时间】:2015-03-05 17:41:53
【问题描述】:
假设我有一个简单的实体层次结构设置,其中基(抽象)类具有公共字段:
@Entity
public abstract class Base {
@Id
Long mId;
@Index
Long mVal;
}
现在我有了一个子类(在 Java 和 Objectify 中):
@Subclass(index=true)
public class Concrete extends Base {
@Index mOtherVal;
}
保存Concrete 时,它会在数据存储中为所有字段(mId、mVal 和mOtherVal)正确创建条目。但是,如果我尝试使用 mVal 上的过滤器对 Concrete 条目运行查询,应用引擎会抱怨没有索引:
List<Concrete> result =
OfyService.ofy().load().type(Concrete.class).filter("mVal > ", 10).list();
我看到这样的异常日志:
com.google.api.server.spi.SystemService invokeServiceMethod: cause={0}
com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found.
The suggested index for this query is:
<datastore-index kind="Post" ancestor="false" source="manual">
<property name="^i" direction="asc"/>
<property name="mVal" direction="asc"/>
</datastore-index>
我没有故意创建datastore-indexes.xml,因为 Objectify 文档指出字段索引是动态创建的。所以我的问题是:这是 Objectify 的已知限制,还是我做错了什么?
【问题讨论】:
-
据我所知,这应该是可能的,但我明白为什么需要复合索引。因为您只过滤子类(保存在
^i属性中),所以查询需要类信息(^i) 和mVal的复合索引。我的猜测是 Objectify 文档指的是单个属性索引,但我会遵从 stickfigure ! -
谢谢,这是我的担心。关于这种类型的复合索引,文档有点模糊。我将尝试在
datastore-indexes.xml中注入复合材料,看看它是否会改变行为。 -
@LarrySchiefer 你不需要自己添加它。确保您需要的所有索引的一个好主意是在您的开发服务器上运行所有查询。这将填充“datastore-indexes.xml”本身(在此处查看我的完整答案:stackoverflow.com/questions/28424157/…
-
感谢@Patrice,感谢您的反馈。
-
@LarrySchiefer 非常欢迎 :) 复合索引不容易适应。我宁愿不冒险,运行我的 devserver 来创建所有内容,然后查看 datastore-indexes.xml 并删除我不需要快速的超大索引(利用 zig-zag 合并)
标签: google-app-engine objectify