示例#1
0
  @Test
  public void testRangeQueryBelow() throws Exception {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.set(10 + 1800, 2, 12, 0, 0, 0);
    Date to = calendar.getTime();

    Query query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .below(to)
            .createQuery();

    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Month.class);
    assertEquals(1, hibQuery.getResultSize());
    assertEquals("March", ((Month) hibQuery.list().get(0)).getName());

    query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .ignoreFieldBridge()
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .below(DateTools.round(to, DateTools.Resolution.MINUTE))
            .createQuery();

    hibQuery = fullTextSession.createFullTextQuery(query, Month.class);
    assertEquals(1, hibQuery.getResultSize());
    assertEquals("March", ((Month) hibQuery.list().get(0)).getName());

    query = monthQb.range().onField("raindropInMm").below(0.24d).createQuery();

    assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class));

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

    assertEquals("test range numeric ", 1, results.size());
    assertEquals("test range numeric ", "January", ((Month) results.get(0)).getName());

    transaction.commit();
  }
  private CacheQuery createRequestLogsQuery(CapedwarfLogQuery logQuery) {
    LogQuery query = logQuery.getQuery();
    QueryBuilder queryBuilder =
        searchManager.buildQueryBuilderForClass(CapedwarfRequestLogs.class).get();
    List<Query> queries = new ArrayList<Query>();
    if (query.getStartTimeUsec() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.END_TIME_USEC)
              .above(query.getStartTimeUsec())
              .createQuery());
    }
    if (query.getEndTimeUsec() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.END_TIME_USEC)
              .below(query.getEndTimeUsec())
              .createQuery());
    }
    if (query.getMinLogLevel() != null) {
      queries.add(
          queryBuilder
              .range()
              .onField(CapedwarfRequestLogs.MAX_LOG_LEVEL)
              .above(query.getMinLogLevel().ordinal())
              .createQuery());
    }

    boolean onlyCompleteRequests = !Boolean.TRUE.equals(query.getIncludeIncomplete());
    if (onlyCompleteRequests) {
      queries.add(
          queryBuilder
              .keyword()
              .onField(CapedwarfRequestLogs.FINISHED)
              .matching(Boolean.TRUE)
              .createQuery());
    }

    Query luceneQuery = getQuery(queryBuilder, queries);
    CacheQuery cacheQuery = searchManager.getQuery(luceneQuery, CapedwarfRequestLogs.class);
    cacheQuery.sort(
        new Sort(new SortField(CapedwarfRequestLogs.END_TIME_USEC, SortField.LONG, true)));
    cacheQuery.firstResult(logQuery.getOptions().getOffset());
    if (logQuery.getOptions().getLimit() != null) {
      cacheQuery.maxResults(logQuery.getOptions().getLimit());
    }
    return cacheQuery;
  }
示例#3
0
  @Test
  public void testRangeQueryFromTo() throws Exception {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.set(1900, 2, 12, 0, 0, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date from = calendar.getTime();
    calendar.set(1910, 2, 12, 0, 0, 0);
    Date to = calendar.getTime();

    Query query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .from(from)
            .to(to)
            .excludeLimit()
            .createQuery();

    assertEquals(1, fullTextSession.createFullTextQuery(query, Month.class).getResultSize());

    query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .ignoreFieldBridge()
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .from(DateTools.round(from, DateTools.Resolution.MINUTE))
            .to(DateTools.round(to, DateTools.Resolution.MINUTE))
            .excludeLimit()
            .createQuery();
    assertEquals(1, fullTextSession.createFullTextQuery(query, Month.class).getResultSize());
    transaction.commit();
  }
示例#4
0
  @Test
  public void testRangeQueryAbove() throws Exception {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.set(10 + 1900, 2, 12, 0, 0, 0);
    Date to = calendar.getTime();

    Query query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .above(to)
            .createQuery();
    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Month.class);
    assertEquals(1, hibQuery.getResultSize());
    assertEquals("February", ((Month) hibQuery.list().get(0)).getName());

    query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .ignoreFieldBridge()
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .above(DateTools.round(to, DateTools.Resolution.MINUTE))
            .createQuery();
    hibQuery = fullTextSession.createFullTextQuery(query, Month.class);
    assertEquals(1, hibQuery.getResultSize());
    assertEquals("February", ((Month) hibQuery.list().get(0)).getName());

    transaction.commit();
  }
示例#5
0
  @Test
  @TestForIssue(jiraKey = "HSEARCH-1378")
  public void testNumericRangeQueryBelow() {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    // inclusive
    Query query = monthQb.range().onField("raindropInMm").below(0.435d).createQuery();

    assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class));

    List<?> results = fullTextSession.createFullTextQuery(query, Month.class).list();
    assertThat(results).onProperty("name").containsOnly("January", "February", "March");

    // exclusive
    query = monthQb.range().onField("raindropInMm").below(0.435d).excludeLimit().createQuery();

    results = fullTextSession.createFullTextQuery(query, Month.class).list();
    assertThat(results).onProperty("name").containsOnly("January");

    transaction.commit();
  }
示例#6
0
  private static Query createRangeBelowQuery(
      Column column,
      Object value,
      BooleanJunction<BooleanJunction> junction,
      QueryBuilder queryBuilder) {

    Query queryKey =
        queryBuilder
            .range()
            .onField(ObjectExecution.getNameInSource(column))
            .below(value)
            .excludeLimit()
            .createQuery();
    junction.must(queryKey);
    return queryKey;
  }
示例#7
0
  @Test
  public void testNumericRangeQueries() {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    Query query = monthQb.range().onField("raindropInMm").from(0.23d).to(0.24d).createQuery();

    assertTrue(query.getClass().isAssignableFrom(NumericRangeQuery.class));

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

    assertEquals("test range numeric ", 1, results.size());
    assertEquals("test range numeric ", "January", ((Month) results.get(0)).getName());

    transaction.commit();
  }
示例#8
0
  @Test
  public void testRangeQueryAboveExclusive() throws Exception {
    Transaction transaction = fullTextSession.beginTransaction();
    final QueryBuilder monthQb =
        fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Month.class).get();

    // test the limits, exclusive
    Query query =
        monthQb
            .range()
            .onField("estimatedCreation")
            .andField("justfortest")
            .ignoreFieldBridge()
            .ignoreAnalyzer()
            .above(february)
            .excludeLimit()
            .createQuery();
    FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Month.class);
    assertEquals(0, hibQuery.getResultSize());

    transaction.commit();
  }
  public static void Bench() {
    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);

      long gridTotalDuration = 0;
      long spatialTotalDuration = 0;
      long doubleRangeTotalDuration = 0;
      long distanceDoubleRangeTotalDuration = 0;

      long gridDocsFetched = 0;
      long spatialDocsFetched = 0;
      long doubleRangeDocsFetched = 0;
      long distanceDoubleRangeDocsFetched = 0;

      org.apache.lucene.search.Query luceneQuery;
      long startTime, endTime, duration;
      FullTextQuery hibQuery;
      List gridResults, rangeResults;
      final QueryBuilder queryBuilder =
          fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(POI.class).get();
      org.apache.lucene.search.Query query;
      final Integer iterations = 2000;
      final Integer warmUp = 50;
      Random random = new Random(42);

      for (int i = 0; i < iterations; i++) {
        Point center = Point.fromDegrees(random.nextDouble() * 2 + 44, random.nextDouble() * 2 + 3);
        double radius = 25.0d;
        Rectangle boundingBox = Rectangle.fromBoundingCircle(center, radius);

        query =
            queryBuilder
                .bool()
                .must(
                    queryBuilder
                        .range()
                        .onField("latitude")
                        .from(boundingBox.getLowerLeft().getLatitude())
                        .to(boundingBox.getUpperRight().getLatitude())
                        .createQuery())
                .must(
                    queryBuilder
                        .range()
                        .onField("longitude")
                        .from(boundingBox.getLowerLeft().getLongitude())
                        .to(boundingBox.getUpperRight().getLongitude())
                        .createQuery())
                .createQuery();
        hibQuery = fullTextSession.createFullTextQuery(query, POI.class);
        hibQuery.setProjection("id", "name");
        startTime = System.nanoTime();
        try {
          doubleRangeDocsFetched += hibQuery.getResultSize();
        } finally {
          endTime = System.nanoTime();
        }
        duration = endTime - startTime;
        if (i > warmUp) {
          doubleRangeTotalDuration += duration;
        }
        session.clear();

        query =
            queryBuilder
                .bool()
                .must(
                    queryBuilder
                        .range()
                        .onField("latitude")
                        .from(boundingBox.getLowerLeft().getLatitude())
                        .to(boundingBox.getUpperRight().getLatitude())
                        .createQuery())
                .must(
                    queryBuilder
                        .range()
                        .onField("longitude")
                        .from(boundingBox.getLowerLeft().getLongitude())
                        .to(boundingBox.getUpperRight().getLongitude())
                        .createQuery())
                .createQuery();
        org.apache.lucene.search.Query filteredQuery =
            new ConstantScoreQuery(
                SpatialQueryBuilderFromPoint.buildDistanceFilter(
                    new QueryWrapperFilter(query), center, radius, "location"));
        hibQuery = fullTextSession.createFullTextQuery(filteredQuery, POI.class);
        hibQuery.setProjection("id", "name");
        startTime = System.nanoTime();
        try {
          distanceDoubleRangeDocsFetched += hibQuery.getResultSize();
        } finally {
          endTime = System.nanoTime();
        }
        duration = endTime - startTime;
        if (i > warmUp) {
          distanceDoubleRangeTotalDuration += duration;
        }
        rangeResults = hibQuery.list();
        session.clear();

        luceneQuery = SpatialQueryBuilderFromPoint.buildGridQuery(center, radius, "location");
        hibQuery = fullTextSession.createFullTextQuery(luceneQuery, POI.class);
        hibQuery.setProjection("id", "name");
        startTime = System.nanoTime();

        try {
          gridDocsFetched += hibQuery.getResultSize();
        } finally {
          endTime = System.nanoTime();
        }
        duration = endTime - startTime;
        if (i > warmUp) {
          gridTotalDuration += duration;
        }
        session.clear();

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

        try {
          spatialDocsFetched += hibQuery.getResultSize();
        } finally {
          endTime = System.nanoTime();
        }
        duration = endTime - startTime;
        if (i > warmUp) {
          spatialTotalDuration += duration;
        }
        gridResults = hibQuery.list();
        session.clear();

        if (rangeResults.size() != gridResults.size()) {
          luceneQuery = SpatialQueryBuilderFromPoint.buildDistanceQuery(center, radius, "location");
          hibQuery = fullTextSession.createFullTextQuery(luceneQuery, POI.class);
          hibQuery.setProjection("id", "name");

          System.out.println(
              ">>>>> Different numbers of documents fetched for point ("
                  + Double.toString(center.getLatitude())
                  + ","
                  + Double.toString(center.getLongitude())
                  + ") and radius "
                  + Double.toString(radius));
          System.out.println("Range results : " + rangeResults);
          System.out.println("Grid results : " + gridResults);
          System.out.println("Pure distance results : " + hibQuery.getResultSize());

          List<Integer> rangeIds = new ArrayList<Integer>();
          for (int index = 0; index < rangeResults.size(); index++) {
            rangeIds.add((Integer) ((Object[]) rangeResults.get(index))[0]);
          }
          List<Integer> gridIds = new ArrayList<Integer>();
          for (int index = 0; index < gridResults.size(); index++) {
            gridIds.add((Integer) ((Object[]) gridResults.get(index))[0]);
          }

          rangeIds.removeAll(gridIds);

          System.out.println("Missing Ids : " + rangeIds);
        }
      }
      session.getTransaction().commit();
      session.close();
      sessionFactory.close();

      System.out.println(
          "Mean time with Grid : "
              + Double.toString(
                  (double) gridTotalDuration * Math.pow(10, -6) / (iterations - warmUp))
              + " ms. Average number of docs  fetched : "
              + Double.toString(gridDocsFetched / ((iterations - warmUp) * 1.0d)));
      System.out.println(
          "Mean time with Grid + Distance filter : "
              + Double.toString(
                  (double) spatialTotalDuration * Math.pow(10, -6) / (iterations - warmUp))
              + " ms. Average number of docs  fetched : "
              + Double.toString(spatialDocsFetched / ((iterations - warmUp) * 1.0d)));
      System.out.println(
          "Mean time with DoubleRange : "
              + Double.toString(
                  (double) doubleRangeTotalDuration * Math.pow(10, -6) / (iterations - warmUp))
              + " ms. Average number of docs  fetched : "
              + Double.toString(doubleRangeDocsFetched / ((iterations - warmUp) * 1.0d)));
      System.out.println(
          "Mean time with DoubleRange + Distance filter : "
              + Double.toString(
                  (double) distanceDoubleRangeTotalDuration
                      * Math.pow(10, -6)
                      / (iterations - warmUp))
              + " ms. Average number of docs  fetched : "
              + Double.toString(distanceDoubleRangeDocsFetched / ((iterations - warmUp) * 1.0d)));

    } 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();
      }
    }
  }
  private FullTextQuery buildFullTextQuery(
      ArticleSearchRequest request, Pageable pageable, Criteria criteria) {
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
    QueryBuilder qb =
        fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Article.class).get();

    @SuppressWarnings("rawtypes")
    BooleanJunction<BooleanJunction> junction = qb.bool();
    junction.must(qb.all().createQuery());

    junction.must(
        qb.keyword().onField("drafted").ignoreAnalyzer().matching("_null_").createQuery());

    if (StringUtils.hasText(request.getKeyword())) {
      Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms");
      String[] fields =
          new String[] {
            "title", "body",
            "categories.name", "tags.name",
          };
      MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer);
      parser.setDefaultOperator(QueryParser.Operator.AND);
      Query query = null;
      try {
        query = parser.parse(request.getKeyword());
      } catch (ParseException e1) {
        try {
          query = parser.parse(QueryParser.escape(request.getKeyword()));
        } catch (ParseException e2) {
          throw new RuntimeException(e2);
        }
      }
      junction.must(query);
    }
    if (request.getStatus() != null) {
      junction.must(qb.keyword().onField("status").matching(request.getStatus()).createQuery());
    }
    if (StringUtils.hasText(request.getLanguage())) {
      junction.must(qb.keyword().onField("language").matching(request.getLanguage()).createQuery());
    }

    if (request.getDateFrom() != null) {
      junction.must(qb.range().onField("date").above(request.getDateFrom()).createQuery());
    }
    if (request.getDateTo() != null) {
      junction.must(qb.range().onField("date").below(request.getDateTo()).createQuery());
    }

    if (!CollectionUtils.isEmpty(request.getCategoryIds())) {
      BooleanJunction<BooleanJunction> subJunction = qb.bool();
      for (long categoryId : request.getCategoryIds()) {
        subJunction.should(
            qb.keyword().onField("categories.id").matching(categoryId).createQuery());
      }
      junction.must(subJunction.createQuery());
    }
    if (!CollectionUtils.isEmpty(request.getCategoryCodes())) {
      BooleanJunction<BooleanJunction> subJunction = qb.bool();
      for (String categoryCode : request.getCategoryCodes()) {
        subJunction.should(
            qb.keyword().onField("categories.code").matching(categoryCode).createQuery());
      }
      junction.must(subJunction.createQuery());
    }

    if (!CollectionUtils.isEmpty(request.getTagIds())) {
      BooleanJunction<BooleanJunction> subJunction = qb.bool();
      for (long tagId : request.getTagIds()) {
        subJunction.should(qb.keyword().onField("tags.id").matching(tagId).createQuery());
      }
      junction.must(subJunction.createQuery());
    }
    if (!CollectionUtils.isEmpty(request.getTagNames())) {
      BooleanJunction<BooleanJunction> subJunction = qb.bool();
      for (String tagName : request.getTagNames()) {
        subJunction.should(qb.phrase().onField("tags.name").sentence(tagName).createQuery());
      }
      junction.must(subJunction.createQuery());
    }

    if (request.getAuthorId() != null) {
      junction.must(
          qb.keyword().onField("author.id").matching(request.getAuthorId()).createQuery());
    }

    Query searchQuery = junction.createQuery();

    Sort sort =
        new Sort(
            new SortField("date", SortField.Type.STRING, true),
            new SortField("id", SortField.Type.LONG, true));

    FullTextQuery persistenceQuery =
        fullTextEntityManager
            .createFullTextQuery(searchQuery, Article.class)
            .setCriteriaQuery(criteria)
            .setSort(sort);
    if (pageable != null) {
      persistenceQuery.setFirstResult(pageable.getOffset());
      persistenceQuery.setMaxResults(pageable.getPageSize());
    }
    return persistenceQuery;
  }