protected void assertDocsIndexed(long millis) { int numEntries = getNumEntries(); this.eventually(() -> { CacheQuery<Object[]> query = Search.getSearchManager(pickCache()).getQuery(new MatchAllDocsQuery()).projection("val"); Set<Integer> indexedDocsIds = query.list().stream().map(projections -> (Integer) projections[0]).collect(Collectors.toSet()); int resultSize = indexedDocsIds.size(); return resultSize == numEntries; }, millis); }
private CacheQuery<?> createProjectionQuery(String... projection) { QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(Foo.class).get(); Query query = queryBuilder.keyword().onField("bar").matching("bar1").createQuery(); return searchManager.getQuery(query).projection(projection); }
cacheQuery = cacheQuery.projection(luceneParsingResult.getProjections());
@Test public void testQueryReturnsNullWhenProjectingCacheValue() throws Exception { withTx(tm(), (Callable<Void>) () -> { cache.remove("1"); // cache will now be out of sync with the index Query query = createQueryBuilder().keyword().onField("bar").matching("1").createQuery(); ResultIterator<Object[]> iterator = searchManager.getQuery(query).projection(ProjectionConstants.VALUE, "bar").iterator(new FetchOptions().fetchMode(LAZY)); assertTrue(iterator.hasNext()); Object[] projection = iterator.next(); assertNull(projection[0]); assertEquals("1", projection[1]); return null; }); }
cacheQuery = cacheQuery.projection(luceneParsingResult.getProjections());
/** * Build a Lucene index query. */ protected <E> CacheQuery<E> buildLuceneQuery(IckleParsingResult<TypeMetadata> ickleParsingResult, Map<String, Object> namedParameters, long startOffset, int maxResults) { if (log.isDebugEnabled()) { log.debugf("Building Lucene query for : %s", ickleParsingResult.getQueryString()); } if (!isIndexed) { throw log.cannotRunLuceneQueriesIfNotIndexed(cache.getName()); } LuceneQueryParsingResult luceneParsingResult = transformParsingResult(ickleParsingResult, namedParameters); org.apache.lucene.search.Query luceneQuery = makeTypeQuery(luceneParsingResult.getQuery(), luceneParsingResult.getTargetEntityName()); if (log.isDebugEnabled()) { log.debugf("The resulting Lucene query is : %s", luceneQuery.toString()); } CacheQuery<?> cacheQuery = makeCacheQuery(ickleParsingResult, luceneQuery); if (luceneParsingResult.getSort() != null) { cacheQuery = cacheQuery.sort(luceneParsingResult.getSort()); } if (luceneParsingResult.getProjections() != null) { cacheQuery = cacheQuery.projection(luceneParsingResult.getProjections()); } if (startOffset >= 0) { cacheQuery = cacheQuery.firstResult((int) startOffset); } if (maxResults > 0) { cacheQuery = cacheQuery.maxResults(maxResults); } return (CacheQuery<E>) cacheQuery; }
public void testQueryReturnsNullWhenProjectingCacheValue() throws Exception { prepareData(); withTx(tm(0), (Callable<Void>) () -> { Cache<String, Foo> cache = getKeyLocation("1"); cache.remove("1"); // cache will now be out of sync with the index searchManager = Search.getSearchManager(cache); Query query = createQueryBuilder().keyword().onField("bar").matching("1").createQuery(); ResultIterator<Object[]> iterator = searchManager.getQuery(query).projection(ProjectionConstants.VALUE, "bar") .iterator(new FetchOptions().fetchMode(LAZY)); assertTrue("Expected an element in iterator.", iterator.hasNext()); Object[] projection = iterator.next(); assertNull(projection[0]); assertEquals("1", projection[1]); return null; }); }