

Comparing two string fields value in Solr is not an straight forward task. Solr does not provide any direct functionality to compare two string fields.In this article we will discuss how we can compare two string fields value.
To compare string fields value we will use below solr functionality:
Table of Contents
1. Function range query parser
Function range query parser also called frange. It will create range query over a function.
2. strdist function
It will calculate distance between two strings. This function returns a float between 0 and 1 based on how similar the specified strings are to one another. Returning a value of 1 means the specified strings are identical and 0 means the string are maximally different.
Signature:
strdist(string1, string2, {jw|edit|ngram|FQN}[, ngram size])
Example:
strdist(“SOLR”,”ELASTIC”,edit)
Steps
Please follow steps to check two string fields are equal or not.
Step 1: Add fields
Add two string type fields called name_str_1,name_str_2. Also add one uniq field called id.
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name_str_1" type="string" indexed="true" stored="true"/> <field name="name_str_2" type="string" 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,name_str_1,name_str_2 1,java,java 2,language,language 3,laptop,computer
Now we have sample data indexed.we can fire query for compare name_str_1 and name_str_2 fields are equal or not.
string fields have same value
To fetch document which have name_str_1 and name_str_2 fields have same value fire below query.
{!frange l=1 u=1}strdist(name_str_1, name_str_2,edit)
It will give 2 document,Id 1 and 2 as below:
string fields have different value
To fetch documents which does not have same value of two string fields fire below query.
{!frange l=0 u=0.9999}strdist(name_str_1, name_str_2,edit)
It will return 2 documents,id 3 and 4 as below
string fields have partially equal value
To fetch documents which strings fields are 85 to 95% match fire below query
{!frange l=0.85 u=0.95}strdist(name_str_1, name_str_2,edit)
It will return one document which string fields value are computer and computar. It is used when we want to find some spelling mistake.
Refer Solr Query Parser , Function Query , strdist function for more details.