Caching 将对象列表放入Infinispan缓存并按它们进行搜索
我将对象列表放入缓存,需要按其属性值进行选择,如下所示:Caching 将对象列表放入Infinispan缓存并按它们进行搜索,caching,search,infinispan,Caching,Search,Infinispan,我将对象列表放入缓存,需要按其属性值进行选择,如下所示: Cache<Object, Object> cache = cacheManager.getCache(searchResultName); assertThat(cache, is(notNullValue())); Container container = new Container(); List<Detail> details = Lists.newArray
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;
}
}
Cache Cache=cacheManager.getCache(searchResultName);
断言(缓存,是(notNullValue());
容器=新容器();
List details=Lists.newArrayList();
详情。增加(新详情(2,“两”);
详情。增加(新详情(2,“两”);
详情。增加(新详情(3,“三”);
详情。增加(新详情(3,“三”);
详情。增加(新详情(3,“三”);
详情。增加(新详情(5,“五”);
详情。增加(新详情(5,“五”);
详情。增加(新详情(5,“五”);
详情。增加(新详情(5,“五”);
详情。增加(新详情(5,“五”);
cache.put(“q”,细节);
QueryFactory QueryFactory=org.infinispan.query.Search.getQueryFactory(缓存);
org.infinispan.query.dsl.QueryBuilder QueryBuilder=queryFactory.from(Detail.class)
.具有(数字)式(5);
details=queryBuilder.build().list();
断言(details.size(),为(5));
@索引
类容器{
@指数化
私人名单详情;
公共列表getDetails(){
退货详情;
}
公共无效设置详细信息(列表详细信息){
this.details=详细信息;
}
}
@索引
课堂细节{
@场
私有整数;
私有字符串文本;
公共详细信息(整数、字符串文本){
这个数字=数字;
this.text=文本;
}
public int getNumber(){
返回号码;
}
公共无效集合号(整数){
这个数字=数字;
}
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
}
我的代码返回
预期:是吗
但是:是吗
我尝试使用POJO作为容器(使用属性列表和get | set方法)。
在这种情况下,我收到了包含所有详细信息的容器,没有任何选择
救命啊 您的问题出在
cache.put("q", details);
您正在添加一个详细信息列表作为值。稍后使用的查询将详细信息作为值进行搜索(.from(Details.class)
)。也许你打算去商店
container.setDetails(details);
cache.put("q", container);
然后查询将从
qf.from(Container.class)
开始,选择嵌入的对象。但是请注意,这并不能很好地扩展;Infinispan不会分解对象,它会将其作为一个大块存储在键q
下,我就是这样研究的。Infinispan返回一个包含所有详细信息的容器,不进行任何选择。您需要使用投影: