将json插入solr 5.5

时间:2017-05-16 06:43:08

标签: java solr solrj

以下是我的代码:

HttpPost postRequest = new HttpPost("http://localhost:8983/solr/core1/update/json?wt=json&commit=true");

StringEntity entity = new StringEntity("{\n"
                                + "   \"id\": \"1\",\n"
                                + "   \"title\": \"Doc 1\"\n"
                                + "}", "UTF-8");

entity.setContentType("application/json");
postRequest.setEntity(entity);
HttpResponse response = httpClient.execute(postRequest);

但是当我尝试将Json添加到Solr 5.5.0时,会引发以下异常:

{"responseHeader":{"status":400,"QTime":2},"error":{"metadata":["error-class","org.apache.solr.common.SolrException","root-error-class","org.apache.solr.common.SolrException"],"msg":"Unknown command 'id' at [9]","code":400}}

2 个答案:

答案 0 :(得分:1)

在不同的Solr版本中,有一些关于处理json的更改/错误修正,但是最好的方法就是提示msg给你的错误,并采取相应的行动。

Solr期待一个'命令',所以你应该把你的doc放在'add'元素中,这样String就像这样:

{
  "add": {
   "doc": {
    "id": "1",
    "title":"Doc 1"
    }
   }
}

那应该有用

答案 1 :(得分:0)

您可以尝试以下内容。忽略那些“\ n”。你不需要它们。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://solrHost.com:8983/solr/update/json");
StringEntity input = new StringEntity("{\"firstName\":\"Hello\",\"lastName\":\"World\"}");
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);