Calling Solr Scheme API using Solrj

99封情书 提交于 2020-05-13 09:44:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!