Query not working with Infinispan Cache
fzipper Jan 4, 2013 7:59 PMI've been trying out the query capabilities of Inifinispan and was able to get the sample from the documention working but I seem to be having problems getting my own test program to work. I have a ControlRequest class that is indexed by a SiteAddress field, which uses a StringBridge to convert it's value to a string. However, the test code below doesn't seem to work. I'm expecting it to return only 3 entries to me by it is returning all 5 in the cache.
Could some kind soul enlighten me as to what I'm doing wrong?
Thanks!
Footnote: If I change the address2 to NuidSiteAddress(1234, 18) , the query to work???
public class Test {
private static final SiteAddress address1 = new IpSiteAddress("1.2.3.4", 17); // toString="1.2.3.4/17"
private static final SiteAddress address2 = new NuidSiteAddress(1234, 17); // toString="1234/17"
public static void main(String[] args) {
org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout("%m%n")));
org.apache.log4j.Logger.getRootLogger().setLevel(Level.TRACE);
SearchMapping mapping = new SearchMapping();
mapping.entity(ControlRequest.class).indexed().providedId();
Properties properties = new Properties();
properties.put(org.hibernate.search.Environment.MODEL_MAPPING, mapping);
Configuration infinispanConfiguration = new ConfigurationBuilder()
.indexing()
.enable()
.indexLocalOnly(true)
.withProperties(properties)
.build();
Cache<Long, ControlRequest> cache = new DefaultCacheManager(infinispanConfiguration).getCache();
cache.put(1L, new ControlRequest(address1));
cache.put(2L, new ControlRequest(address1));
cache.put(3L, new ControlRequest(address1));
cache.put(4L, new ControlRequest(address2));
cache.put(5L, new ControlRequest(address2));
SearchManager searchManager = org.infinispan.query.Search.getSearchManager(cache);
QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(ControlRequest.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder
.keyword()
.onField("siteAddress")
.matching(address1) // Also tried this with "address1.toString()"
.createQuery();
CacheQuery query = searchManager.getQuery(luceneQuery, ControlRequest.class);
List<Object> objectList = query.list();
Assert.assertEquals(3, objectList.size());
}
}
public class ControlRequest {
@Field
@FieldBridge(impl = SiteAddressFieldBridge.class)
private SiteAddress siteAddress;
public ControlRequest(SiteAddress siteAddress) {
this.siteAddress = siteAddress;
}
public SiteAddress getSiteAddress() {
return siteAddress;
}
}
public class SiteAddressFieldBridge implements StringBridge {
@Override
public String objectToString(Object object) {
return object.toString();
}
}