I am having problems with the CachingWrapperFilter. As I understand it HibernateSearch should cache them automatically. A CachingWrapperFilter reapplied on a new query should not have to consult the bits method of the Filter it wraps... right?
Here is my small test showing that either my assumption above is wrong or the functionality broken:
Test program:
Code:
/**
* SearchTester is a playground for exploring Hibernate Search and Lucene.
*/
public class SearchTester {
private void start(FullTextEntityManager fullTextEntityManager) throws ParseException {
System.out.println("QUERY 1:");
performQuery(fullTextEntityManager);
System.out.println("QUERY 2:");
performQuery(fullTextEntityManager);
}
private void performQuery(FullTextEntityManager fullTextEntityManager) {
TermQuery p = new TermQuery(new Term("name", "foo"));
FullTextQuery query = fullTextEntityManager.createFullTextQuery(p, Company.class);
query.enableFullTextFilter("geoFilter")
.setParameter("swLat", 51.0345)
.setParameter("swLng", 12.3093)
.setParameter("neLat", 85.2345)
.setParameter("neLng", 29.3093);
query.getResultList();
}
private static FullTextEntityManager fullTextEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("standalone");
return Search.createFullTextEntityManager(emf.createEntityManager());
}
public static void main(String[] args) throws ParseException {
new SearchTester().start(fullTextEntityManager());
}
}
Actual Filter:Code:
public class GeoFilter {
private final static PaddedDoubleBridge PADDED_DOUBLE_BRIDGE = new PaddedDoubleBridge();
private double swLat;
private double swLng;
private double neLat;
private double neLng;
public void setSwLat(double swLat) {
this.swLat = swLat;
}
public void setSwLng(double swLng) {
this.swLng = swLng;
}
public void setNeLat(double neLat) {
this.neLat = neLat;
}
public void setNeLng(double neLng) {
this.neLng = neLng;
}
@Key
public FilterKey getKey() {
StandardFilterKey key = new StandardFilterKey();
key.addParameter(swLat);
key.addParameter(swLng);
key.addParameter(neLat);
key.addParameter(neLng);
return key;
}
@Factory
public Filter getFilter() {
// Add some printouts before the bits method is called
ChainedFilter filter = new ChainedFilter() {
public BitSet bits(IndexReader reader) throws IOException {
System.out.println("ChainedFilter.bits (reader=" + reader + ")");
return super.bits(reader);
}
};
filter.addFilter(new RangeFilter("address.position.lat", pad(swLat), pad(neLat), false, false));
filter.addFilter(new RangeFilter("address.position.lng", pad(swLng), pad(neLng), false, false));
// Add some printouts before the bits method is called
return new CachingWrapperFilter(filter) {
public BitSet bits(IndexReader reader) throws IOException {
System.out.println("CachingWrapperFilter.bits (reader=" + reader + ")");
return super.bits(reader);
}
};
}
private String pad(double d) {
return PADDED_DOUBLE_BRIDGE.objectToString(d);
}
}
Printout:Code:
QUERY 1:
CachingWrapperFilter.bits (reader=org.apache.lucene.index.MultiReader@8a52b6)
ChainedFilter.bits (reader=org.apache.lucene.index.MultiReader@8a52b6)
QUERY 2:
CachingWrapperFilter.bits (reader=org.apache.lucene.index.MultiReader@17b2b2)
ChainedFilter.bits (reader=org.apache.lucene.index.MultiReader@17b2b2)
Expected printout:Code:
QUERY 1:
CachingWrapperFilter.bits (reader=org.apache.lucene.index.MultiReader@8a52b6)
ChainedFilter.bits (reader=org.apache.lucene.index.MultiReader@8a52b6)
QUERY 2:
CachingWrapperFilter.bits (reader=org.apache.lucene.index.MultiReader@17b2b2)
I suspect that the fact that I am getting different readers could be part of the problem since the reader might be part of the key to make a lookup in the cache. Why do I get that? Or is the error elsewhere?
Thankful for any feedback on this!
Tobias Hill