1. Overview

Solr provide facility to set default search field, the user can set most used field in default search field to avoid pass field name in every search request.

2. Configuration

Need to set default search field in solrconfig.xml file under /query request handler or as IntParams.Let’s look at an example for better understanding.

<requestHandler name="/query" class="solr.SearchHandler">
   <lst name="defaults">
     <str name="echoParams">explicit</str>
     <str name="wt">json</str>
     <str name="indent">true</str>
     <str name="df">name</str>
<initParams path="/update/**,/query,/select,/tvrh,/elevate,/spell,/browse">
  <lst name="defaults">
    <str name="df">name</str>

Here we have set name as a default search field for our techproduct example.

3. Example

We have set name field as a default search field so now no need to pass field name if we want to search in that field.We can also search in a different field other than default search field by specifying that field names.

3.1. Query

  q=Samsung SpinPoint OR manu:Samsung

3.2 debug Query Output

Here you can see solr consider default search field if you haven’t specified any field name in a query

    "rawquerystring":"Samsung SpinPoint OR manu:Samsung",
    "querystring":"Samsung SpinPoint OR manu:Samsung",
    "parsedquery":"name:samsung name:spinpoint manu:samsung",
    "parsedquery_toString":"name:samsung name:spinpoint manu:samsung"}}}

3.3 Output

      "q":"Samsung SpinPoint OR manu:Samsung",
        "name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
        "manu":"Samsung Electronics Co. Ltd.",
          "hard drive"],
        "features":["7200RPM, 8MB cache, IDE Ultra ATA-133",
          "NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor"],

4. Conclusion

In this article, we have discussed how to set default search field in a solr with an example.

5. References

Refer Solr Reference GuideSolr Multiple Filter QueriesMain Query vs Filter Query , Solr Fuzzy SearchSolr Proximity Search for more details.

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

Leave a Reply

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