How to search between dates (Hibernate Search)?

Dangling Piyush picture Dangling Piyush · Oct 10, 2012 · Viewed 9.6k times · Source

I am wondering how I can search between by dates in Hibernate Search using Range-Query or is there any filter I have to implement.Following is my field in Record Entity

    /**
     * When the analysis started.
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Field(index = Index.UN_TOKENIZED)
    @DateBridge(resolution = Resolution.MILLISECOND)
    private Date startTS;

My requirment is to find the records analysed between a two dates eg. 11/11/2011 to 11/11/2012.I am confused how to do this.

Answer

Hardy picture Hardy · Oct 10, 2012

You should use a range query using from and to.

query = monthQb
        .range()
            .onField( "startTS" ).ignoreFieldBridge()
            .from( DateTools.dateToString( from, DateTools.Resolution.MILLISECOND ) )
            .to( DateTools.dateToString( to, DateTools.Resolution.MILLISECOND ) ).excludeLimit()
            .createQuery();

The ignoreFieldBridge is needed since you create the string based search string yourself using DateTools.