List of usage examples for org.apache.lucene.search Sort Sort
public Sort(SortField... fields)
From source file:org.fenixedu.bennu.search.Search.java
License:Open Source License
public <T extends Indexable> List<T> search(Class<T> type, int maxHits) { if (sort.isEmpty()) { return DomainIndexSystem.search(type, query, maxHits); }/*from w w w . jav a2 s. com*/ return DomainIndexSystem.search(type, query, maxHits, new Sort(sort.toArray(new SortField[0]))); }
From source file:org.geotoolkit.lucene.filter.SpatialQuery.java
License:Open Source License
@Override public void setSort(String fieldName, boolean desc, Character fieldType) { final SortField sf; if (fieldType != null) { switch (fieldType) { case 'd': sf = new SortField(fieldName, SortField.Type.DOUBLE, desc); break; case 'i': sf = new SortField(fieldName, SortField.Type.INT, desc); break; case 'f': sf = new SortField(fieldName, SortField.Type.FLOAT, desc); break; case 'l': sf = new SortField(fieldName, SortField.Type.LONG, desc); break; default:/*from w w w.j a va 2 s .co m*/ sf = new SortField(fieldName, SortField.Type.STRING, desc); break; } } else { sf = new SortField(fieldName, SortField.Type.STRING, desc); } final Sort sortFilter = new Sort(sf); setSort(sortFilter); }
From source file:org.hbasene.index.search.TestHBaseIndexSearcher.java
License:Apache License
@Test public void testSortFieldAsc() throws IOException { LOG.info(this.airportMap.toString()); TermQuery termQuery = new TermQuery(new Term("searchterm", "always")); Sort sort = new Sort(new SortField("airport", SortField.STRING)); TopDocs docs = this.indexSearcher.search(termQuery.createWeight(indexSearcher), null, 25, sort, false); LOG.info("Total results are " + docs.scoreDocs.length); this.printScoreDocs(docs.scoreDocs, "Sorted "); assertSortOrderAsc(docs.scoreDocs);/*from ww w.j a v a 2s .co m*/ }
From source file:org.hbasene.index.search.TestHBaseIndexSearcher.java
License:Apache License
@Test public void testSortFieldDesc() throws IOException { LOG.info(this.airportMap.toString()); TermQuery termQuery = new TermQuery(new Term("searchterm", "always")); Sort sort = new Sort(new SortField("airport", SortField.STRING, true)); //sort by reverse TopDocs docs = this.indexSearcher.search(termQuery.createWeight(indexSearcher), null, 25, sort, false); LOG.info("Total results are " + docs.scoreDocs.length); this.printScoreDocs(docs.scoreDocs, "Sorted "); assertSortOrderDesc(docs.scoreDocs); }
From source file:org.hibernate.hql.lucene.internal.LuceneQueryRendererDelegate.java
License:Open Source License
@Override public LuceneQueryParsingResult getResult() { Sort sort = null;//from w ww . j av a2 s.co m if (sortFields != null) { sort = new Sort(sortFields.toArray(new SortField[sortFields.size()])); } return new LuceneQueryParsingResult(builder.build(), targetTypeName, targetType, projections, sort); }
From source file:org.hibernate.search.backend.elasticsearch.test.ElasticsearchIT.java
License:LGPL
@Test public void testFirstResultAndMaxResults() throws Exception { Session s = openSession();/* w w w . j ava 2 s . com*/ 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).onProperty("title").containsOnly("Latest in ORM", "High-performance ORM"); tx.commit(); s.close(); }
From source file:org.hibernate.search.backend.elasticsearch.test.ElasticsearchIT.java
License:LGPL
@Test public void testScroll() throws Exception { Session s = openSession();// w ww . jav a 2 s .c o m FullTextSession session = Search.getFullTextSession(s); Transaction tx = s.beginTransaction(); QueryDescriptor query = ElasticsearchQueries .fromJson("{ 'query': { 'match' : { 'abstract' : 'Hibernate' } } }"); FullTextQuery fullTextQuery = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("id", SortField.Type.STRING, false))); ScrollableResults scrollableResults = fullTextQuery.scroll(); assertEquals(-1, scrollableResults.getRowNumber()); assertTrue(scrollableResults.last()); assertEquals(3, scrollableResults.getRowNumber()); scrollableResults.beforeFirst(); List<ScientificArticle> articles = new ArrayList<>(); while (scrollableResults.next()) { articles.add((ScientificArticle) scrollableResults.get()[0]); } scrollableResults.close(); assertThat(articles).onProperty("title").containsExactly("ORM for dummies", "Latest in ORM", "High-performance ORM", "ORM modelling"); fullTextQuery = session.createFullTextQuery(query, ScientificArticle.class) .setSort(new Sort(new SortField("id", SortField.Type.STRING, false))); scrollableResults = fullTextQuery.setFirstResult(1).setMaxResults(2).scroll(); assertEquals(-1, scrollableResults.getRowNumber()); assertTrue(scrollableResults.last()); assertEquals(1, scrollableResults.getRowNumber()); scrollableResults.beforeFirst(); articles = new ArrayList<>(); while (scrollableResults.next()) { articles.add((ScientificArticle) scrollableResults.get()[0]); } scrollableResults.close(); assertThat(articles).onProperty("title").containsExactly("Latest in ORM", "High-performance ORM"); tx.commit(); s.close(); }
From source file:org.hibernate.search.backend.elasticsearch.test.ElasticsearchIT.java
License:LGPL
@Test public void testSort() throws Exception { Session s = openSession();/* w ww. java2s. 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("_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.backend.elasticsearch.test.ElasticsearchSpatialIT.java
License:LGPL
@Test public void testGeoDistanceQuery() { Session s = openSession();/*from ww w .ja v a2 s .co m*/ FullTextSession session = Search.getFullTextSession(s); Transaction tx = s.beginTransaction(); String geoDistanceQuery = "{\n" + "'query' : {\n" + "'bool' : {\n" + "'must' : {\n" + "'match_all' : {}\n" + "},\n" + "'filter' : {\n" + "'geo_distance' : {\n" + "'distance' : '12km',\n" + "'location' : {\n" + "'lat' : 24,\n" + "'lon' : 32\n" + "}\n" + "}\n" + "}\n" + "}\n" + "}\n" + "}"; QueryDescriptor query = ElasticsearchQueries.fromJson(geoDistanceQuery); List<?> result = session.createFullTextQuery(query, POI.class) .setSort(new Sort(new DistanceSortField(24, 32, "location"))).list(); assertThat(result).onProperty("id").describedAs("Geo distance query").containsOnly(1, 2, 3); tx.commit(); s.close(); }
From source file:org.hibernate.search.backend.lucene.search.impl.LuceneSearchQueryElementCollector.java
License:LGPL
public Sort toLuceneSort() { if (sortFields == null || sortFields.isEmpty()) { return null; }// w w w . j av a2 s. c o m return new Sort(sortFields.toArray(new SortField[sortFields.size()])); }