2

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?

freedev
  • 25,946
  • 8
  • 108
  • 125
ypriverol
  • 585
  • 2
  • 8
  • 28
  • I'm not 100% sure what you mean. The `qf` parameter specifies which fields to search in, you can add this at query time. You can also define this parameter in the search handler in `solrconfig.xml`. You can use the [Solr Config API](https://lucene.apache.org/solr/guide/7_3/config-api.html#config-api) to update the configuration, though you shouldn't be doing that in response to queries. If the query fields are going to depend on the search, then use the `qf` parameter at query time. – Jayce444 May 10 '18 at 13:58
  • Thanks @Jayce444 for your quick response. I know that I can query using the df field. I would like to give solr a default field for search as you mentioned. This field will enable me to pass raw text to solr and get at least something. I know that this field before was defined in the ```solrconfig.xml``` however I would like to change that field using the Solr API but I can't find a good example about how to do it. Thanks in advance – ypriverol May 10 '18 at 14:05
  • 1
    Did you look at the Solr Config API documentation I linked in my first comment? I hyperlinked the text. It's got a full example of how to update a request handler's parameter using the Config API, search the page for `update-requesthandler` and you'll find the curl command (note that that's the documentation for Solr 7.3, make sure to consult the matching documentation if you're using a different version) – Jayce444 May 10 '18 at 14:35

2 Answers2

3

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

freedev
  • 25,946
  • 8
  • 108
  • 125
  • Thanks, @freedev . do you know how to change this using the Solr API calls. – ypriverol May 11 '18 at 10:27
  • @ypriverol I've updated my answer but in my experience, modify only the the `solrconfig.xml` would lead to a more clear configuration in the long term period – freedev May 11 '18 at 11:01
  • Agree with you @freedev. However.. I'm trying to use more and more the API because I can control almost the entire solr instalation by code without needs to copy files, login into my servers etc – ypriverol May 11 '18 at 11:12
  • thanks for your reply. I have changed the parameters using the API. Do you know how to change the uniqueKey by API? – ypriverol May 11 '18 at 12:26
  • @ypriverol very likely you should try [schema api](https://lucene.apache.org/solr/guide/7_3/schema-api.html) – freedev May 11 '18 at 12:43
  • It looks like is not supported by the API – ypriverol May 11 '18 at 13:28
  • looks like the uniqueKey can't be changed using the API. – ypriverol May 14 '18 at 16:30
2

You can update the properties like below using Java code. Ex. you would like update the autoCommit and autoSoftCommit property.

Map<String, String> props= new HashMap<>();
props.put("solr.autoCommit.maxTime", 10000);
props.put("solr.autoSoftCommit.maxTime", 15000);

StringBuilder command = new StringBuilder("{\"set-property\": {");
   for (Map.Entry<String, String> entry: props.entrySet())
   {
      command.append('"').append(entry.getKey()).append('"').append(':');
      command.append(entry.getValue()).append(',');
   }
   command.setLength(command.length()-1); // remove last comma
   command.append("}}");

   GenericSolrRequest rq = new GenericSolrRequest(SolrRequest.METHOD.POST, "/config", null);
   ContentStream content = new ContentStreamBase.StringStream(command.toString());
   rq.setContentStreams(Collections.singleton(content));
   rq.process(solrClient);
Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47