@Test(expected = ClassCastException.class) public void testPOJOqbeSearchWithSearchHandle() { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\":{" + "\"$and\":[{\"inventory\":{\"$gt\":1010}},{\"inventory\":{\"$le\":1110}}]" + ",\"$filtered\": true}}"; System.out.println(queryAsString); PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawQueryByExampleDefinition( new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("even"); SearchHandle results = new SearchHandle(); products.setPageLength(10); p = products.search(qd, 1, results); assertEquals("total no of pages", 5, p.getTotalPages()); System.out.println(p.getTotalPages()); // System.out.println(results.getMetrics().getQueryResolutionTime()); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo, results); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); assertTrue( "Enventory lies between 1010 to 1110", a.getInventory() > 1010 && a.getInventory() <= 1110); assertTrue("Artifact Id is even", a.getId() % 2 == 0); assertTrue("Company name contains Acme", a.getManufacturer().getName().contains("Acme")); count++; // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); } assertEquals("Page size", count, p.size()); pageNo = pageNo + p.getPageSize(); MatchDocumentSummary[] mds = results.getMatchResults(); assertEquals("Size of the results summary", 10, mds.length); for (MatchDocumentSummary md : mds) { assertTrue("every uri should contain the class name", md.getUri().contains("Artifact")); } String[] facetNames = results.getFacetNames(); for (String fname : facetNames) { System.out.println(fname); } // assertEquals("Total results from search handle ",50,results.getTotalResults()); // assertTrue("Search Handle metric results ",results.getMetrics().getTotalTime()>0); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertEquals("Page start check", 41, p.getStart()); assertEquals("page number after the loop", 5, p.getPageNumber()); assertEquals("total no of pages", 5, p.getTotalPages()); }
private PojoQueryDefinition wrapQuery(PojoQueryDefinition query) { if (query == null) { return qb.collection(entityClass.getName()); } else { List<String> collections = Arrays.asList(query.getCollections()); HashSet<String> collectionSet = new HashSet<String>(collections); collectionSet.add(entityClass.getName()); query.setCollections(collectionSet.toArray(new String[0])); return query; } }
@Test(expected = ClassCastException.class) public void testPOJOqbeSearchWithoutSearchHandle() { PojoRepository<Artifact, Long> products = client.newPojoRepository(Artifact.class, Long.class); PojoPage<Artifact> p; this.loadSimplePojos(products); QueryManager queryMgr = client.newQueryManager(); String queryAsString = "{\"$query\":{" + "\"$and\":[{\"name\":{\"$word\":\"cogs\",\"$exact\": false}}]" + ",\"$not\":[{\"name\":{\"$word\":\"special\",\"$exact\": false}}]" + "}}"; PojoQueryDefinition qd = (PojoQueryDefinition) queryMgr.newRawQueryByExampleDefinition( new StringHandle(queryAsString).withFormat(Format.JSON)); qd.setCollections("odd"); products.setPageLength(11); p = products.search(qd, 1); assertEquals("total no of pages", 4, p.getTotalPages()); // System.out.println(p.getTotalPages()); long pageNo = 1, count = 0; do { count = 0; p = products.search(qd, pageNo); while (p.iterator().hasNext()) { Artifact a = p.iterator().next(); validateArtifact(a); assertFalse("Verifying document with special is not there", a.getId() % 5 == 0); assertTrue("Artifact Id is odd", a.getId() % 2 != 0); assertTrue( "Company name contains widgets", a.getManufacturer().getName().contains("Widgets")); count++; // System.out.println(a.getId()+" "+a.getManufacturer().getName() +" "+count); } assertEquals("Page size", count, p.size()); pageNo = pageNo + p.getPageSize(); } while (!p.isLastPage() && pageNo < p.getTotalSize()); assertEquals("page number after the loop", 4, p.getPageNumber()); assertEquals("total no of pages", 4, p.getTotalPages()); }