简体   繁体   English

从Java向solr添加json数据

[英]Add json data to solr from java

I want to add data to solr 4.0 using java. 我想使用Java将数据添加到solr 4.0 I have the following array List<Map<String, Object>> docs = new ArrayList<>(); 我有以下数组List<Map<String, Object>> docs = new ArrayList<>(); . I am converting the array to json object using GSON method. 我正在使用GSON方法将数组转换为json对象。 I want to commit this data to solr. 我想将此数据提交到solr。 How do i do that? 我怎么做? I have read solrj but not getting idea how to get it to work. 我读过solrj,但不知道如何使它工作。

With the solrj client you create SolrInputDocument objects and post them to SolrServer instance be it a HttpSolrServer or a EmbeddedSolrServer. 使用solrj客户端,您可以创建SolrInputDocument对象,并将其发布到SolrServer实例(无论是HttpSolrServer还是EmbeddedSolrServer)。 The SolrInputDocument is a name value pair collection the would be equivalent to the json you are trying to post. SolrInputDocument是一个名称值对集合,它等效于您尝试发布的json。 As described at https://wiki.apache.org/solr/Solrj#Adding_Data_to_Solr https://wiki.apache.org/solr/Solrj#Adding_Data_to_Solr所述

If you truly want to send JSON to a Solr server you could use something like the HTTPClient to post the JSON to http://solrHost.com:8983/solr/update/json . 如果您确实想将JSON发送到Solr服务器,则可以使用HTTPClient之类的东西将JSON发布到http://solrHost.com:8983/solr/update/json

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

For indexing/adding to solr by Java, try this. 要通过Java索引/添加到solr,请尝试此操作。 Solr use REST like API to operate the index data. Solr使用REST之类的API来操作索引数据。

public void postSolr() {
try{
   DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8983/solr/update/json?wt=json&commit=true");
    StringEntity entity  = new StringEntity("{\"add\": { \"doc\": {\"id\": \"26\",\"keys\": \"java,php\"}}}", "UTF-8");
    entity.setContentType("application/json");
    post.setEntity(entity);                
    HttpResponse response = httpClient.execute(post);
    HttpEntity httpEntity = response.getEntity();
    InputStream in = httpEntity.getContent();

    String encoding = httpEntity.getContentEncoding() == null ? "UTF-8" : httpEntity.getContentEncoding().getName();
    encoding = encoding == null ? "UTF-8" : encoding;
    String responseText = IOUtils.toString(in, encoding);
    System.out.println("response Text is " + responseText);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}
}

The response when successfully post: 成功发布后的响应:

response Text is {"responseHeader":{"status":0,"QTime":1352}} 响应文本为{“ responseHeader”:{“ status”:0,“ QTime”:1352}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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