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

License:Open Source License

@SuppressWarnings("unchecked")
public void testResultOrderedByDateDescending() throws Exception {
    EntityTransaction tx = em.getTransaction();
    tx.begin();/*w  ww  .  j a v  a2  s .co m*/
    Sort dateDescending = new Sort(new SortField("creationDate", SortField.STRING, DESC));
    List<ProductArticle> result = query("Hib*").setSort(dateDescending).setFirstResult(3).getResultList();

    assertThat(result).as("query result").hasSize(3);
    assertThat(result.get(0).getArticleId()).as("article id").isEqualTo(3);
    assertThat(result.get(1).getArticleId()).as("article id").isEqualTo(2);
    assertThat(result.get(2).getArticleId()).as("article id").isEqualTo(1);
    tx.commit();
    em.clear();
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testSortingOnNumericInt() {
    // Index all testData:
    storeTestingData(new Person(0, 3, "Three"), new Person(1, 10, "Ten"), new Person(2, 9, "Nine"),
            new Person(3, 5, "Five"));

    // Non sorted, expect results in indexing order:
    Query query = factoryHolder.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get().all()
            .createQuery();/*from  w w  w.  j a va 2s  . c  o m*/
    assertSortedResults(query, null, 0, 1, 2, 3);

    // Sorting Age as string:
    Sort sortAsString = new Sort(new SortField("ageForStringSorting", SortField.Type.STRING));
    assertSortedResults(query, sortAsString, 1, 0, 3, 2);

    // Sorting Age as Int (numeric):
    Sort sortAsInt = new Sort(new SortField("ageForIntSorting", SortField.Type.INT));
    assertSortedResults(query, sortAsInt, 0, 3, 2, 1);
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testSortingOnString() {
    // Index all testData:
    storeTestingData(new Person(0, 3, "Three"), new Person(1, 10, "Ten"), new Person(2, 9, "Nine"),
            new Person(3, 5, "Five"));

    // Sorting Name
    Query query = factoryHolder.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get().all()
            .createQuery();/*  www.  j a va2s.  c o  m*/
    Sort sortAsString = new Sort(new SortField("name", SortField.Type.STRING));
    assertSortedResults(query, sortAsString, 3, 2, 1, 0);
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
@TestForIssue(jiraKey = "HSEARCH-2376")
public void testSortingOnCollatedString() {
    // Index all testData:
    storeTestingData(new Person(0, 3, "lonore"), new Person(1, 10, "douard"), new Person(2, 9, "Edric"),
            new Person(3, 5, "aaron"), new Person(4, 7, " zach"));

    // Sorting by collated name
    Query query = factoryHolder.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get().all()
            .createQuery();/*from ww w .j  a va2  s  .c  om*/
    Sort sortAsString = new Sort(new SortField("collatedName", SortField.Type.STRING));
    assertSortedResults(query, sortAsString, 4, 3, 1, 2, 0);
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testSortingOnEmbeddedString() {
    // Index all testData:
    storeTestingData(new Person(0, 3, "Three", new CuddlyToy("Hippo")),
            new Person(1, 10, "Ten", new CuddlyToy("Giraffe")),
            new Person(2, 9, "Nine", new CuddlyToy("Gorilla")),
            new Person(3, 5, "Five", new CuddlyToy("Alligator")));

    Query query = factoryHolder.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get().all()
            .createQuery();// w  w  w .j  a v a2s. c  o  m
    Sort sortAsString = new Sort(new SortField("favoriteCuddlyToy.type", SortField.Type.STRING));
    assertSortedResults(query, sortAsString, 3, 1, 2, 0);
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

/**
 * Sortable fields within an embedded to-many association should be ignored. They should not prevent other sort
 * fields from working, though./*  w  w w. ja  va 2  s.  co m*/
 */
@Test
@TestForIssue(jiraKey = "HSEARCH-2000")
public void testSortingForTypeWithSortableFieldWithinEmbeddedToManyAssociation() {
    // Index all testData:
    storeTestingData(
            new Person(0, 3, "Three",
                    Arrays.asList(new Friend(new CuddlyToy("Hippo")), new Friend(new CuddlyToy("Giraffe")))),
            new Person(1, 10, "Ten", Arrays.asList(new Friend(new CuddlyToy("Gorilla")),
                    new Friend(new CuddlyToy("Alligator")))));

    Query query = factoryHolder.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get().all()
            .createQuery();
    Sort sortAsString = new Sort(new SortField("ageForStringSorting", SortField.Type.STRING));
    assertSortedResults(query, sortAsString, 1, 0);
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testExceptionSortingStringFieldAsNumeric() throws Exception {
    thrown.expect(SearchException.class);
    thrown.expectMessage(SORT_TYPE_ERROR_CODE);

    storeTestingData(new UnsortableToy("111", "Teddy Bear", 300L, 555));
    Class<?> entityType = UnsortableToy.class;

    ExtendedSearchIntegrator integrator = factoryHolder.getSearchFactory();
    QueryBuilder queryBuilder = integrator.buildQueryBuilder().forEntity(entityType).get();
    Query query = queryBuilder.keyword().onField("description").matching("Teddy Bear").createQuery();

    Sort sort = new Sort(new SortField("description", SortField.Type.DOUBLE));
    HSQuery hsQuery = integrator.createHSQuery(query, entityType);
    hsQuery.sort(sort);//w  ww .j  a va  2 s.  c om
    hsQuery.queryEntityInfos().size();
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testExceptionSortingNumericFieldWithStringType() throws Exception {
    thrown.expect(SearchException.class);
    thrown.expectMessage(SORT_TYPE_ERROR_CODE);

    storeTestingData(new UnsortableToy("111", "Teddy Bear", 300L, 555));
    Class<?> entityType = UnsortableToy.class;

    ExtendedSearchIntegrator integrator = factoryHolder.getSearchFactory();
    QueryBuilder queryBuilder = integrator.buildQueryBuilder().forEntity(entityType).get();
    Query query = queryBuilder.keyword().onField("description").matching("Teddy Bear").createQuery();

    Sort sort = new Sort(new SortField("longValue", SortField.Type.STRING));
    HSQuery hsQuery = integrator.createHSQuery(query, entityType);
    hsQuery.sort(sort);// ww w.ja  va 2  s.c om
    hsQuery.queryEntityInfos().size();
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testExceptionSortingNumericFieldWithWrongType() throws Exception {
    thrown.expect(SearchException.class);
    thrown.expectMessage(SORT_TYPE_ERROR_CODE);

    storeTestingData(new UnsortableToy("111", "Teddy Bear", 300L, 555));
    Class<?> entityType = UnsortableToy.class;

    ExtendedSearchIntegrator integrator = factoryHolder.getSearchFactory();
    QueryBuilder queryBuilder = integrator.buildQueryBuilder().forEntity(entityType).get();
    Query query = queryBuilder.keyword().onField("description").matching("Teddy Bear").createQuery();

    Sort sort = new Sort(new SortField("longValue", SortField.Type.INT));
    HSQuery hsQuery = integrator.createHSQuery(query, entityType);
    hsQuery.sort(sort);//from   w ww .j  a v a 2s. com
    hsQuery.queryEntityInfos().size();
}

From source file:org.hibernate.search.test.sorting.SortingTest.java

License:LGPL

@Test
public void testSortOnNullableNumericFieldArray() throws Exception {
    storeTestingData(new Person(1, 25, "name1", 1, 2, 3), new Person(2, 22, "name2", 1, null, 3),
            new Person(3, 23, "name3", null, null, null));

    Query rangeQuery = queryForRangeOnFieldSorted(0, 2, "array");
    Sort sortAsInt = new Sort(new SortField("array", SortField.Type.INT));
    assertNumberOfResults(2, rangeQuery, sortAsInt);
}