Ejemplo n.º 1
0
  @Test
  public void testSpatialQueries() {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder builder =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

    Coordinates coordinates = Point.fromDegrees(24d, 31.5d);
    Query query =
        builder
            .spatial()
            .onField("location")
            .within(51, Unit.KM)
            .ofCoordinates(coordinates)
            .createQuery();

    List<?> results = fullTextSession.createFullTextQuery(query, POI.class).list();

    assertEquals("test spatial hash based spatial query", 1, results.size());
    assertEquals("test spatial hash based spatial query", "Bozo", ((POI) results.get(0)).getName());

    query =
        builder
            .spatial()
            .onField("location")
            .within(500, Unit.KM)
            .ofLatitude(48.858333d)
            .andLongitude(2.294444d)
            .createQuery();
    results = fullTextSession.createFullTextQuery(query, POI.class).list();

    assertEquals("test spatial hash based spatial query", 1, results.size());
    assertEquals(
        "test spatial hash based spatial query", "Tour Eiffel", ((POI) results.get(0)).getName());

    transaction.commit();
  }
  @Test
  public void testSpatial() {
    org.hibernate.Session s = openSession();
    Transaction tx = s.beginTransaction();
    MemberLevelTestPoI memberLevelTestPoI = new MemberLevelTestPoI("test", 24.0, 32.0d);
    s.persist(memberLevelTestPoI);
    s.flush();
    tx.commit();

    tx = s.beginTransaction();
    FullTextSession session = Search.getFullTextSession(s);

    QueryBuilder builder =
        session.getSearchFactory().buildQueryBuilder().forEntity(MemberLevelTestPoI.class).get();

    double centerLatitude = 24;
    double centerLongitude = 31.5;

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

    org.hibernate.Query hibQuery =
        session.createFullTextQuery(luceneQuery, MemberLevelTestPoI.class);
    List<?> results = hibQuery.list();
    assertEquals(0, results.size());

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

    org.hibernate.Query hibQuery2 =
        session.createFullTextQuery(luceneQuery2, MemberLevelTestPoI.class);
    List<?> results2 = hibQuery2.list();
    assertEquals(1, results2.size());

    List<?> testPoIs = session.createQuery("from " + MemberLevelTestPoI.class.getName()).list();
    for (Object entity : testPoIs) {
      session.delete(entity);
    }
    tx.commit();
    session.close();

    s = openSession();
    tx = s.beginTransaction();
    ClassLevelTestPoI classLevelTestPoI = new ClassLevelTestPoI("test", 24.0, 32.0d);
    s.persist(classLevelTestPoI);
    s.flush();
    tx.commit();

    tx = s.beginTransaction();
    session = Search.getFullTextSession(s);

    builder =
        session.getSearchFactory().buildQueryBuilder().forEntity(ClassLevelTestPoI.class).get();

    centerLatitude = 24;
    centerLongitude = 31.5;

    luceneQuery =
        SpatialQueryBuilder.buildSpatialQueryByHash(
            centerLatitude, centerLongitude, 50, "location");
    hibQuery = session.createFullTextQuery(luceneQuery, ClassLevelTestPoI.class);
    results = hibQuery.list();
    assertEquals(0, results.size());

    luceneQuery2 =
        SpatialQueryBuilder.buildSpatialQueryByHash(
            centerLatitude, centerLongitude, 51, "location");
    hibQuery2 = session.createFullTextQuery(luceneQuery2, ClassLevelTestPoI.class);
    results2 = hibQuery2.list();
    assertEquals(1, results2.size());

    testPoIs = session.createQuery("from " + ClassLevelTestPoI.class.getName()).list();
    for (Object entity : testPoIs) {
      session.delete(entity);
    }
    tx.commit();
    session.close();

    s = openSession();
    tx = s.beginTransaction();
    LatLongAnnTestPoi latLongAnnTestPoi = new LatLongAnnTestPoi("test", 24.0, 32.0d);
    s.persist(latLongAnnTestPoi);
    s.flush();
    tx.commit();
    tx = s.beginTransaction();
    session = Search.getFullTextSession(s);

    builder =
        session.getSearchFactory().buildQueryBuilder().forEntity(LatLongAnnTestPoi.class).get();

    centerLatitude = 24;
    centerLongitude = 31.5;

    luceneQuery =
        builder
            .spatial()
            .onField("location")
            .within(50, Unit.KM)
            .ofLatitude(centerLatitude)
            .andLongitude(centerLongitude)
            .createQuery();

    hibQuery = session.createFullTextQuery(luceneQuery, LatLongAnnTestPoi.class);
    results = hibQuery.list();
    assertEquals(0, results.size());

    luceneQuery2 =
        builder
            .spatial()
            .onField("location")
            .within(51, Unit.KM)
            .ofLatitude(centerLatitude)
            .andLongitude(centerLongitude)
            .createQuery();

    hibQuery2 = session.createFullTextQuery(luceneQuery2, LatLongAnnTestPoi.class);
    results2 = hibQuery2.list();
    assertEquals(1, results2.size());

    testPoIs = session.createQuery("from " + LatLongAnnTestPoi.class.getName()).list();
    for (Object entity : testPoIs) {
      session.delete(entity);
    }
    tx.commit();

    session.close();
  }