3
votes

Based on documentation https://cwiki.apache.org/confluence/display/solr/Schema+API

I want to call Solr Scheme API using Solrj. Following is curl command that i want to call from SolrJ

curl -X POST -H 'Content-type:application/json' --data-binary '{
  "add-field":{
     "name":"sell-by",
     "type":"tdate",
     "stored":true }
}' http://localhost:8983/solr/gettingstarted/schema

Is there a way to call using SolrJ ?

2
which version of solrj you have? - freedev

2 Answers

4
votes

This should do it:

    String urlString = "http://localhost:8983/solr/gettingstarted";
    SolrClient solr = new HttpSolrClient.Builder(urlString).build();


    Map<String, Object> fieldAttributes = new LinkedHashMap<>();
    fieldAttributes.put("name", "sell-by");
    fieldAttributes.put("type", "tdate");
    fieldAttributes.put("stored", true);

    SchemaRequest.AddField addFieldUpdateSchemaRequest =
            new SchemaRequest.AddField(fieldAttributes);
    SchemaResponse.UpdateResponse addFieldResponse = addFieldUpdateSchemaRequest.process(solr);

Some other code samples on working with Solr Schema API can be found in SchemaTest.java file.

2
votes

Another way to do it is via SchemaAPI in SolrJ (since Solr 5.3):

CloudSolrClient client = ...;
SchemaRequest request = new SchemaRequest();
SchemaResponse response = request.process(client, "collectionName");
SchemaRepresentation schema = response.getSchemaRepresentation();