0

I'm trying to find if the expiry date falls between today and another date specified. Though my database has results, the search query doesn't seem to fetch the value. Please help!

This is the query that's being run: jpaQuery: FullTextQueryImpl(+membershipStatus:full_member +accountManager.firstName:nikki +expiryDate:[20190416 TO 20190516})

Also, database has expiry date in YYYY-MM-dd HH:mm:ss format, not sure how to match the fields.

This is the query:

Query expiringInQuery = queryBuilder
    .range()
    .onField("expiryDate")
    .ignoreFieldBridge()
    .from(DateTools.dateToString(today, DateTools.Resolution.DAY)) 
    .to(DateTools.dateToString(expiringDate, DateTools.Resolution.DAY))
    .excludeLimit()
    .createQuery();

This is on the entity:

@Field
@Column(name = "expiryDate")
@Temporal(TemporalType.TIMESTAMP)
@JsonIgnore
@IndexedEmbedded
@DateBridge(resolution=org.hibernate.search.annotations.Resolution.DAY, encoding = EncodingType.STRING)
private Date expiryDate;
sh.seo
  • 1,482
  • 13
  • 20

2 Answers2

0

If you stored them in a date field, look here: https://lucene.apache.org/solr/guide/6_6/working-with-dates.html

Thus the correct syntax would be something like (u might be able to leave out the time)

datefield:[2019-04-16T00:00:00.000Z TO 2019-05-16T23:59:59.999Z]

e.g DateRangeField also allows queries like

[2014 TO 2014-12-01]

check out the link.

awagen
  • 236
  • 2
  • 8
0

This is what finally worked.

 String expiringIn = (inputsMap.get("expiringIn").equals("None")) ? "*" : 
 (inputsMap.get("expiringIn"));
 Date today = DateUtils.truncate(new Date(), Calendar.DATE);

 Query luceneQuery = queryBuilder
            .range()
            .onField("expiryDate")
            .from(today)
            .to(expiringDate)
            .createQuery();

On the entity,

@Field
@Column(name = "expiryDate")
@Temporal(TemporalType.TIMESTAMP)
@JsonIgnore
@IndexedEmbedded
@DateBridge(resolution = org.hibernate.search.annotations.Resolution.MILLISECOND, 
encoding = EncodingType.STRING)
private Date expiryDate;