问题
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 ?
回答1:
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:
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();
来源:https://stackoverflow.com/questions/40214460/calling-solr-scheme-api-using-solrj