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.SortTest.java

License:LGPL

@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", SortField.Type.STRING)); //ASC
    hibQuery.setSort(sort);// ww  w .  j a  v a 2 s  . c  om
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertThat(result).onProperty("id").containsExactly(2, 1, 3, 4, 10);

    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
@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).onProperty("id").containsExactly(1, 2, 3, 10);

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

    tx.commit();
}

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

License:LGPL

@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);/*from   w  ww  .j  ava  2  s.  c  o  m*/

    @SuppressWarnings("unchecked")
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertThat(result).onProperty("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();
}

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

License:LGPL

/**
 * The index is shared by two entities. One declares the required sorts, the other does not. As this would require
 * uninverting the index for one entity but not the other, that situation is considered inconsistent and an
 * exception is expected./*  w  w w  .j  av  a2  s  .  c  o m*/
 */
@Test
@Category(SkipOnElasticsearch.class) // This problem does not affect the Elasticsearch backend
public void testQueryOnIndexSharedByEntityWithRequiredSortFieldAndEntityWithoutRaisesException()
        throws Exception {
    Transaction tx = fullTextSession.beginTransaction();

    Query query = queryParser.parse("name:Bill");
    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Thatcher.class, BrickLayer.class);
    Sort sort = new Sort(new SortField("sortName", SortField.Type.STRING)); //ASC
    hibQuery.setSort(sort);

    try {
        hibQuery.list();
        fail("Expected exception was not raised");
    } catch (Exception e) {
        assertThat(e.getMessage()).contains("HSEARCH000298");
    }

    tx.commit();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
public void testResultOrderedById() 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.STRING, false));
    hibQuery.setSort(sort);//from ww w . j av a  2 s  . com
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 3, result.size());
    int id = 1;
    for (Book b : result) {
        assertEquals("Expected another id", Integer.valueOf(id), b.getId());
        id++;
    }

    tx.commit();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
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.STRING)); //ASC
    hibQuery.setSort(sort);//from w  w w . j a v a 2 s  . co  m
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 4, result.size());
    assertEquals("Groovy in Action", result.get(0).getSummary());

    tx.commit();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
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.STRING, true)); //DESC
    hibQuery.setSort(sort);/*  ww w. j  a  va 2  s  .  co  m*/
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 4, result.size());
    assertEquals("Hibernate & Lucene", result.get(0).getSummary());

    tx.commit();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
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.STRING, true)); //DESC
    hibQuery.setSort(sort);//from  www. j  a va2  s .co  m
    List<Book> result = hibQuery.list();
    assertNotNull(result);
    assertEquals("Wrong number of test results.", 4, result.size());
    for (Book book : result) {
        System.out.println(book.getSummary() + " : " + book.getPublicationDate());
    }
    assertEquals("Groovy in Action", result.get(0).getSummary());

    tx.commit();
}

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

License:Open Source License

@SuppressWarnings("unchecked")
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);//from   ww w . j  ava  2 s. c o  m
    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.SortTest.java

License:Open Source License

@SuppressWarnings("unchecked")
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);//w  w w  .j av  a 2s  .co  m
    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();
}