Search code examples
solrlucenesolrjsolrcloudspring-data-solr

Change default query fields in SolrCloud using the API


I'm using SolrCLoud is to search documents with multiple attributes. In my application, I would like to search over all the fields if the query does not specify any specific field such term1 AND term2 query should search for that combination in all the fields.

Reading the documentation looks like you can define a default fields for your search.

I have found examples of changing the default facets for search handler, but not for the default search fields but not for the default search fields on query handler.

Does anyone know how to use the Solr API to change the default fields in the QueryHandler?


Solution

  • You can modify your default field and default operator configuration using Config API.

    For example you can add it creating a new initParams with:

    curl http://localhost:8983/solr/films/config -H 'Content-type:application/json' -d '{
          "add-initparams" : { name : "my-init", "path" : "/select,/browse", 
               "defaults":{ "df":"term1 term2" ,"q.op":"AND" }
          } }'
    

    this configuration will be saved in the configoverlay.json.

    But I usually prefer not use the ConfigAPI and save default configuration directly in solrconfig.xml file. This, in the long term period, will lead to a more clear configuration.

    For example the following configuration is for few request handlers you have defined:

    <initParams path="/select,/get,standard">
       <lst name="defaults">
          <str name="df">term1 term2</str>
          <str name="q.op">AND</str>
       </lst>
    </initParams>
    

    As you can see I've defined the df (default fields) and q.op (default operator).

    Note that with versions of Solr (or SolrCloud), older than Solr5, these configurations parameters were in schema.xml file.

    Have a look at Major changes in Solr 7