List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.hibernate.search.test.query.ProjectionQueryTest.java
License:Open Source License
public void testLuceneObjectsProjectionWithScroll() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);/*from ww w .ja v a2 s . com*/ Transaction tx; s.clear(); 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); // Is the 'FullTextQuery.ID' value correct here? Do we want the Lucene internal document number? hibQuery.setProjection("id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.DOCUMENT, FullTextQuery.ID); hibQuery.setSort(new Sort(new SortField("id", SortField.STRING))); resetFieldSelector(); ScrollableResults projections = hibQuery.scroll(); assertFieldSelectorDisabled(); //because of DOCUMENT being projected // There are a lot of methods to check in ScrollableResultsImpl // so, we'll use methods to check each projection as needed. projections.beforeFirst(); projections.next(); Object[] projection = projections.get(); checkProjectionFirst(projection, s); assertTrue(projections.isFirst()); projections.last(); projection = projections.get(); checkProjectionLast(projection, s); assertTrue(projections.isLast()); projections.next(); projection = projections.get(); assertNull(projection); projections.previous(); projection = projections.get(); checkProjectionLast(projection, s); projections.first(); projection = projections.get(); checkProjectionFirst(projection, s); projections.scroll(2); projection = projections.get(); checkProjection2(projection, s); projections.scroll(-5); projection = projections.get(); assertNull(projection); //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.ProjectionQueryTest.java
License:Open Source License
public void testResultTransformToDelimString() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);//www .ja va 2 s . c o m Transaction tx; s.clear(); 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", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.ID); hibQuery.setResultTransformer(new ProjectionToDelimStringResultTransformer()); hibQuery.setSort(new Sort(new SortField("id", SortField.STRING))); resetFieldSelector(); @SuppressWarnings("unchecked") List<String> result = hibQuery.list(); assertFieldSelectorEnabled("lastname", "dept", ProjectionConstants.ID); assertTrue("incorrect transformation", result.get(0).startsWith("1000, Griffin, ITech")); assertTrue("incorrect transformation", result.get(1).startsWith("1002, Jimenez, ITech")); //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.ProjectionQueryTest.java
License:Open Source License
public void testResultTransformMap() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);/*from w w w.j a va 2s . c o m*/ Transaction tx; s.clear(); 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", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.DOCUMENT, FullTextQuery.ID); hibQuery.setSort(new Sort(new SortField("id", SortField.STRING))); hibQuery.setResultTransformer(new ProjectionToMapResultTransformer()); List transforms = hibQuery.list(); Map map = (Map) transforms.get(1); assertEquals("incorrect transformation", "ITech", map.get("dept")); assertEquals("incorrect transformation", 1002, map.get("id")); assertTrue("incorrect transformation", map.get(FullTextQuery.DOCUMENT) instanceof Document); assertEquals("incorrect transformation", "01002", ((Document) map.get(FullTextQuery.DOCUMENT)).get("id")); //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.ProjectionQueryTest.java
License:Open Source License
public void testLuceneObjectsProjectionWithList() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); prepEmployeeIndex(s);/* w w w .j a v a 2s . c o m*/ Transaction tx; s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser(getTargetLuceneVersion(), "dept", SearchTestCase.standardAnalyzer); Query query = parser.parse("dept:Accounting"); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery(query, Employee.class); hibQuery.setProjection("id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.DOCUMENT, FullTextQuery.ID, FullTextQuery.DOCUMENT_ID); List result = hibQuery.list(); assertNotNull(result); Object[] projection = (Object[]) result.get(0); assertNotNull(projection); assertEquals("id incorrect", 1001, projection[0]); assertEquals("last name incorrect", "Jackson", projection[1]); assertEquals("dept incorrect", "Accounting", projection[2]); assertEquals("THIS incorrect", "Jackson", ((Employee) projection[3]).getLastname()); assertEquals("THIS incorrect", projection[3], s.get(Employee.class, (Serializable) projection[0])); assertTrue("SCORE incorrect", projection[4] instanceof Float); assertFalse("SCORE should not be a NaN", Float.isNaN((Float) projection[4])); assertTrue("DOCUMENT incorrect", projection[5] instanceof Document); assertEquals("DOCUMENT size incorrect", 5, ((Document) projection[5]).getFields().size()); assertEquals("ID incorrect", 1001, projection[6]); assertNotNull("Lucene internal doc id", projection[7]); // Change the projection order and null one hibQuery.setProjection(FullTextQuery.DOCUMENT, FullTextQuery.THIS, FullTextQuery.SCORE, null, FullTextQuery.ID, "id", "lastname", "dept", "hireDate", FullTextQuery.DOCUMENT_ID); result = hibQuery.list(); assertNotNull(result); projection = (Object[]) result.get(0); assertNotNull(projection); assertTrue("DOCUMENT incorrect", projection[0] instanceof Document); assertEquals("DOCUMENT size incorrect", 5, ((Document) projection[0]).getFields().size()); assertEquals("THIS incorrect", projection[1], s.get(Employee.class, (Serializable) projection[4])); assertTrue("SCORE incorrect", projection[2] instanceof Float); assertNull("BOOST not removed", projection[3]); assertEquals("ID incorrect", 1001, projection[4]); assertEquals("id incorrect", 1001, projection[5]); assertEquals("last name incorrect", "Jackson", projection[6]); assertEquals("dept incorrect", "Accounting", projection[7]); assertNotNull("Date", projection[8]); assertNotNull("Lucene internal doc id", projection[9]); hibQuery.setSort(new Sort(new SortField("lastname", SortField.STRING_VAL))); hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE); result = hibQuery.list(); projection = (Object[]) result.get(0); assertTrue("SCORE incorrect", projection[1] instanceof Float); assertFalse("SCORE should not be a NaN", Float.isNaN((Float) projection[1])); //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.ScrollableResultsTest.java
License:Open Source License
/** * Test forward scrolling using pagination */// w w w . j ava 2 s .c o m @Test public void testScrollingForward() { Transaction tx = sess.beginTransaction(); TermQuery tq = new TermQuery(new Term("summary", "number")); Sort sort = new Sort(new SortField("id", SortField.STRING)); ScrollableResults scrollableResults = sess.createFullTextQuery(tq, AlternateBook.class).setSort(sort) .setFetchSize(10).setFirstResult(20).setMaxResults(111).scroll(); assertEquals(-1, scrollableResults.getRowNumber()); assertTrue(scrollableResults.last()); assertEquals(110, scrollableResults.getRowNumber()); scrollableResults.beforeFirst(); int position = scrollableResults.getRowNumber(); while (scrollableResults.next()) { position++; int bookId = position + 20; assertEquals(position, scrollableResults.getRowNumber()); AlternateBook book = (AlternateBook) scrollableResults.get()[0]; assertEquals(bookId, book.getId().intValue()); assertEquals("book about the number " + bookId, book.getSummary()); assertTrue(sess.contains(book)); } assertEquals(110, position); scrollableResults.close(); tx.commit(); }
From source file:org.hibernate.search.test.query.ScrollableResultsTest.java
License:Open Source License
/** * Verify inverse-order scrolling./* w w w. j a va 2 s . c om*/ * TODO to verify correct FetchSize behavior I've been debugging * the behavior; we should add a mock library to automate this kind of tests. */ @Test public void testScrollingBackwards() { Transaction tx = sess.beginTransaction(); TermQuery tq = new TermQuery(new Term("summary", "number")); Sort sort = new Sort(new SortField("id", SortField.STRING)); ScrollableResults scrollableResults = sess.createFullTextQuery(tq, AlternateBook.class).setSort(sort) .setFetchSize(10).scroll(); scrollableResults.beforeFirst(); // initial position should be -1 as in Hibernate Core assertEquals(-1, scrollableResults.getRowNumber()); assertTrue(scrollableResults.last()); int position = scrollableResults.getRowNumber(); assertEquals(323, position); while (scrollableResults.previous()) { AlternateBook book = (AlternateBook) scrollableResults.get()[0]; assertEquals(--position, book.getId().intValue()); assertEquals("book about the number " + position, book.getSummary()); } assertEquals(0, position); assertEquals(-1, scrollableResults.getRowNumber()); scrollableResults.close(); tx.commit(); }
From source file:org.hibernate.search.test.query.ScrollableResultsTest.java
License:Open Source License
/** * Test that all entities returned by a ScrollableResults * are always attached to Session//w w w. j a v a2s . c om */ @Test public void testResultsAreManaged() { Transaction tx = sess.beginTransaction(); TermQuery tq = new TermQuery(new Term("summary", "number")); Sort sort = new Sort(new SortField("id", SortField.STRING)); ScrollableResults scrollableResults = sess.createFullTextQuery(tq, AlternateBook.class).setSort(sort) .setFetchSize(10).scroll(); int position = -1; while (scrollableResults.next()) { position++; AlternateBook book = (AlternateBook) scrollableResults.get()[0]; assertTrue(sess.contains(book)); // evict some entities: if (position % 3 == 0) { sess.evict(book); assertFalse(sess.contains(book)); } } //verifies it did scroll to the end: assertEquals(323, position); //assert the entities are re-attached after eviction: while (scrollableResults.previous()) { position--; AlternateBook book = (AlternateBook) scrollableResults.get()[0]; assertTrue(sess.contains(book)); } assertEquals(-1, position); sess.clear(); //assert the entities are re-attached after Session.clear: while (scrollableResults.next()) { position++; AlternateBook book = (AlternateBook) scrollableResults.get()[0]; assertTrue(sess.contains(book)); } assertEquals(323, position); tx.commit(); }
From source file:org.hibernate.search.test.query.ScrollableResultsTest.java
License:Open Source License
/** * Verify scrolling works correctly when combined with Projection * and that the projected entities are managed, even in case * of evict usage for memory management. *//*w w w . j av a2s .c o m*/ @Test public void testScrollProjectionAndManaged() { Transaction tx = sess.beginTransaction(); TermQuery tq = new TermQuery(new Term("dept", "num")); //the tests relies on the results being returned sorted by id: Sort sort = new Sort(new SortField("id", SortField.STRING)); ScrollableResults scrollableResults = sess.createFullTextQuery(tq, Employee.class) .setProjection(FullTextQuery.OBJECT_CLASS, FullTextQuery.ID, FullTextQuery.THIS, "lastname", FullTextQuery.THIS) .setFetchSize(10).setSort(sort).scroll(); scrollableResults.last(); assertEquals(132, scrollableResults.getRowNumber()); scrollableResults.beforeFirst(); assertEquals(-1, scrollableResults.getRowNumber()); int position = scrollableResults.getRowNumber(); while (scrollableResults.next()) { position++; Object[] objs = scrollableResults.get(); assertEquals(Employee.class, objs[0]); assertEquals(position, objs[1]); assertTrue(objs[2] instanceof Employee); sess.contains(objs[2]); assertEquals("Rossi", objs[3]); assertTrue(objs[4] instanceof Employee); sess.contains(objs[4]); assertTrue(objs[2] == objs[4]); //projected twice the same entity // detach some objects: if (position % 3 == 0) { sess.evict(objs[2]); } } //verify we scrolled to the end: assertEquals(132, position); // and now the other way around, checking entities are attached again: while (scrollableResults.previous()) { position--; Object[] objs = scrollableResults.get(); assertTrue(objs[2] instanceof Employee); sess.contains(objs[2]); assertTrue(objs[4] instanceof Employee); sess.contains(objs[4]); assertTrue(objs[2] == objs[4]); } assertEquals(-1, position); scrollableResults.close(); tx.commit(); }
From source file:org.hibernate.search.test.query.sorting.SortOnFieldsFromCustomBridgeTest.java
License:LGPL
@Test public void testSortableFieldConfiguredThroughCustomFieldLevelBridge() throws Exception { FullTextSession fullTextSession = Search.getFullTextSession(openSession()); Transaction tx = fullTextSession.beginTransaction(); @SuppressWarnings("unchecked") List<Book> result = fullTextSession.createFullTextQuery(new MatchAllDocsQuery(), Explorer.class) .setSort(new Sort(new SortField("nameParts_lastName", SortField.Type.STRING))).list(); assertNotNull(result);//from www . j a v a 2 s . co m assertThat(result).onProperty("id").containsExactly(3, 1, 2); tx.commit(); fullTextSession.close(); }
From source file:org.hibernate.search.test.query.sorting.SortOnFieldsFromCustomBridgeTest.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2325") public void testNumericCustomFieldLevelBridge() throws Exception { FullTextSession fullTextSession = Search.getFullTextSession(openSession()); Transaction tx = fullTextSession.beginTransaction(); @SuppressWarnings("unchecked") List<Book> result = fullTextSession.createFullTextQuery(new MatchAllDocsQuery(), Explorer.class) .setSort(new Sort(new SortField("favoriteTerritory.idFromBridge", SortField.Type.INT))).list(); assertNotNull(result);/* www .j a va 2 s . c o m*/ assertThat(result).onProperty("id").containsExactly(1, 2, 3); tx.commit(); fullTextSession.close(); }