Example usage for org.apache.lucene.search Sort Sort

List of usage examples for org.apache.lucene.search Sort Sort

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort Sort.

Prototype

public Sort(SortField... fields) 

Source Link

Document

Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc.

Usage

From source file:org.hibernate.search.test.query.sorting.SortOnFieldsFromCustomBridgeTest.java

License:LGPL

@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);/*from  www.j a v  a 2s . co m*/
    assertThat(result).onProperty("id").containsExactly(2, 1, 3);

    tx.commit();
    fullTextSession.close();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test//from   w ww  .  jav  a  2  s.co m
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).onProperty("id").containsExactly(1, 10, 2, 3);

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test//w  w w. j  a v  a  2 s . c  o m
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).onProperty("id").containsExactly(1, 2, 3, 10);

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test/*from   w w  w  .ja v a  2  s  . c o  m*/
@Category(SkipOnElasticsearch.class)
public void testResultOrderedByIdAlteringSortStyle() 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", SortField.Type.STRING, false)));
    List<Book> result = hibQuery.list();
    assertThat(result).onProperty("id").containsExactly(1, 10, 2, 3);

    hibQuery.setSort(new Sort(new SortField("id_forIntegerSort", SortField.Type.INT, false)));
    result = hibQuery.list();
    assertThat(result).onProperty("id").containsExactly(1, 2, 3, 10);

    hibQuery.setSort(new Sort(new SortField("id", SortField.Type.STRING, false)));
    result = hibQuery.list();
    assertThat(result).onProperty("id").containsExactly(1, 10, 2, 3);

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test/*from www.ja v  a 2  s. com*/
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();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test/*from   ww  w .j  a  v  a2 s  .c o  m*/
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();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test//from   w  ww . j a v  a 2  s  .c o m
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).onProperty("id").containsExactly(4, 10, 3, 2, 1);
    assertEquals("Groovy in Action", result.get(0).getSummary());

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test/*from   w w w.java  2 s .  c o  m*/
@Category(SkipOnElasticsearch.class)
public void testCustomFieldComparatorAscendingSort() {
    Transaction tx = fullTextSession.beginTransaction();

    Query query = new MatchAllDocsQuery();
    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, NumberHolder.class);
    Sort sort = new Sort(new SortField("sum", new SumFieldComparatorSource()));
    hibQuery.setSort(sort);
    List<NumberHolder> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 4, result.size());

    int previousSum = 0;
    for (NumberHolder n : result) {
        assertTrue("Documents should be ordered by increasing sum", previousSum < n.getSum());
        previousSum = n.getSum();
    }

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test/*w  w w . ja  va 2s. co  m*/
@Category(SkipOnElasticsearch.class)
public void testCustomFieldComparatorDescendingSort() {
    Transaction tx = fullTextSession.beginTransaction();

    Query query = new MatchAllDocsQuery();
    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, NumberHolder.class);
    Sort sort = new Sort(new SortField("sum", new SumFieldComparatorSource(), true));
    hibQuery.setSort(sort);
    List<NumberHolder> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 4, result.size());

    int previousSum = 100;
    for (NumberHolder n : result) {
        assertTrue("Documents should be ordered by decreasing sum", previousSum > n.getSum());
        previousSum = n.getSum();
    }

    tx.commit();
}

From source file:org.hibernate.search.test.query.sorting.SortTest.java

License:LGPL

@SuppressWarnings("unchecked")
@Test//from  ww w .  ja  v a2  s. c om
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).onProperty("id").containsOnly(1, 2, 3, 10);

    tx.commit();
}