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>
   </lst>
</requestHandler>
<initParams path="/update/**,/query,/select,/tvrh,/elevate,/spell,/browse">
  <lst name="defaults">
    <str name="df">name</str>
  </lst>
</initParams>

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

http://localhost:8983/solr/DefaultSearchFieldExample/select?
  debugQuery=on&
  indent=on&
  q=Samsung SpinPoint OR manu:Samsung
  &wt=json

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

"debug":{
    "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

{
  "responseHeader":{
    "status":0,
    "QTime":6,
    "params":{
      "q":"Samsung SpinPoint OR manu:Samsung",
      "indent":"on",
      "wt":"json",
      "debugQuery":"off"}},
  "response":{"numFound":1,"start":0,"docs":[
      {
        "id":"SP2514N",
        "name":"Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133",
        "manu":"Samsung Electronics Co. Ltd.",
        "manu_id_s":"samsung",
        "cat":["electronics",
          "hard drive"],
        "features":["7200RPM, 8MB cache, IDE Ultra ATA-133",
          "NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor"],
        "price":92.0,
        "price_c":"92.0,USD",
        "popularity":6,
        "inStock":true,
        "manufacturedate_dt":"2006-02-13T15:26:37Z",
        "store":"35.0752,-97.032",
        "_version_":1596475133470441472}]
  }}

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.
Yes
No

Leave a Reply

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