In previous two posts Compare two string fields and compare-two-date-fields we have discuss about comparing string and date fields. In this article we are going to discuss how we can check two numeric fields like int , float , long.

To compare two numeric 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. if function

if() function enable function queries.We can check any condition like other programming language.

Signature

if(test,value1,value2)

test is or refers to a logical value or expression that returns a logical value (TRUE or FALSE).
value1 is the value that is returned by the function if test yields TRUE.
value2 is the value that is returned by the function if test yields FALSE.

Example

if(language==”java”, 100, if(language==”python”, 50, 25))

This function will check the document field “language”, and if it is “java” return 100, if it is “python” return 50, else return 25.

 3. Boolean functions

Solr provide five comparison functions eq, lt, lte, gt and gte for equality check as below.

Steps

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

Step 1: Add fields

Add any two int fields called popularity_int_1,popularity_int_1. Also add one uniq field called id.

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="popularity_int_1" type="int" indexed="true" stored="true"/>
<field name="popularity_int_2" type="int" 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,popularity_int_1,popularity_int_2
101,1,1
102,100,200
103,300,200
104,99,99

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

check numbers are equals

To fetch documents which have popularity_int_1 and popularity_int_2 fields have same value.

{!frange l=1 u=1}if(eq(popularity_int_1,popularity_int_2),1,0)

numeric fields same value

check numbers are not equals

Use below query to fetch documents which does not have the same value

{!frange l=0 u=0}if(eq(popularity_int_1,popularity_int_2),1,0)

numeric fields diff value

check first number is less than second number

Use below query to check first number is less than second number.

{!frange l=1 u=1}if(lt(popularity_int_1,popularity_int_2),1,0)

numeric fields lt value

check first number is greater than second number

Use below query to check first number is greater than second number.

{!frange l=1 u=1}if(gt(popularity_int_1,popularity_int_2),1,0)

numeric fields gt value

Refer Function Querycompare two date fields , 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 *