Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search between dates (Hibernate Search)?

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.

like image 544
Dangling Piyush Avatar asked Dec 15 '22 18:12

Dangling Piyush


1 Answers

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.

like image 82
Hardy Avatar answered Jan 01 '23 05:01

Hardy