Infinispan Query is not working
thospfuller Aug 25, 2010 6:36 AMHi,
I have integrated the query module with an application I have and it's not working -- the exact problem as well as my setup are described here. Any thoughts on what I'm missing would be very helpful.
The following code is in Groovy and running on the Grails platform.
Thanks,
Tom
----- Problem -----
I think the problem is in the agent. If I set the @Indexed(index="name") I get the following result:
When I start the application 46 agents are loaded into the cache with guns and 55 with fists only. The query returns 49 hits, which is incorrect, then a null pointer exception is thrown on def found = results.list().
infinispantestapp.QueryAgentsController hits: 49
errors.GrailsExceptionResolver null
java.lang.NullPointerException
at org.infinispan.query.impl.CacheQueryImpl.list(CacheQueryImpl.java:310
)
at infinispantestapp.QueryAgentsController$_closure1.doCall(QueryAgentsC
ontroller.groovy:39)
at infinispantestapp.QueryAgentsController$_closure1.doCall(QueryAgentsC
ontroller.groovy)
at java.lang.Thread.run(Thread.java:619)
Changing the index to "weapon" or leaving it blank results in zero hits being returned.
Keep in mind that searching by using a Lucene query seems to give me the same results:
def term = new Term ("weapon", "gun")
def termQuery = new TermQuery (term)
def results = agentCache.query (Agent.class, termQuery)
----- Setup -----
This is my configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<infinispan>
<default>
<indexing enabled="true" indexLocalOnly="false" />
</default>
</infinispan>
I have query capabilities mixed-in directly on the cache, however the precondition is that the developer sets the queryHelper before any object are loaded into the cache -- this is done in the BootStrap.groovy file as follows:
agentCache.queryHelper = new QueryHelper (agentCache, new Properties (), Agent.class)
The next step is to load the cache with Agents (from the movie The Matrix) -- this is also done in the BootStrap.groovy file as follows:
def gunCtr = 0, fistCtr = 0
for (ctr in 0..100) {
String key = "agent $ctr"
String name = key
def weapon = rand.nextBoolean () ? "gun" : "fist"
if ("gun".equals (weapon)) gunCtr++ else fistCtr++
def agent = new Agent (name:name, weapon:weapon)
agentCache.put (key, agent)
}
Note that I keep track of the number of agents loaded that have guns or fists only, so I know the number available in the cache.
The agent looks like this:
import org.hibernate.search.annotations.ProvidedId
import org.hibernate.search.annotations.Indexed
@ProvidedId
@Indexed(index="name")
class Agent {
String name
String weapon
String toString () {
return "name: $name, weapon: $weapon"
}
}
I perform the query as follows:
def index = {
def results = agentCache.query (Agent.class, "weapon", "gun")
println "results: $results"
def hits = results.getResultSize()
log.info ("hits: $hits")
def found = results.list()
def eagerIterator = results.iterator();
eagerIterator.each {
log.info ("agent: $it")
}
}
Finally, my mixin looks like this:
class QueryCapabilities {
private static final def log = Logger.getInstance(QueryCapabilities.class)
def queryHelper = null
def query (Class clazz, String field, String search) {
log.info ("query: method begins; queryHelper: $queryHelper, clazz: $clazz, field: $field, search: $search")
def queryFactory = new QueryFactory((owner as Cache), queryHelper)
def cacheQuery = queryFactory.getBasicQuery (field, search)
log.info ("query: method ends; cacheQuery: $cacheQuery")
return cacheQuery
}
def query (Class clazz, Query query) {
log.info ("query: method begins; queryHelper: $queryHelper, clazz: $clazz, query: $query")
def queryFactory = new QueryFactory((owner as Cache), queryHelper)
def cacheQuery = queryFactory.getQuery (query)
log.info ("query: method ends; cacheQuery: $cacheQuery")
return cacheQuery
}
}
The following jars are included in my project:
Infinispan 4.1.0.CR3 (Radegast)
* infinispan-core.jar
* jboss-common-core-2.2.14.GA.jar
* jgroups-2.10.0.GA.jar
* marshalling-api-1.2.2.GA.jar
* river-1.2.2.GA.jar
Infinispan Query Module
* hibernate-commons-annotations-3.2.0.Final.jar
* hibernate-search-3.2.1.Final.jar
* infinispan-query.jar
* lucene-analyzers-2.9.3.jar
* lucene-core-2.9.3.jar
Hibernate Infinispan version 3.5.0-Final
* hibernate-infinispan-3.5.0-Final.jar