【问题标题】:App Engine DataStore - Compound Indexes - datastore-indexes - not workingApp Engine DataStore - 复合索引 - 数据存储索引 - 不工作
【发布时间】:2013-12-11 04:34:31
【问题描述】:

我无法使用复合索引搜索实体。我正在使用 Objectify 4。

实体配置:

@Entity
@Unindex
class MyEntity implements Serialiable
{
    @Id String id;
    String one;
    String two;
    long three;
}

索引配置:

<datastore-indexes autoGenerate="true">
    <datastore-index kind="MyEntity" ancestor="false">
        <property name="one" direction="asc" />
        <property name="two" direction="desc" />
    </datastore-index>
</datastore-indexes>

我看到了DataStore Indexes 中内置的索引。但是,当我使用以下查询进行搜索时,我总是得到空结果。

Objectify ofy = ObjectifyService.ofy();
Query<MyEntity> query = ofy.loader()
                           .type(MyEntity.class)
                           .filter("one", "value-one")
                           .filter("two", "value-two");
List<MyEntity> result = query.list();

response.getWriter().println("Size: " + result.size());
//^^ this is always "0" ^^

我正在使用 HRD。

大家猜猜出了什么问题?顺便说一句,它曾经工作到一段时间……早在上周。现在,它在开发服务器和实际服务器上都不起作用。

【问题讨论】:

  • 补充一点,在服务器上运行 GQL 也会返回零结果。 SELECT * FROM MyEntity where one='value-one' and two='value-two'

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


【解决方案1】:

复合索引需要对所有涉及的属性进行索引:

@Entity
class MyEntity implements Serialiable
{
    @Id String id;
    @Index String one;
    @Index String two;
    long three;
}

当您保存实体而不将字段标记为索引时,Objectify 会保存这些实体而不在这些字段上建立索引,这就是您的 GQL 命令也返回 0 个结果的原因。

【讨论】:

  • IIRC,以前不是这样的,还是这样?
  • 一直都是这样。关于这个问题跟踪器有一个讨论:code.google.com/p/googleappengine/issues/detail?id=4231
  • 我猜,这是由于 Objectify 的变化。早些时候,默认值是“索引”,现在更改为“未索引”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多