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.envers.SearchAndEnversIntegrationTest.java

License:LGPL

@SuppressWarnings("unchecked")
private List<Person> findPeopleFromIndex(FullTextSession session, String term, String value) {
    Query luceneQuery = createLuceneQuery(term, value);
    return session.createFullTextQuery(luceneQuery, Person.class)
            .setSort(new Sort(new SortField("surname", SortField.Type.STRING))).list();
}

From source file:org.hibernate.search.test.jpa.SpatialQueringJPATest.java

License:Open Source License

public void testDistanceSort() throws Exception {
    POI poi = new POI(1, "Distance to 24,32 : 0", 24.0d, 32.0d, "");
    POI poi2 = new POI(2, "Distance to 24,32 : 10.16", 24.0d, 31.9d, "");
    POI poi3 = new POI(3, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "");
    POI poi4 = new POI(4, "Distance to 24,32 : 15.06", 23.9d, 32.1d, "");
    POI poi5 = new POI(5, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "");
    POI poi6 = new POI(6, "Distance to 24,32 : 24.45", 24.2d, 31.9d, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();/*from  w w w. j  a  va 2  s.  co  m*/
    em.persist(poi);
    em.persist(poi2);
    em.persist(poi3);
    em.getTransaction().commit();
    em.getTransaction().begin();
    em.persist(poi4);
    em.persist(poi5);
    em.persist(poi6);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates("location")
            .within(100, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(Point.fromDegrees(centerLatitude, centerLongitude), "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Object[] thirdResult = (Object[]) results.get(2);
    Object[] fourthResult = (Object[]) results.get(3);
    Object[] fifthResult = (Object[]) results.get(4);
    Object[] sixthResult = (Object[]) results.get(5);
    Assert.assertEquals((Double) firstResult[1], 0.0, 0.0001);
    Assert.assertEquals((Double) secondResult[1], 10.1582, 0.0001);
    Assert.assertEquals((Double) thirdResult[1], 11.1195, 0.0001);
    Assert.assertEquals((Double) fourthResult[1], 15.0636, 0.0001);
    Assert.assertEquals((Double) fifthResult[1], 22.239, 0.001);
    Assert.assertEquals((Double) sixthResult[1], 24.446, 0.001);

    List<?> pois = em.createQuery("from " + POI.class.getName()).getResultList();
    for (Object entity : pois) {
        em.remove(entity);
    }
    em.getTransaction().commit();
    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueringJPATest.java

License:Open Source License

public void testDistanceSort2() throws Exception {
    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();//from  w  ww  .j av  a  2 s  .  co m
    int cnt = 0;
    for (double[] c : new double[][] { { 41.04389845, -74.06328534 }, { 40.64383333, -73.75050000 },
            { 40.75666667, -73.98650000 }, { 40.69416667, -73.78166667 }, { 40.75802992, -73.98532391 },
            { 40.75802992, -73.98532391 }, { 50.77687257, 6.08431213 }, { 50.78361600, 6.07003500 },
            { 50.76066667, 6.08866667 }, { 50.77683333, 6.08466667 }, { 50.77650000, 6.08416667 }, }) {
        em.persist(new POI(cnt, "Test_" + cnt, c[0], c[1], ""));
        ++cnt;
    }
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 50.7753455;
    double centerLongitude = 6.083886799999959;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates("location")
            .within(1.8097233616663808, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude)
            .createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setMaxResults(1000);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(Point.fromDegrees(centerLatitude, centerLongitude), "location");
    List<Object[]> results = hibQuery.getResultList();

    for (Object[] result : results) {
        POI poi = (POI) result[0];
        String message = poi.getName() + " (" + poi.getLatitude() + ", " + poi.getLongitude() + ") is not at "
                + centerLatitude + ", " + centerLongitude;

        Assert.assertThat(message, ((Double) result[1]).doubleValue(), is(not(0.0)));
    }

    List<?> pois = em.createQuery("from " + POI.class.getName()).getResultList();
    for (Object entity : pois) {
        em.remove(entity);
    }
    em.getTransaction().commit();
    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueringJPATest.java

License:Open Source License

public void testDistanceSortWithMaxResult() throws Exception {
    POI poi = new POI(1, "Distance to 24,32 : 0", 24.0d, 32.0d, "");
    POI poi2 = new POI(2, "Distance to 24,32 : 10.16", 24.0d, 31.9d, "");
    POI poi3 = new POI(3, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "");
    POI poi4 = new POI(4, "Distance to 24,32 : 15.06", 23.9d, 32.1d, "");
    POI poi5 = new POI(5, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "");
    POI poi6 = new POI(6, "Distance to 24,32 : 24.45", 24.2d, 31.9d, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();//from  w  ww  . j  a  v  a2 s  .  c  o  m
    em.persist(poi);
    em.persist(poi2);
    em.persist(poi3);
    em.getTransaction().commit();
    em.getTransaction().begin();
    em.persist(poi4);
    em.persist(poi5);
    em.persist(poi6);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onCoordinates("location")
            .within(100, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    // Set max results to 3 when 6 documents are stored:
    hibQuery.setMaxResults(3);
    hibQuery.setSpatialParameters(Point.fromDegrees(centerLatitude, centerLongitude), "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Object[] thirdResult = (Object[]) results.get(2);
    Assert.assertEquals((Double) firstResult[1], 0.0, 0.0001);
    Assert.assertEquals((Double) secondResult[1], 10.1582, 0.0001);
    Assert.assertEquals((Double) thirdResult[1], 11.1195, 0.0001);

    List<?> pois = em.createQuery("from " + POI.class.getName()).getResultList();
    for (Object entity : pois) {
        em.remove(entity);
    }
    em.getTransaction().commit();
    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueryingJPATest.java

License:LGPL

@Test
public void testDistanceSort() throws Exception {
    POI poi = new POI(1, "Distance to 24,32 : 0", 24.0d, 32.0d, "");
    POI poi2 = new POI(2, "Distance to 24,32 : 10.16", 24.0d, 31.9d, "");
    POI poi3 = new POI(3, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "");
    POI poi4 = new POI(4, "Distance to 24,32 : 15.06", 23.9d, 32.1d, "");
    POI poi5 = new POI(5, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "");
    POI poi6 = new POI(6, "Distance to 24,32 : 24.45", 24.2d, 31.9d, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();/*from  ww w .j  a  va  2  s.co m*/
    em.persist(poi);
    em.persist(poi2);
    em.persist(poi3);
    em.getTransaction().commit();
    em.getTransaction().begin();
    em.persist(poi4);
    em.persist(poi5);
    em.persist(poi6);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onField("location").within(100, Unit.KM)
            .ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(centerLatitude, centerLongitude, "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Object[] thirdResult = (Object[]) results.get(2);
    Object[] fourthResult = (Object[]) results.get(3);
    Object[] fifthResult = (Object[]) results.get(4);
    Object[] sixthResult = (Object[]) results.get(5);
    Assert.assertEquals(0.0, (Double) firstResult[1], 0.01);
    Assert.assertEquals(10.1582, (Double) secondResult[1], 0.01);
    Assert.assertEquals(11.1195, (Double) thirdResult[1], 0.01);
    Assert.assertEquals(15.0636, (Double) fourthResult[1], 0.01);
    Assert.assertEquals(22.239, (Double) fifthResult[1], 0.02);
    Assert.assertEquals(24.446, (Double) sixthResult[1], 0.02);

    distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location", true));
    hibQuery.setSort(distanceSort);
    results = hibQuery.getResultList();
    firstResult = (Object[]) results.get(0);
    secondResult = (Object[]) results.get(1);
    thirdResult = (Object[]) results.get(2);
    fourthResult = (Object[]) results.get(3);
    fifthResult = (Object[]) results.get(4);
    sixthResult = (Object[]) results.get(5);
    Assert.assertEquals(24.446, (Double) firstResult[1], 0.02);
    Assert.assertEquals(22.239, (Double) secondResult[1], 0.02);
    Assert.assertEquals(15.0636, (Double) thirdResult[1], 0.01);
    Assert.assertEquals(11.1195, (Double) fourthResult[1], 0.01);
    Assert.assertEquals(10.1582, (Double) fifthResult[1], 0.01);
    Assert.assertEquals(0.0, (Double) sixthResult[1], 0.01);

    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueryingJPATest.java

License:LGPL

@Test
public void testDistanceSort2() throws Exception {
    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();/* w  w w .j av  a2s  . c o m*/
    int cnt = 0;
    for (double[] c : new double[][] { { 41.04389845, -74.06328534 }, { 40.64383333, -73.75050000 },
            { 40.75666667, -73.98650000 }, { 40.69416667, -73.78166667 }, { 40.75802992, -73.98532391 },
            { 40.75802992, -73.98532391 }, { 50.77687257, 6.08431213 }, { 50.78361600, 6.07003500 },
            { 50.76066667, 6.08866667 }, { 50.77683333, 6.08466667 }, { 50.77650000, 6.08416667 }, }) {
        em.persist(new POI(cnt, "Test_" + cnt, c[0], c[1], ""));
        ++cnt;
    }
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 50.7753455;
    double centerLongitude = 6.083886799999959;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onField("location")
            .within(1.8097233616663808, Unit.KM).ofLatitude(centerLatitude).andLongitude(centerLongitude)
            .createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setMaxResults(1000);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(centerLatitude, centerLongitude, "location");
    @SuppressWarnings("unchecked")
    List<Object[]> results = hibQuery.getResultList();

    for (Object[] result : results) {
        POI poi = (POI) result[0];
        String message = poi.getName() + " (" + poi.getLatitude() + ", " + poi.getLongitude() + ") is not at "
                + centerLatitude + ", " + centerLongitude;

        Assert.assertThat(message, ((Double) result[1]).doubleValue(), is(not(0.0)));
    }

    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueryingJPATest.java

License:LGPL

@Test
@TestForIssue(jiraKey = "HSEARCH-2324")
public void testDistanceSortMissingCoordinates() throws Exception {
    POI poi = new POI(0, "Distance to 24,32 : 10.16km", 24.0d, 31.9d, "");
    POI poi2 = new POI(1, "Distance to 24,32 : unknown, 4361.00km if interpreted as 0,0", null, null, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();/* ww w. jav  a 2s  . c  o m*/
    em.persist(poi);
    em.persist(poi2);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.all().createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(centerLatitude, centerLongitude, "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Assert.assertEquals(10.1582, (Double) firstResult[1], 0.01);
    Assert.assertNull(secondResult[1]);

    distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location", true));
    hibQuery.setSort(distanceSort);
    results = hibQuery.getResultList();
    firstResult = (Object[]) results.get(0);
    secondResult = (Object[]) results.get(1);
    Assert.assertNull(firstResult[1]);
    Assert.assertEquals(10.1582, (Double) secondResult[1], 0.01);

    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueryingJPATest.java

License:LGPL

@Test
@TestForIssue(jiraKey = "HSEARCH-2322")
public void testDistanceSortMissingCoordinatesWholeSegment() throws Exception {
    POI poi = new POI(0, "Distance to 24,32 : 10.16km", 24.0d, 31.9d, "");
    POI poi2 = new POI(1, "Distance to 24,32 : unknown, 4361.00km if interpreted as 0,0", null, null, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();//from   w ww .ja  v  a2  s. c o m
    em.persist(poi);
    em.getTransaction().commit();

    /*
     * Create the POI with a missing value in a separate transaction, so that
     * the document will be alone in one segment (or so it seems...?)
     */
    em.getTransaction().begin();
    em.persist(poi2);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.all().createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    hibQuery.setSpatialParameters(centerLatitude, centerLongitude, "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Assert.assertEquals(10.1582, (Double) firstResult[1], 0.01);
    Assert.assertNull(secondResult[1]);

    distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location", true));
    hibQuery.setSort(distanceSort);
    results = hibQuery.getResultList();
    firstResult = (Object[]) results.get(0);
    secondResult = (Object[]) results.get(1);
    Assert.assertNull(firstResult[1]);
    Assert.assertEquals(10.1582, (Double) secondResult[1], 0.01);

    em.close();
}

From source file:org.hibernate.search.test.jpa.SpatialQueryingJPATest.java

License:LGPL

@Test
public void testDistanceSortWithMaxResult() throws Exception {
    POI poi = new POI(1, "Distance to 24,32 : 0", 24.0d, 32.0d, "");
    POI poi2 = new POI(2, "Distance to 24,32 : 10.16", 24.0d, 31.9d, "");
    POI poi3 = new POI(3, "Distance to 24,32 : 11.12", 23.9d, 32.0d, "");
    POI poi4 = new POI(4, "Distance to 24,32 : 15.06", 23.9d, 32.1d, "");
    POI poi5 = new POI(5, "Distance to 24,32 : 22.24", 24.2d, 32.0d, "");
    POI poi6 = new POI(6, "Distance to 24,32 : 24.45", 24.2d, 31.9d, "");

    FullTextEntityManager em = Search.getFullTextEntityManager(factory.createEntityManager());

    em.getTransaction().begin();/*  w w w. j  a  v  a  2 s .c om*/
    em.persist(poi);
    em.persist(poi2);
    em.persist(poi3);
    em.getTransaction().commit();
    em.getTransaction().begin();
    em.persist(poi4);
    em.persist(poi5);
    em.persist(poi6);
    em.getTransaction().commit();

    em.getTransaction().begin();
    double centerLatitude = 24.0d;
    double centerLongitude = 32.0d;

    final QueryBuilder builder = em.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    org.apache.lucene.search.Query luceneQuery = builder.spatial().onField("location").within(100, Unit.KM)
            .ofLatitude(centerLatitude).andLongitude(centerLongitude).createQuery();

    FullTextQuery hibQuery = em.createFullTextQuery(luceneQuery, POI.class);
    Sort distanceSort = new Sort(new DistanceSortField(centerLatitude, centerLongitude, "location"));
    hibQuery.setSort(distanceSort);
    hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SPATIAL_DISTANCE);
    // Set max results to 3 when 6 documents are stored:
    hibQuery.setMaxResults(3);
    hibQuery.setSpatialParameters(centerLatitude, centerLongitude, "location");
    List results = hibQuery.getResultList();
    Object[] firstResult = (Object[]) results.get(0);
    Object[] secondResult = (Object[]) results.get(1);
    Object[] thirdResult = (Object[]) results.get(2);
    Assert.assertEquals(0.0, (Double) firstResult[1], 0.01);
    Assert.assertEquals(10.1582, (Double) secondResult[1], 0.01);
    Assert.assertEquals(11.1195, (Double) thirdResult[1], 0.01);

    em.close();
}

From source file:org.hibernate.search.test.performance.task.QueryBooksByAverageRatingTask.java

License:LGPL

@Override
@SuppressWarnings({ "unchecked", "unused" })
protected void execute(FullTextSession fts) {
    Query q = fts.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get().range().onField("rating")
            .from(40.0f).to(60.0f).createQuery();

    List<Book> result = fts.createFullTextQuery(q, Book.class)
            .setSort(new Sort(new SortField("rating", SortField.Type.FLOAT, true))).list();
}