简体   繁体   中英

Calling Solr Scheme API using Solrj

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 ?

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM