In Previous Post we have discussed about comparing two string fields. In this article we will discuss about comparing two date fields.

To compare two date fields value we will use below solr functionality:

1.  Function range query parser

Function range query parser also called frange. It will create range query over a function.

 2. ms function

ms function returns milliseconds of difference between its arguments. Arguments may be the name of an indexed TrieDateField, or date math based on a constant date or NOW.

Signatures

ms(): Equivalent to ms(NOW), number of milliseconds since the epoch.

ms(a): Returns the number of milliseconds since the epoch that the argument represents.

ms(a,b) : Returns the number of milliseconds that b occurs before a (that is, a – b)

Example

ms(NOW,mydatefield)

ms(mydatefield, 2000-01-01T00:00:00Z)

ms(datefield1, datefield2)

Steps

Please follow steps to check two date fields are equal or not.

Step 1: Add fields

Add two date type fields called creationDate,modifiedDate. Also add one uniq field called id.

<field name="creationDate" type="date" indexed="true" stored="true" />
<field name="modifiedDate" type="date" indexed="true" stored="true" />

Step 2: Indexed record

add some documents to our index. Here we have added through csv update from solr dashboard.Refer below sample data.

id,creationDate,modifiedDate
1,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,2001-01-01T00:00:00Z,2001-01-01T00:00:00Z
3,2000-01-01T00:00:00Z,2017-01-01T23:59:59Z

Now we have sample data indexed.we can fire query for compare creationDate and modifiedDate fields are equal or not.

Date fields have same value

To fetch document which have creationDate and modifiedDate fields have same value fire below query.

{!frange l=0 u=0}ms(creationDate,modifiedDate)

date same fields

Date fields have different value

To fetch documents which does not have same value of two date fields fire below query.

{!frange l=0.000001 }abs(ms(modifiedDate,creationDate))

different date value

Refer Function Query , Compare two string fields for more details.

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *