public void testAdded() throws Exception { prepareTestData(); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("eats"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); List<Object> found = cacheQuery.list(); AssertJUnit.assertEquals(2, found.size()); assert found.contains(person2); assert found.contains(person3); assert !found.contains(person4) : "This should not contain object person4"; person4 = new Person(); person4.setName("Mighty Goat"); person4.setBlurb("Also eats grass"); cache1.put("mighty", person4); luceneQuery = queryParser.parse("eats"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); found = cacheQuery.list(); AssertJUnit.assertEquals(3, found.size()); assert found.contains(person2); assert found.contains(person3); assert found.contains(person4) : "This should now contain object person4"; }
public void testModified() throws Exception { prepareTestData(); assertQueryInterceptorPresent(cache2); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("playing"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); List<Object> found = cacheQuery.list(); assert found.size() == 1 : "Expected list of size 1, was of size " + found.size(); assert found.get(0).equals(person1); person1.setBlurb("Likes pizza"); cache1.put("Navin", person1); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("pizza"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); found = cacheQuery.list(); assert found.size() == 1; assert found.get(0).equals(person1); }
@SuppressWarnings("unchecked") private CapedwarfLogQueryResult fetchCapedwarfRequestLogs(CapedwarfLogQuery logQuery) { List<CapedwarfRequestLogs> list; int resultCount; List<String> requestIds = logQuery.getQuery().getRequestIds(); if (requestIds.isEmpty()) { CacheQuery cacheQuery = createRequestLogsQuery(logQuery); list = (List<CapedwarfRequestLogs>) (List) cacheQuery.list(); resultCount = cacheQuery.getResultSize(); } else { list = new ArrayList<>(requestIds.size()); for (String requestId : requestIds) { CapedwarfRequestLogs requestLogs = (CapedwarfRequestLogs) store.get(requestId); if (requestLogs != null) { list.add(requestLogs); } } resultCount = list.size(); } for (int i = 0; i < list.size(); i++) { CapedwarfRequestLogs capedwarfRequestLogs = list.get(i); list.set(i, capedwarfRequestLogs.clone()); } return new CapedwarfLogQueryResult(list, resultCount); }
public static List<Object> performSearch( Select command, Class<?> type, String cacheName, CacheContainerWrapper cache) throws TranslatorException { LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Using Lucene Searching."); // $NON-NLS-1$ // Map<?, ?> cache, SearchManager searchManager = Search.getSearchManager((Cache<?, ?>) cache.getCache(cacheName)); QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(type).get(); BooleanJunction<BooleanJunction> junction = queryBuilder.bool(); boolean createdQueries = buildQueryFromWhereClause(command.getWhere(), junction, queryBuilder); Query query = null; if (createdQueries) { query = junction.createQuery(); } else { query = queryBuilder.all().createQuery(); } CacheQuery cacheQuery = searchManager.getQuery(query, type); // rootNodeType List<Object> results = cacheQuery.list(); if (results == null || results.isEmpty()) { return Collections.emptyList(); } return results; }
public void testPrimitiveAndStringKeys() { String key1 = "key1"; int key2 = 2; byte key3 = (byte) 3; float key4 = (float) 4; long key5 = (long) 5; short key6 = (short) 6; boolean key7 = true; double key8 = (double) 8; char key9 = '9'; cache.put(key1, person1); cache.put(key2, person1); cache.put(key3, person1); cache.put(key4, person1); cache.put(key5, person1); cache.put(key6, person1); cache.put(key7, person1); cache.put(key8, person1); cache.put(key9, person1); // Going to search the 'blurb' field for 'owns' Term term = new Term("blurb", "owns"); CacheQuery cacheQuery = new QueryFactory(cache, qh).getQuery(new TermQuery(term)); assert cacheQuery.getResultSize() == 9; List<Object> found = cacheQuery.list(); for (int i = 0; i < 9; i++) { assert found.get(i).equals(person1); } }
public void testFaceting() throws Exception { QueryBuilder queryBuilder = qf.buildQueryBuilderForClass(Car.class).get(); FacetingRequest request = queryBuilder .facet() .name(facetName) .onField(indexFieldName) .discrete() .createFacetingRequest(); Query luceneQuery = queryBuilder.all().createQuery(); CacheQuery query = qf.getQuery(luceneQuery); query.getFacetManager().enableFaceting(request); List<Facet> facetList = query.getFacetManager().getFacets(facetName); assertEquals("Wrong number of facets", 2, facetList.size()); assertEquals("4000", facetList.get(0).getValue()); assertEquals(2, facetList.get(0).getCount()); assertEquals(indexFieldName, facetList.get(0).getFieldName()); assertEquals("2341", facetList.get(1).getValue()); assertEquals(1, facetList.get(1).getCount()); assertEquals(indexFieldName, facetList.get(1).getFieldName()); }
private List searchByName(String name, Cache c) { SearchManager sm = Search.getSearchManager(c); CacheQuery q = sm.getQuery(SEntity.searchByName(name), SEntity.class); int resultSize = q.getResultSize(); List l = q.list(); assert l.size() == resultSize; return q.list(); }
@SuppressWarnings("unchecked") private void fetchAppLogLines(RequestLogs requestLogs, CapedwarfLogQuery logQuery) { CacheQuery query = createAppLogLinesQuery(requestLogs); // FetchOptions fetchOptions = createAppLogFetchOptions(logQuery); List<CapedwarfAppLogLine> capedwarfAppLogLines = (List<CapedwarfAppLogLine>) (List) query.list(); for (CapedwarfAppLogLine capedwarfAppLogLine : capedwarfAppLogLines) { requestLogs.getAppLogLines().add(capedwarfAppLogLine.getAppLogLine()); } }
public void simpleIndexTest() throws ParseException { cache.put("1", new Person("A Person's Name", "A paragraph containing some text", 75)); CacheQuery cq = TestQueryHelperFactory.createCacheQuery(cache, "name", "Person"); assertEquals(1, cq.getResultSize()); List<Object> l = cq.list(); assertEquals(1, l.size()); Person p = (Person) l.get(0); assertEquals("A Person's Name", p.getName()); assertEquals("A paragraph containing some text", p.getBlurb()); assertEquals(75, p.getAge()); }
public void testOverwriteNotIndexedValue() { final long id = 10; cache.put(id, new NotIndexedType("name1")); Map<Object, Object> map = new HashMap<Object, Object>(); map.put(id, new TestEntity("name2", "surname2", id, "note")); cache.putAll(map); CacheQuery q1 = queryByNameField("name2", AnotherTestEntity.class); assertEquals(1, q1.getResultSize()); assertEquals(TestEntity.class, q1.list().get(0).getClass()); }
private CacheQuery createAppLogLinesQuery(RequestLogs requestLogs) { QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(CapedwarfAppLogLine.class).get(); Query query = queryBuilder .keyword() .onField(CapedwarfAppLogLine.REQUEST_ID) .matching(requestLogs.getRequestId()) .createQuery(); CacheQuery cacheQuery = searchManager.getQuery(query, CapedwarfAppLogLine.class); cacheQuery.sort(new Sort(new SortField(CapedwarfAppLogLine.SEQUENCE_NUMBER, SortField.LONG))); return cacheQuery; }
private CacheQuery createRequestLogsQuery(CapedwarfLogQuery logQuery) { LogQuery query = logQuery.getQuery(); QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(CapedwarfRequestLogs.class).get(); List<Query> queries = new ArrayList<Query>(); if (query.getStartTimeUsec() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.END_TIME_USEC) .above(query.getStartTimeUsec()) .createQuery()); } if (query.getEndTimeUsec() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.END_TIME_USEC) .below(query.getEndTimeUsec()) .createQuery()); } if (query.getMinLogLevel() != null) { queries.add( queryBuilder .range() .onField(CapedwarfRequestLogs.MAX_LOG_LEVEL) .above(query.getMinLogLevel().ordinal()) .createQuery()); } boolean onlyCompleteRequests = !Boolean.TRUE.equals(query.getIncludeIncomplete()); if (onlyCompleteRequests) { queries.add( queryBuilder .keyword() .onField(CapedwarfRequestLogs.FINISHED) .matching(Boolean.TRUE) .createQuery()); } Query luceneQuery = getQuery(queryBuilder, queries); CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, CapedwarfRequestLogs.class); cacheQuery.sort( new Sort(new SortField(CapedwarfRequestLogs.END_TIME_USEC, SortField.LONG, true))); cacheQuery.firstResult(logQuery.getOptions().getOffset()); if (logQuery.getOptions().getLimit() != null) { cacheQuery.maxResults(logQuery.getOptions().getLimit()); } return cacheQuery; }
public void testCustomKeys() { CustomKey key1 = new CustomKey(1, 2, 3); CustomKey key2 = new CustomKey(900, 800, 700); CustomKey key3 = new CustomKey(1024, 2048, 4096); cache.put(key1, person1); cache.put(key2, person1); cache.put(key3, person1); Term term = new Term("blurb", "owns"); CacheQuery cacheQuery = new QueryFactory(cache, qh).getQuery(new TermQuery(term)); int i; assert (i = cacheQuery.getResultSize()) == 3 : "Expected 3, was " + i; }
private void queryTest() { cache.put("jekyll", new TestEntity("Henry", "Jekyll", 1, "dissociate identity disorder")); cache.put("hyde", new TestEntity("Edward", "Hyde", 2, "dissociate identity disorder")); SearchManager sm = Search.getSearchManager(cache); Query query = sm.buildQueryBuilderForClass(TestEntity.class) .get() .keyword() .onField("name") .matching("Henry") .createQuery(); CacheQuery q = sm.getQuery(query); assertEquals(1, q.getResultSize()); assertEquals(TestEntity.class, q.list().get(0).getClass()); }
public void testRemoved() throws Exception { prepareTestData(); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("eats"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); List<Object> found = cacheQuery.list(); assert found.size() == 2; assert found.contains(person2); assert found.contains(person3) : "This should still contain object person3"; cache1.remove(key3); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("eats"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); found = cacheQuery.list(); }
public void testGetResultSize() throws Exception { prepareTestData(); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("playing"); cacheQuery = Search.getSearchManager(cache2).getQuery(luceneQuery); List<Object> found = cacheQuery.list(); AssertJUnit.assertEquals(1, found.size()); }
public void testSearchKeyTransformer() throws Exception { Cache cache1 = caches().get(0); Cache cache2 = caches().get(1); SearchManager manager = Search.getSearchManager(cache1); SearchManager manager1 = Search.getSearchManager(cache2); manager.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class); manager1.registerKeyTransformer(CustomKey3.class, CustomKey3Transformer.class); prepareTestedObjects(); TransactionManager transactionManager = cache1.getAdvancedCache().getTransactionManager(); CustomKey3 customeKey1 = new CustomKey3(key1); CustomKey3 customeKey2 = new CustomKey3(key2); CustomKey3 customeKey3 = new CustomKey3(key3); // Put the 3 created objects in the cache1. if (transactionsEnabled()) transactionManager.begin(); cache1.put(customeKey1, person1); cache1.put(customeKey2, person2); cache2.put(customeKey3, person3); if (transactionsEnabled()) transactionManager.commit(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("Eats"); CacheQuery cacheQuery = manager.getQuery(luceneQuery); ResultIterator found = cacheQuery.iterator(new FetchOptions().fetchMode(FetchOptions.FetchMode.LAZY)); int counter = 0; while (found.hasNext()) { found.next(); counter++; } AssertJUnit.assertEquals(2, counter); }
@Override public List<Comment> getCommentsBySongId(String songId) { if (songId == null) { throw new IllegalArgumentException("Song id is null."); } ArrayList<Comment> comments = new ArrayList<Comment>(); commentCache = provider.getCacheContainer().getCache("commentcache"); SearchManager sm = Search.getSearchManager((Cache<String, Object>) commentCache); QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Comment.class).get(); Query q = queryBuilder.keyword().onField("songId").matching(songId).createQuery(); logger.debug("Lucene query: " + q); CacheQuery cq = sm.getQuery(q, Comment.class); for (Object o : cq.list()) { if (o instanceof Comment) { comments.add(((Comment) o)); } } return comments; }
public void testClear() throws Exception { prepareTestData(); queryParser = createQueryParser("blurb"); luceneQuery = queryParser.parse("eats"); cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery); Query[] queries = new Query[2]; queries[0] = luceneQuery; luceneQuery = queryParser.parse("playing"); queries[1] = luceneQuery; Query luceneQuery = queries[0].combine(queries); CacheQuery cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery); AssertJUnit.assertEquals(3, cacheQuery.getResultSize()); cache2.clear(); AssertJUnit.assertEquals(3, cacheQuery.getResultSize()); cacheQuery = Search.getSearchManager(cache1).getQuery(luceneQuery); AssertJUnit.assertEquals(0, cacheQuery.getResultSize()); }
public void testSimple() throws Exception { prepareTestData(); cacheQuery = createCacheQuery(cache2, "blurb", "playing"); List<Object> found = cacheQuery.list(); assert found.size() == 1; if (found.get(0) == null) { log.warn("found.get(0) is null"); Person p1 = cache2.get(key1); if (p1 == null) { log.warn("Person p1 is null in sc2 and cannot actually see the data of person1 in sc1"); } else { log.trace("p1 name is " + p1.getName()); } } assert found.get(0).equals(person1); }
public void testFullTextFilterOnOff() throws Exception { prepareTestData(); queryParser = createQueryParser("blurb"); Query luceneQuery = queryParser.parse("eats"); CacheQuery query = Search.getSearchManager(cache1).getQuery(luceneQuery); FullTextFilter filter = query.enableFullTextFilter("personFilter"); filter.setParameter("blurbText", "cheese"); AssertJUnit.assertEquals(1, query.getResultSize()); List result = query.list(); Person person = (Person) result.get(0); AssertJUnit.assertEquals("MiniGoat", person.getName()); AssertJUnit.assertEquals("Eats cheese", person.getBlurb()); // Disabling the fullTextFilter. query.disableFullTextFilter("personFilter"); AssertJUnit.assertEquals(2, query.getResultSize()); }
@Test public void testIndexAndQuery() throws Exception { SearchManager searchManager = Search.getSearchManager(cache); cache.put(123405, new Bond(new Date(System.currentTimeMillis()), 450L)); assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Bond.class); cache.put(123502, new Debenture("GB", 116d)); assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Debenture.class); cache.put(223456, new Bond(new Date(System.currentTimeMillis()), 550L)); assertEfficientIndexingUsed(searchManager.unwrap(SearchIntegrator.class), Bond.class); CacheQuery query = searchManager.getQuery(new MatchAllDocsQuery(), Bond.class, Debenture.class); assertEquals(query.list().size(), 3); CacheQuery queryBond = searchManager.getQuery(new MatchAllDocsQuery(), Bond.class); assertEquals(queryBond.getResultSize(), 2); CacheQuery queryDeb = searchManager.getQuery(new MatchAllDocsQuery(), Debenture.class); assertEquals(queryDeb.getResultSize(), 1); }
/** Execute Lucene index query. */ private QueryResponse executeQuery( AdvancedCache<byte[], byte[]> cache, SerializationContext serCtx, QueryRequest request) { final SearchManager searchManager = Search.getSearchManager(cache); final SearchIntegrator searchFactory = 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()); } int projSize = 0; if (parsingResult.getProjections() != null && !parsingResult.getProjections().isEmpty()) { projSize = parsingResult.getProjections().size(); cacheQuery = cacheQuery.projection(parsingResult.getProjections().toArray(new String[projSize])); } if (request.getStartOffset() > 0) { cacheQuery = cacheQuery.firstResult((int) request.getStartOffset()); } if (request.getMaxResults() > 0) { cacheQuery = cacheQuery.maxResults(request.getMaxResults()); } List<?> list = cacheQuery.list(); List<WrappedMessage> results = new ArrayList<WrappedMessage>(projSize == 0 ? list.size() : list.size() * projSize); for (Object o : list) { if (projSize == 0) { results.add(new WrappedMessage(o)); } else { Object[] row = (Object[]) o; for (int j = 0; j < projSize; j++) { results.add(new WrappedMessage(row[j])); } } } QueryResponse response = new QueryResponse(); response.setTotalResults(cacheQuery.getResultSize()); response.setNumResults(list.size()); response.setProjectionSize(projSize); response.setResults(results); return response; }
protected int countIndex() { SearchManager searchManager = Search.getSearchManager(cache1); CacheQuery q = searchManager.getQuery(new MatchAllDocsQuery(), Transaction.class); return q.getResultSize(); }