Partial string search in Apache Solr

In this tutorial, we will see a demonstration of how to configure Apache Solr for partial string search.

Create demo core via below command.

sudo su - solr -c "/opt/solr/bin/solr create -c demo -n data_driven_schema_configs"

Add test data to the demo core.

curl http://localhost:8983/solr/demo/update?commitWithin=3000 -d '[{'name':'test'},{'name':'test1'},{'name':'abc'}]'

Try to search partial string to the demo core using below command.

curl http://localhost:8983/solr/demo/query -d 'q=name:t'

Result:

{
"responseHeader":{
"status":0,
"QTime":1,
"params":{
"q":"name:t"}},
"response":{"numFound":0,"start":0,"docs":[]
}}

Number of objects found = 0

Try to search full string to the demo core using below command.

curl http://localhost:8983/solr/demo/query -d 'q=name:test'

Result:

{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"q":"name:test"}},
"response":{"numFound":1,"start":0,"docs":[
{
"name":["test"],
"id":"eed9ebdd-05e1-463c-a5fe-e31b359a166e",
"_version_":1590098186233446400}]
}}

Number of objects found = 1

Configure solr for partial string search

Add below code to the var/solr/data/demo(core name)/conf/managed-schema file.

<fieldType name="text_ngrm" class="solr.TextField" positionIncrementGap="100">

<analyzer type="index">
<tokenizer class="solr.WhitespaceTokenizerFactory"/>
<filter class="solr.NGramFilterFactory" minGramSize="1" maxGramSize="50"/>

<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.WhitespaceTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<field name="name" type="text_ngrm" indexed="true" stored="true"/>

Change type of name field to text_ngram.

Remove all documents and add it again.

curl "http://localhost:8983/solr/Demo/update?commit=true" -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'

After its done, we restart the solr.

sudo service solr restart

Try to search partial string to the demo core using below command.

curl http://localhost:8983/solr/demo/query -d 'q=name:t'

Result:

{
"responseHeader":{
"status":0,
"QTime":3,
"params":{
"q":"name:t"}},
"response":{"numFound":2,"start":0,"docs":[
{
"name":"test",
"id":"9603c801-9a0c-4076-a958-7353ca69814f",
"_version_":1593837840603545600},
{
"name":"test1",
"id":"36f94100-d094-4638-8803-1dd92f57ad56",
"_version_":1593837840712597504}]
}}

Number of objects found=2

Partial string search is working now……….

Finished…..:)

Well, this is it! I hope you liked it.