List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.hibernate.search.test.performance.task.QueryBooksByBestRatingTask.java
License:LGPL
@Override protected void execute(FullTextSession fts) { Query q = fts.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get().all().createQuery(); fts.createFullTextQuery(q, Book.class) .setSort(new Sort(new SortField("rating", SortField.Type.FLOAT, true))).setMaxResults(100).list(); }
From source file:org.hibernate.search.test.performance.task.QueryBooksByNewestPublishedTask.java
License:LGPL
@Override protected void execute(FullTextSession fts) { Query q = fts.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get().all().createQuery(); fts.createFullTextQuery(q, Book.class) .setSort(new Sort(new SortField("publicationDate", SortField.Type.LONG, true))).setMaxResults(100) .list();//from ww w . ja v a2s .com }
From source file:org.hibernate.search.test.performance.task.QueryBooksByTotalSoldTask.java
License:LGPL
@Override protected void execute(FullTextSession fts) { Query q = fts.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get().all().createQuery(); fts.createFullTextQuery(q, Book.class) .setSort(new Sort(new SortField("totalSold", SortField.Type.LONG, true))).setMaxResults(100).list(); }
From source file:org.hibernate.search.test.query.fieldcache.CachedNumericIdTest.java
License:Open Source License
public void testLocationLoading() { Session session = openSession();/*from w ww . j av a 2s . c o m*/ Transaction tx = session.beginTransaction(); QueryBuilder queryBuilder = getSearchFactory().buildQueryBuilder().forEntity(Location.class).get(); Query query = queryBuilder.all().createQuery(); FieldSelectorLeakingReaderProvider.resetFieldSelector(); FullTextSession fullTextSession = Search.getFullTextSession(session); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Location.class); fullTextQuery.setSort(new Sort(new SortField("description", SortField.STRING))); // to avoid loading in document order -- too easy List<Location> locations = fullTextQuery.list(); FieldSelectorLeakingReaderProvider.assertFieldSelectorDisabled(); Assert.assertEquals(NUM_LOCATIONS, locations.size()); for (Location location : locations) { int id = location.getId(); Assert.assertEquals(String.valueOf(id) + "42", location.getDescription()); } tx.commit(); session.close(); }
From source file:org.hibernate.search.test.query.grouping.GroupingSortTest.java
License:LGPL
private void testSortGroupsByColor(boolean invert) throws Exception { final GroupingRequest request = queryBuilder(Car.class).group().onField(indexFieldName).topGroupCount(10) .groupSort(new Sort(new SortField(indexFieldName, Type.STRING, invert))).createGroupingRequest(); final FullTextQuery query = queryHondaWithGrouping(request); final GroupingResult groups = query.getGroupingManager().getGroupingResult(); // check groups sorted by color Group lastGroup = null;//w ww . j a va2s . c o m for (Group nextGroup : groups.getGroups()) { if (lastGroup != null && !invert) { assertTrue(lastGroup.getValue().compareTo(nextGroup.getValue()) < 0); } else if (lastGroup != null && invert) { assertTrue(lastGroup.getValue().compareTo(nextGroup.getValue()) > 0); } lastGroup = nextGroup; } }
From source file:org.hibernate.search.test.query.grouping.GroupingSortTest.java
License:LGPL
public void testSortEntitiesInGroupByCubicCapacity(boolean invert) throws Exception { final GroupingRequest request = queryBuilder(Car.class).group().onField(indexFieldName).topGroupCount(10) .maxDocsPerGroup(10).withinGroupSort(new Sort(new SortField("cubicCapacity", Type.INT, invert))) .createGroupingRequest();//from ww w .j av a2 s.co m final FullTextQuery query = queryHondaWithGrouping(request); final GroupingResult groups = query.getGroupingManager().getGroupingResult(); // check entities in groups sorted by cubic capacity for (Group nextGroup : groups.getGroups()) { final Session session = this.getSession(); Car lastCar = null; for (EntityInfo nextEntityInfo : nextGroup.getHits()) { final Car car = (Car) session.load(nextEntityInfo.getClazz(), nextEntityInfo.getId()); if (lastCar != null && !invert) { assertTrue(lastCar.getCubicCapacity() < car.getCubicCapacity()); } else if (lastCar != null && invert) { assertTrue(lastCar.getCubicCapacity() > car.getCubicCapacity()); } lastCar = car; } } }
From source file:org.hibernate.search.test.query.initandlookup.CriteriaObjectInitializerAndHierarchyInheritanceTest.java
License:LGPL
private List<?> getResultsFiltered(FullTextSession session, Query query, Class<? extends BaseEntity>... classes) { return session.createFullTextQuery(query, classes) .setSort(new Sort(new SortField("idSort", SortField.Type.INT))).list(); }
From source file:org.hibernate.search.test.query.LuceneQuerySortTest.java
License:Open Source License
/** * Test that we can change the default sort order of the lucene search result. * * @throws Exception in case the test fails. *///from w w w. j a va2 s. c om public void testList() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); createTestBooks(s); Transaction tx = s.beginTransaction(); QueryParser parser = new QueryParser(getTargetLuceneVersion(), "title", SearchTestCase.stopAnalyzer); Query query = parser.parse("summary:lucene"); FullTextQuery hibQuery = s.createFullTextQuery(query, Book.class); List<Book> result = hibQuery.list(); assertNotNull(result); assertEquals("Wrong number of test results.", 3, result.size()); // make sure that the order is according to in which order the books got inserted // into the index. int id = 1; for (Book b : result) { assertEquals("Expected another id", Integer.valueOf(id), b.getId()); id++; } // now the same query, but with a lucene sort specified. query = parser.parse("summary:lucene"); hibQuery = s.createFullTextQuery(query, Book.class); Sort sort = new Sort(new SortField("id", SortField.STRING, true)); hibQuery.setSort(sort); result = hibQuery.list(); assertNotNull(result); assertEquals("Wrong number of test results.", 3, result.size()); id = 3; for (Book b : result) { assertEquals("Expected another id", Integer.valueOf(id), b.getId()); id--; } // order by summary query = parser.parse("summary:lucene OR summary:action"); hibQuery = s.createFullTextQuery(query, Book.class); sort = new Sort(new SortField("summary_forSort", SortField.STRING)); //ASC hibQuery.setSort(sort); result = hibQuery.list(); assertNotNull(result); assertEquals("Wrong number of test results.", 4, result.size()); assertEquals("Groovy in Action", result.get(0).getSummary()); // order by summary backwards query = parser.parse("summary:lucene OR summary:action"); hibQuery = s.createFullTextQuery(query, Book.class); sort = new Sort(new SortField("summary_forSort", SortField.STRING, true)); //DESC hibQuery.setSort(sort); result = hibQuery.list(); assertNotNull(result); assertEquals("Wrong number of test results.", 4, result.size()); assertEquals("Hibernate & Lucene", result.get(0).getSummary()); // order by date backwards query = parser.parse("summary:lucene OR summary:action"); hibQuery = s.createFullTextQuery(query, Book.class); sort = new Sort(new SortField("publicationDate", SortField.STRING, true)); //DESC hibQuery.setSort(sort); result = hibQuery.list(); assertNotNull(result); assertEquals("Wrong number of test results.", 4, result.size()); for (Book book : result) { System.out.println(book.getSummary() + " : " + book.getPublicationDate()); } assertEquals("Groovy in Action", result.get(0).getSummary()); tx.commit(); deleteTestBooks(s); s.close(); }
From source file:org.hibernate.search.test.query.LuceneQueryTest.java
License:Open Source License
public void testFetchSizeDefaultFirstAndMax() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);/*from w ww . j a va 2s .c o m*/ s.clear(); Transaction tx = s.beginTransaction(); QueryParser parser = new QueryParser(getTargetLuceneVersion(), "dept", SearchTestCase.standardAnalyzer); Query query = parser.parse("dept:ITech"); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery(query, Employee.class); hibQuery.setSort(new Sort(new SortField("id", SortField.STRING))); hibQuery.setProjection("id", "lastname", "dept"); hibQuery.setFetchSize(3); ScrollableResults results = hibQuery.scroll(); results.beforeFirst(); results.next(); Object[] result = results.get(); assertEquals("incorrect entityInfo returned", 1000, result[0]); results.scroll(2); result = results.get(); assertEquals("incorrect entityInfo returned", 1003, result[0]); // check cache addition results.next(); result = results.get(); assertEquals("incorrect entityInfo returned", 1004, result[0]); results.scroll(-2); result = results.get(); assertEquals("incorrect entityInfo returned", 1002, result[0]); //cleanup for (Object element : s.createQuery("from " + Employee.class.getName()).list()) { s.delete(element); } tx.commit(); s.close(); }
From source file:org.hibernate.search.test.query.LuceneQueryTest.java
License:Open Source License
public void testFetchSizeNonDefaultFirstAndMax() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);// ww w .j a v a 2s . c om s.clear(); Transaction tx = s.beginTransaction(); QueryParser parser = new QueryParser(getTargetLuceneVersion(), "dept", SearchTestCase.standardAnalyzer); Query query = parser.parse("dept:ITech"); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery(query, Employee.class); hibQuery.setProjection("id", "lastname", "dept"); hibQuery.setFetchSize(3); hibQuery.setFirstResult(1); hibQuery.setMaxResults(3); hibQuery.setSort(new Sort(new SortField("id", SortField.STRING))); ScrollableResults results = hibQuery.scroll(); results.beforeFirst(); results.next(); Object[] result = results.get(); assertEquals("incorrect entityInfo returned", 1002, result[0]); results.scroll(2); result = results.get(); assertEquals("incorrect entityInfo returned", 1004, result[0]); results.next(); result = results.get(); assertNull(result); results.scroll(-8); result = results.get(); assertNull(result); // And test a bad forward scroll. results.scroll(10); result = results.get(); assertNull(result); //cleanup for (Object element : s.createQuery("from " + Employee.class.getName()).list()) { s.delete(element); } tx.commit(); s.close(); }