Esempio n. 1
0
  public void testMultipleFacets() {
    final String descendingOrderedFacet = "desc";
    FacetingRequest requestDesc =
        queryBuilder(Car.class)
            .facet()
            .name(descendingOrderedFacet)
            .onField(indexFieldName)
            .discrete()
            .createFacetingRequest();

    final String ascendingOrderedFacet = "asc";
    FacetingRequest requestAsc =
        queryBuilder(Car.class)
            .facet()
            .name(ascendingOrderedFacet)
            .onField(indexFieldName)
            .discrete()
            .orderedBy(FacetSortOrder.COUNT_ASC)
            .createFacetingRequest();
    TermQuery term = new TermQuery(new Term("make", "honda"));
    FullTextQuery query = fullTextSession.createFullTextQuery(term, Car.class);
    FacetManager facetManager = query.getFacetManager();

    facetManager.enableFaceting(requestDesc);
    facetManager.enableFaceting(requestAsc);

    assertFacetCounts(facetManager.getFacets(descendingOrderedFacet), new int[] {5, 4, 4, 0});
    assertFacetCounts(facetManager.getFacets(ascendingOrderedFacet), new int[] {0, 4, 4, 5});

    facetManager.disableFaceting(descendingOrderedFacet);
    assertTrue(
        "descendingOrderedFacet should be disabled",
        query.getFacetManager().getFacets(descendingOrderedFacet).isEmpty());
    assertFacetCounts(facetManager.getFacets(ascendingOrderedFacet), new int[] {0, 4, 4, 5});

    facetManager.disableFaceting(ascendingOrderedFacet);
    assertTrue(
        "descendingOrderedFacet should be disabled",
        facetManager.getFacets(descendingOrderedFacet).isEmpty());
    assertTrue(
        "ascendingOrderedFacet should be disabled",
        facetManager.getFacets(ascendingOrderedFacet).isEmpty());
  }
  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();
      }
    }
  }