Пример #1
0
  public void testFaceting() throws Exception {
    QueryBuilder queryBuilder = qf.buildQueryBuilderForClass(Car.class).get();

    FacetingRequest request =
        queryBuilder
            .facet()
            .name(facetName)
            .onField(indexFieldName)
            .discrete()
            .createFacetingRequest();

    Query luceneQuery = queryBuilder.all().createQuery();

    CacheQuery query = qf.getQuery(luceneQuery);

    query.getFacetManager().enableFaceting(request);

    List<Facet> facetList = query.getFacetManager().getFacets(facetName);

    assertEquals("Wrong number of facets", 2, facetList.size());

    assertEquals("4000", facetList.get(0).getValue());
    assertEquals(2, facetList.get(0).getCount());
    assertEquals(indexFieldName, facetList.get(0).getFieldName());

    assertEquals("2341", facetList.get(1).getValue());
    assertEquals(1, facetList.get(1).getCount());
    assertEquals(indexFieldName, facetList.get(1).getFieldName());
  }
Пример #2
0
  public static void FacetTest() {
    Session session = null;
    FullTextSession fullTextSession = null;

    SessionFactory sessionFactory = null;

    try {
      sessionFactory =
          new Configuration().configure(hibernateConfigurationFile).buildSessionFactory();

      session = sessionFactory.openSession();
      session.beginTransaction();
      fullTextSession = Search.getFullTextSession(session);

      org.apache.lucene.search.Query luceneQuery;

      FullTextQuery hibQuery;

      Point center = Point.fromDegrees(46, 4);
      double radius = 50.0d;

      luceneQuery =
          SpatialQueryBuilderFromPoint.buildSpatialQueryByGrid(center, radius, "location");
      hibQuery = fullTextSession.createFullTextQuery(luceneQuery, POI.class);
      hibQuery.setProjection("id", "name", "type");

      FacetManager facetManager = hibQuery.getFacetManager();

      QueryBuilder queryBuilder =
          fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();

      FacetingRequest facetingRequest =
          queryBuilder
              .facet()
              .name("typeFacet")
              .onField("type")
              .discrete()
              .orderedBy(FacetSortOrder.COUNT_DESC)
              .includeZeroCounts(false)
              .createFacetingRequest();

      facetManager.enableFaceting(facetingRequest);

      Integer size = hibQuery.getResultSize();

      List list = hibQuery.list();

      List<Facet> facets = facetManager.getFacets("typeFacet");

      System.out.println(facets);

      session.getTransaction().commit();
      session.close();
      sessionFactory.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fullTextSession != null && fullTextSession.isOpen()) {
        Transaction transaction = fullTextSession.getTransaction();
        if (transaction != null && transaction.isActive()) {
          transaction.rollback();
        }
        fullTextSession.close();
      }
      if (sessionFactory != null && !sessionFactory.isClosed()) {
        sessionFactory.close();
      }
    }
  }