List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.hibernate.search.demos.tweets.ServiceImpl.java
License:LGPL
/** * To search for all tweets, sorted in creation order (assuming the timestamp is correct). * @return/*from w w w.ja va 2 s . co m*/ */ public FullTextQuery allTweetsSortedByTime() { Query query = getQueryBuilder().all().createQuery(); FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(query); fullTextQuery.setSort(new Sort(new SortedNumericSortField("timestamp", SortField.Type.LONG))); return fullTextQuery; }
From source file:org.hibernate.search.elasticsearch.test.ElasticsearchIT.java
License:LGPL
@Test @TestForIssue(jiraKey = "HSEARCH-2253") public void testSort() throws Exception { Session s = openSession();/*from w ww . ja v a 2 s .c o m*/ FullTextSession session = Search.getFullTextSession(s); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticsearchQueries .fromJson("{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }"); // by title, ascending List<?> result = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("title", SortField.Type.STRING, false))).list(); assertThat(result).onProperty("title").containsExactly("High-performance ORM", "Latest in ORM", "ORM for dummies", "ORM modelling"); // By id, descending result = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("id", SortField.Type.STRING, true))).list(); assertThat(result).onProperty("id").containsExactly(5L, 4L, 2L, 1L); tx.commit(); s.close(); }
From source file:org.hibernate.search.elasticsearch.test.ElasticsearchScrollingIT.java
License:LGPL
private HSQuery getQuery(Query luceneQuery) { ExtendedSearchIntegrator sf = sfHolder.getSearchFactory(); HSQuery hsQuery = sf.createHSQuery(luceneQuery, IndexedObject.class); return hsQuery.projection("id").sort(new Sort(new SortField("idSort", SortField.Type.INT))); }
From source file:org.hibernate.search.elasticsearch.test.ElasticsearchSimpleQueryStringDSLIT.java
License:LGPL
@Test(expected = SearchException.class) @TestForIssue(jiraKey = "HSEARCH-2678") public void testOverridingSeveralAnalyzers() { QueryBuilder qb = sfHolder.getSearchFactory().buildQueryBuilder().forEntity(Book.class) .overridesForField("author", "titleAnalyzer").overridesForField("title", "authorAnalyzer").get(); Query query = qb.simpleQueryString().onFields("title", "author").withAndAsDefaultOperator() .matching("Molire").createQuery(); HSQuery hsQuery = sfHolder.getSearchFactory().createHSQuery(query, Book.class); hsQuery.sort(new Sort(new SortField("title_sort", SortField.Type.STRING))); hsQuery.queryEntityInfos();//from w w w. ja v a2s.c o m }
From source file:org.hibernate.search.engineperformance.elasticsearch.NonStreamWriteJMHBenchmarks.java
License:LGPL
@Benchmark @Threads(6 * AbstractBookEntity.TYPE_COUNT) public void queryBooksByBestRating(NonStreamWriteEngineHolder eh, NonStreamQueryParams qp, Blackhole bh) { SearchIntegrator searchIntegrator = eh.getSearchIntegrator(); Class<?> entityType = qp.getEntityType(); Query luceneQuery = searchIntegrator.buildQueryBuilder().forEntity(entityType).get().all().createQuery(); int maxResults = qp.getQueryMaxResults(); HSQuery hsQuery = searchIntegrator.createHSQuery(luceneQuery, entityType); hsQuery.sort(new Sort(new SortField("rating", SortField.Type.FLOAT, true))); hsQuery.maxResults(maxResults);// w w w . j av a2 s. com int queryResultSize = hsQuery.queryResultSize(); List<EntityInfo> queryEntityInfos = hsQuery.queryEntityInfos(); bh.consume(queryResultSize); bh.consume(queryEntityInfos); }
From source file:org.hibernate.search.engineperformance.JMHBenchmarks.java
License:LGPL
@Benchmark @Threads(20)// w w w. j av a2 s . c o m public void queryBooksByBestRating(QueryEngineHolder eh, Blackhole bh) { SearchIntegrator searchIntegrator = eh.si; Query luceneQuery = searchIntegrator.buildQueryBuilder().forEntity(BookEntity.class).get().all() .createQuery(); long expectedIndexSize = eh.getExpectedIndexSize(); int maxResults = eh.getMaxResults(); HSQuery hsQuery = searchIntegrator.createHSQuery(luceneQuery, BookEntity.class); hsQuery.targetedEntities(Arrays.<Class<?>>asList(BookEntity.class)); hsQuery.sort(new Sort(new SortField("rating", SortField.Type.FLOAT, true))); hsQuery.maxResults(maxResults); int queryResultSize = hsQuery.queryResultSize(); List<EntityInfo> queryEntityInfos = hsQuery.queryEntityInfos(); if (eh.isQuerySync() && queryResultSize != expectedIndexSize) { throw new RuntimeException("Unexpected index size"); } if (maxResults != queryEntityInfos.size()) { throw new RuntimeException("Unexpected resultset size"); } bh.consume(queryEntityInfos); }
From source file:org.hibernate.search.engineperformance.lucene.JMHBenchmarks.java
License:LGPL
@Benchmark @Threads(20)/*from w ww . jav a 2s.c o m*/ public void queryBooksByBestRating(QueryEngineHolder eh, Blackhole bh) { SearchIntegrator searchIntegrator = eh.si; Query luceneQuery = searchIntegrator.buildQueryBuilder().forEntity(BookEntity.class).get().all() .createQuery(); long expectedIndexSize = eh.getExpectedIndexSize(); int maxResults = eh.getMaxResults(); HSQuery hsQuery = searchIntegrator.createHSQuery(luceneQuery, BookEntity.class); hsQuery.sort(new Sort(new SortField("rating", SortField.Type.FLOAT, true))); hsQuery.maxResults(maxResults); int queryResultSize = hsQuery.queryResultSize(); List<EntityInfo> queryEntityInfos = hsQuery.queryEntityInfos(); if (eh.isQuerySync() && queryResultSize != expectedIndexSize) { throw new RuntimeException("Unexpected index size"); } if (maxResults != queryEntityInfos.size()) { throw new RuntimeException("Unexpected resultset size"); } bh.consume(queryEntityInfos); }
From source file:org.hibernate.search.query.dsl.sort.impl.SortFieldStates.java
License:LGPL
public Sort createSort() { return new Sort(sortFields.toArray(new SortField[sortFields.size()])); }
From source file:org.hibernate.search.test.backend.elasticsearch.ElasticSearchIT.java
License:LGPL
@Test public void testSort() throws Exception { Session s = openSession();//from ww w .j a va 2 s . co m FullTextSession session = Search.getFullTextSession(s); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticSearchQueries .fromJson("{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }"); // by title, ascending List<?> result = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("title", SortField.Type.STRING, false))).list(); assertThat(result).onProperty("title").containsExactly("High-performance ORM", "Latest in ORM", "ORM for dummies", "ORM modelling"); // By id, descending result = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("_uid", SortField.Type.STRING, true))).list(); assertThat(result).onProperty("id").containsExactly(5L, 4L, 2L, 1L); tx.commit(); s.close(); }
From source file:org.hibernate.search.test.dsl.BoostDSLTest.java
License:LGPL
@Test public void testBoostOnTermQuery() { QueryBuilder qb = helper.queryBuilder(Coffee.class); Query query = qb.bool().should(qb.keyword().onField("name").boostedTo(40f).matching("Kazaar").createQuery()) .should(qb.keyword().onField("summary").boostedTo(1f).matching("VELVETY").createQuery()) .createQuery();//from w ww . ja v a 2 s. c o m helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Kazaar", "Dharkan"); query = qb.bool().should(qb.keyword().onField("name").boostedTo(1f).matching("Kazaar").createQuery()) .should(qb.keyword().onField("summary").boostedTo(40f).matching("VELVETY").createQuery()) .createQuery(); helper.assertThat(query).from(Coffee.class).sort(new Sort(SortField.FIELD_SCORE)) .matchesExactlyIds("Dharkan", "Kazaar"); }