0

There is:

  • Line;
  • Score;
  • Date.

Search on the line and the store is carried out as follows:

@Override
public List<Delivery> searchNewDeliveriesBy(long storeId, String text, Long selectedDateAsMills) {
    try (Session session = createSession()) {
        Store store = session.get(Store.class, storeId);
        FullTextEntityManager fullTextSession = Search.getFullTextSession(session);
        QueryBuilder qb = fullTextSession.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(Delivery.class)
                .get();
        org.apache.lucene.search.Query luceneQuery = qb.bool()
                .must(
                        qb.keyword().onFields(DELIVERY_SEARCH_FIELDS).matching(text).createQuery()
                )
                .must(
                        qb.keyword().onField("store").matching(store).createQuery()
                )
                .createQuery();
        //noinspection unchecked
        return Search.getFullTextSession(session)
                .createFullTextQuery(luceneQuery, Delivery.class)
                .setSort(sortByFieldScore)
                .list();
    }
}

My entity (without too much):

public class Delivery {
    ...
    private LocalDateTime deliveryDateMin;

    private LocalDateTime deliveryDateMax;
    ...
}

I need to add a condition so that the selected time (method variable selectedDateAsMills) is between deliveryDateMin and deliveryDateMax.

There is a class like LocalDateTimeBridge, but there are no examples of use on the Internet. This answer (How to search between dates (Hibernate Search)?) does not fit because there is java.util.Date, not java.time.LocalDateTime.

UPDATE

@Entity
@Table(name = "DELIVERY")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Cacheable
@Indexed
public class Delivery {
    @Column(name = "delivery_date_min")
    @JsonFormat(pattern = "yyyy-mm-dd HH:MM")
    @Field
    @FieldBridge(impl = LocalDateTimeBridge.class)
    private LocalDateTime deliveryDateMin;
    @Column(name = "delivery_date_max")
    @JsonFormat(pattern = "yyyy-mm-dd HH:MM")
    @Field
    @FieldBridge(impl = LocalDateTimeBridge.class)
    private LocalDateTime deliveryDateMax;
}

1 Answers1

0

Convert your number of milliseconds to a localDateTime, then just add add two range queries:

        LocalDateTime selectedDateTime = Instant.ofEpochMilli( selectedDateAsMills )
                .atOffset( ZoneOffset.UTC ) // ofEpochMilli assumes the given millis are in the UTC timezone
                .toLocalDateTime();

        org.apache.lucene.search.Query luceneQuery = qb.bool()
                .must(
                        qb.keyword().onFields(DELIVERY_SEARCH_FIELDS).matching(text).createQuery()
                )
                .must(
                        qb.keyword().onField("store").matching(store).createQuery()
                )
                .must(
                        qb.range().onField("deliveryDateMin").below(selectedDateTime).createQuery()
                )
                .must(
                        qb.range().onField("deliveryDateMax").above(selectedDateTime).createQuery()
                )
                .createQuery();

If it doesn't work for you, please explain what happens and give more information about your entity (the @Field annotations in particular).

yrodiere
  • 9,280
  • 1
  • 13
  • 35
  • As I understand it, you need to add `@Field @FieldBridge(impl = LocalDateTimeBridge.class)` annotations to an entity. Am I right? – Sadrutdin Zaynukov Jul 10 '19 at 13:30
  • Sorry, I assumed you had these annotations already but left them out of your snippet. Yes you need `@Field`, but you don't need `@FieldBridge(impl = LocalDateTimeBridge.class)`: it will be inferred automatically by Hibernate Search. – yrodiere Jul 10 '19 at 15:23