从java向solr添加json数据
我想使用java向Solr4.0添加数据。我有以下数组列表docs=newarraylist;。我使用GSON方法将数组转换为json对象。我想把这些数据提交给solr。我该怎么做?我已经读过,但不知道如何让它工作。使用solrj客户端,您可以创建SolrInputDocument对象,并将它们发布到SolrServer实例,无论是HttpSolrServer还是EmbeddedSolrServer。SolrInputDocument是一个名称-值对集合,它将等效于您试图发布的json。如中所述 如果您真的想将JSON发送到Solr服务器,您可以使用HTTPClient之类的东西将JSON发送到Solr服务器从java向solr添加json数据,java,solr,solrj,Java,Solr,Solrj,我想使用java向Solr4.0添加数据。我有以下数组列表docs=newarraylist;。我使用GSON方法将数组转换为json对象。我想把这些数据提交给solr。我该怎么做?我已经读过,但不知道如何让它工作。使用solrj客户端,您可以创建SolrInputDocument对象,并将它们发布到SolrServer实例,无论是HttpSolrServer还是EmbeddedSolrServer。SolrInputDocument是一个名称-值对集合,它将等效于您试图发布的json。如中所述
使用solrj客户端,您可以创建SolrInputDocument对象,并将它们发布到SolrServer实例(无论是HttpSolrServer还是EmbeddedSolrServer)。SolrInputDocument是一个名称-值对集合,它将等效于您试图发布的json。如中所述 如果您真的想将JSON发送到Solr服务器,您可以使用HTTPClient之类的东西将JSON发送到Solr服务器
要通过Java对solr进行索引/添加,请尝试以下操作。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();
}
}
成功发布时的响应:
响应文本为{responseHeader:{status:0,QTime:1352}}要通过Java索引/添加到solr,请尝试以下操作。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();
}
}
成功发布时的响应:
响应文本为{responseHeader:{status:0,QTime:1352}}我尝试了您的代码并收到了此响应{responseHeader:{status:400,QTime:0},错误:{msg:Unknown command:userSearchQuery[18],code:400}userSearchQuery是我JSON输入中的第一个键我尝试了您的代码并收到了此响应{responseHeader:{status:400,QTime:0},错误:{msg:Unknown命令:userSearchQuery[18],代码:400}}userSearchQuery是JSON输入中的第一个键