FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(this.entityManager); QueryBuilder qb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get(); FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(qb.all().createQuery()); // I added this line to use projections fullTextQuery.setProjection("id", "name", "email", "user", "phone"); Sort sortField = new Sort(new SortField("name", SortField.STRING)); fullTextQuery.setSort(sortField); return fullTextQuery.getResultList();
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(); }
@Test @TestForIssue(jiraKey = "HSEARCH-2467") public void testDateFieldRangeQuery() throws Exception { Session s = openSession(); FullTextSession session = Search.getFullTextSession( s ); Transaction tx = s.beginTransaction(); Query query = NumericRangeQuery.newLongRange( "dateOfBirth", -373078800000L, null, true, true ); List<?> result = session.createFullTextQuery( query, GolfPlayer.class ) .setSort( new Sort( new SortField( "id", SortField.Type.STRING ) ) ) .list(); assertThat( result ).extracting( "firstName" ).containsExactlyInAnyOrder( "Klaus" ); tx.commit(); s.close(); }
@Test public void testFirstResultAndMaxResults() throws Exception { Session s = openSession(); FullTextSession session = Search.getFullTextSession( s ); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }" ); List<?> result = session.createFullTextQuery( query, ScientificArticle.class ) .setFirstResult( 1 ) .setMaxResults( 2 ) .setSort( new Sort( new SortField( "id", SortField.Type.STRING, false ) ) ) .list(); assertThat( result ).extracting( "title" ).containsExactlyInAnyOrder( "Latest in ORM", "High-performance ORM" ); tx.commit(); s.close(); }
@SuppressWarnings("unchecked") @Test @TestForIssue(jiraKey = "HSEARCH-2287") public void testChangingSortOrder() throws Exception { Transaction tx = fullTextSession.beginTransaction(); Query query = queryParser.parse( "summary:lucene" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); hibQuery.setSort( new Sort( new SortField( "id_forIntegerSort", SortField.Type.INT, false ) ) ); List<Book> result = hibQuery.list(); assertThat( result ).extracting( "id" ).containsExactly( 1, 2, 3, 10 ); hibQuery.setSort( new Sort( new SortField( "id_forIntegerSort", SortField.Type.INT, true ) ) ); result = hibQuery.list(); assertThat( result ).extracting( "id" ).containsExactly( 10, 3, 2, 1 ); tx.commit(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedByIdAsString() throws Exception { Transaction tx = fullTextSession.beginTransaction(); Query query = queryParser.parse( "summary:lucene" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "id", SortField.Type.STRING, false ) ); hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactly( 1, 10, 2, 3 ); tx.commit(); }
@SuppressWarnings("unchecked") private List<Person> findPeopleFromIndex(FullTextSession session, String term, String value) { Query luceneQuery = createLuceneQuery( term, value ); return session.createFullTextQuery( luceneQuery, Person.class ) .setSort( new Sort( new SortField( "surname", SortField.Type.STRING ) ) ).list(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedBySummaryStringAscending() throws Exception { Transaction tx = fullTextSession.beginTransaction(); // order by summary Query query = queryParser.parse( "summary:lucene OR summary:action" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "summary_forSort", SortField.Type.STRING ) ); //ASC hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertEquals( "Wrong number of test results.", 5, result.size() ); assertEquals( "Groovy in Action", result.get( 0 ).getSummary() ); tx.commit(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedBySummaryStringDescending() throws Exception { Transaction tx = fullTextSession.beginTransaction(); // order by summary backwards Query query = queryParser.parse( "summary:lucene OR summary:action" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "summary_forSort", SortField.Type.STRING, true ) ); //DESC hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertEquals( "Wrong number of test results.", 5, result.size() ); assertEquals( "Hibernate & Lucene", result.get( 0 ).getSummary() ); tx.commit(); }
@Test @SuppressWarnings("unchecked") public void testResultOrderedByEmbeddedAuthorNameAscending() throws Exception { Transaction tx = fullTextSession.beginTransaction(); // order by summary Query query = queryParser.parse( "summary:lucene OR summary:action" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "mainAuthor.name_sort", SortField.Type.STRING ) ); //ASC hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactly( 2, 1, 3, 4, 10 ); tx.commit(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedByIdAsLong() throws Exception { Transaction tx = fullTextSession.beginTransaction(); Query query = queryParser.parse( "summary:lucene" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "id_forIntegerSort", SortField.Type.INT, false ) ); hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactly( 1, 2, 3, 10 ); tx.commit(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedByDocId() throws Exception { Transaction tx = fullTextSession.beginTransaction(); Query query = queryParser.parse( "summary:lucene" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( null, SortField.Type.DOC, false ) ); hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactlyInAnyOrder( 1, 2, 3, 10 ); tx.commit(); }
@SuppressWarnings("unchecked") @Test public void testResultOrderedByDateDescending() throws Exception { Transaction tx = fullTextSession.beginTransaction(); // order by date backwards Query query = queryParser.parse( "summary:lucene OR summary:action" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Book.class ); Sort sort = new Sort( new SortField( "publicationDate", SortField.Type.STRING, true ) ); //DESC hibQuery.setSort( sort ); List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactly( 4, 10, 3, 2, 1 ); assertEquals( "Groovy in Action", result.get( 0 ).getSummary() ); tx.commit(); }
@Test public void testCombinedQueryOnIndexWithSortFieldAndIndexToBeUninverted() throws Exception { Transaction tx = fullTextSession.beginTransaction(); Query query = queryParser.parse( "name:Bill" ); FullTextQuery hibQuery = fullTextSession.createFullTextQuery( query, Plumber.class, BrickLayer.class ); Sort sort = new Sort( new SortField( "sortName", SortField.Type.STRING ) ); //ASC hibQuery.setSort( sort ); @SuppressWarnings("unchecked") List<Book> result = hibQuery.list(); assertNotNull( result ); assertThat( result ).extracting( "name" ) .describedAs( "Expecting results from index with sort field and uninverted index in the correct sort order" ) .containsExactly( "Bill the brick layer", "Bill the plumber" ); tx.commit(); }
@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 ); assertThat( result ).extracting( "id" ).containsExactly( 3, 1, 2 ); tx.commit(); fullTextSession.close(); }
@Test public void testSortableFieldConfiguredThroughClassLevelBridgeOnEmbeddedEntity() 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.territoryName", SortField.Type.STRING ) ) ) .list(); assertNotNull( result ); assertThat( result ).extracting( "id" ).containsExactly( 2, 1, 3 ); tx.commit(); fullTextSession.close(); }
@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(); }
@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(); }
@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(); }
@Override @SuppressWarnings({ "unchecked", "unused" }) protected void execute(FullTextSession fts) { Query q = fts.getSearchFactory() .buildQueryBuilder() .forEntity( Book.class ) .get() .range() .onField( "rating" ) .from( 40.0f ) .to( 60.0f ) .createQuery(); List<Book> result = fts.createFullTextQuery( q, Book.class ) .setSort( new Sort( new SortField( "rating", SortField.Type.FLOAT, true ) ) ) .list(); }