将对象列表放入Infinispan缓存并按其搜索

时间:2017-11-18 16:42:11

标签: caching search infinispan

我将对象列表放入缓存中,我需要通过其属性值进行选择:

        Cache<Object, Object> cache = cacheManager.getCache(searchResultName);
    assertThat(cache, is(notNullValue()));


    Container container = new Container();

    List<Detail> details = Lists.newArrayList();

    details.add(new Detail(2, "two"));
    details.add(new Detail(2, "two"));
    details.add(new Detail(3, "three"));
    details.add(new Detail(3, "three"));
    details.add(new Detail(3, "three"));
    details.add(new Detail(5, "five"));
    details.add(new Detail(5, "five"));
    details.add(new Detail(5, "five"));
    details.add(new Detail(5, "five"));
    details.add(new Detail(5, "five"));


    cache.put("q", details);

    QueryFactory queryFactory = org.infinispan.query.Search.getQueryFactory(cache);
    org.infinispan.query.dsl.QueryBuilder queryBuilder = queryFactory.from(Detail.class)
            .having("number").eq(5);
    details = queryBuilder.build().list();
    assertThat(details.size(), is(5));

@Indexed
class Container {

    @IndexedEmbedded
    private List<Detail> details;

    public List<Detail> getDetails() {
        return details;
    }

    public void setDetails(List<Detail> details) {
        this.details = details;
    }
}


@Indexed
class Detail {

    @Field
    private int number;

    private String text;

    public Detail(int number, String text) {
        this.number = number;
        this.text = text;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

我的代码返回 预期:是&lt; 5&gt;      但是:是&lt; 0&gt;

我尝试使用POJO作为容器(具有属性List和get | set方法)。 在这种情况下,我收到包含所有细节的容器,没有任何选择。

帮助,请!

1 个答案:

答案 0 :(得分:1)

您的问题在

cache.put("q", details);

您正在添加详细信息列表作为值。稍后您使用的查询会将详细信息作为值(.from(Details.class))进行搜索。也许你打算存储

container.setDetails(details);
cache.put("q", container);

然后查询将以qf.from(Container.class)开头,选择嵌入的对象。但请注意,这不能很好地扩展; Infinispan不会分解对象,它会将其存储为密钥q下的一个大块。