【问题标题】:Objectify filter not working after adding @index to fields将@index 添加到字段后,Objectify 过滤器不起作用
【发布时间】:2014-12-29 00:08:33
【问题描述】:

我正在尝试使用 Objectify 从数据存储中获取过滤后的列表,但总是返回一个空列表。我尝试添加 @index 并创建一个 datastore-index.xml 文件,但仍然未定义。

我的列表类:

@Entity
@Index
public class Listing {
    @Id private Long id;
    @Index private double price;

...

我的 API:

@Api(name ="xxxx")
@PersistenceCapable(detachable = "true")
public class ListingServiceAPI {

    @ApiMethod(name = "getListings")
    public List<Listing> getListings() {
        return ofy().load().type(Listing.class).filter("price >", 15).list(); //this fails

        //return ofy().load().type(Listing.class).limit(3).list(); //this works
    }
}

datastore-indexes.xml:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<datastore-indexes autoGenerate="true">
  <datastore-index kind="Listing" ancestor="false">
    <property name="price" direction="asc" />
    <property name="category" direction="asc" />
  </datastore-index>
</datastore-indexes>

有人知道如何解决此问题以使过滤器查询正常工作吗?

【问题讨论】:

    标签: java google-app-engine filter objectify


    【解决方案1】:

    如果您在数据已存储在 Datastore 中之后对属性进行了索引,则您的查询将继续为空。发生这种情况是因为 App Engine 在保存所有实体时为其编制索引。更改索引属性列表后,您必须重新保存所有实体。

    【讨论】:

    • 谢谢!这让它显示新数据,但现在它只返回所有新数据,而不仅仅是> 15。你知道那里可能有什么问题吗? Edit 我将其更改为 15.0 并且可以正常工作。哇!
    • 是的,您必须注意数据的类型。如果您存储双精度数,那么您应该在过滤器中使用双精度数。
    猜你喜欢
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多