/**
* Execute Lucene index query.
*/
private QueryResponse executeQuery(AdvancedCache<byte[], byte[]> cache, SerializationContext serCtx, QueryRequest request) {
final SearchManager searchManager = Search.getSearchManager(cache);
final SearchFactoryIntegrator searchFactory = (SearchFactoryIntegrator) searchManager.getSearchFactory();
final QueryCache queryCache = ComponentRegistryUtils.getQueryCache(cache); // optional component
LuceneQueryParsingResult parsingResult;
Query luceneQuery;
if (queryCache != null) {
KeyValuePair<String, Class> queryCacheKey = new KeyValuePair<String, Class>(request.getJpqlString(), LuceneQueryParsingResult.class);
parsingResult = queryCache.get(queryCacheKey);
if (parsingResult == null) {
parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
queryCache.put(queryCacheKey, parsingResult);
}
} else {
parsingResult = parseQuery(cache, serCtx, request.getJpqlString(), searchFactory);
}
luceneQuery = parsingResult.getQuery();
if (!cache.getCacheConfiguration().compatibility().enabled()) {
// restrict on entity type
QueryBuilder qb = searchFactory.buildQueryBuilder().forEntity(parsingResult.getTargetEntity()).get();
luceneQuery = qb.bool()
.must(qb.keyword().onField(TYPE_FIELD_NAME)
.ignoreFieldBridge()
.ignoreAnalyzer()
.matching(parsingResult.getTargetEntityName()).createQuery())
.must(luceneQuery)
.createQuery();
}
CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, parsingResult.getTargetEntity());
if (parsingResult.getSort() != null) {
cacheQuery = cacheQuery.sort(parsingResult.getSort());
}