if(!(Boolean) server.getAttribute(name, "StatisticsEnabled")) {
server.setAttribute(name, new Attribute("StatisticsEnabled", true));
}
Cache cache = cacheManager.getCache(CACHE_NAME);
// check that our settings are not ignored
SearchManager searchManager = Search.getSearchManager(cache);
assertTrue(searchManager.getSearchFactory().getStatistics().isStatisticsEnabled());
// add some test data
for(int i = 0; i < numberOfEntries; i++) {
Person person = new Person();
person.setName("key" + i);
person.setAge(i);
person.setBlurb("value " + i);
person.setNonSearchableField("i: " + i);
cache.put("key" + i, person);
}
// after adding more classes and reconfiguring the SearchFactory it might happen isStatisticsEnabled is reset, so we check again
assertTrue(searchManager.getSearchFactory().getStatistics().isStatisticsEnabled());
assertEquals(0L, server.getAttribute(name, "SearchQueryExecutionCount"));
QueryParser queryParser = createQueryParser("blurb");
Query luceneQuery = queryParser.parse("value");
CacheQuery cacheQuery = searchManager.getQuery(luceneQuery);
List<Object> found = cacheQuery.list();
assertEquals(1L, server.getAttribute(name, "SearchQueryExecutionCount"));
assertEquals(numberOfEntries, found.size());
assertEquals(numberOfEntries, server.invoke(name, "getNumberOfIndexedEntities",
new Object[]{Person.class.getCanonicalName()},
new String[]{String.class.getCanonicalName()}));
assertEquals(1, searchManager.getSearchFactory().getStatistics().indexedEntitiesCount().size());
// add more test data
AnotherGrassEater anotherGrassEater = new AnotherGrassEater("Another grass-eater", "Eats grass");
cache.put("key101", anotherGrassEater);
cacheQuery = searchManager.getQuery(luceneQuery);
found = cacheQuery.list();
assertEquals(numberOfEntries, found.size());