1

I want to add data to solr 4.0 using java. I have the following array List<Map<String, Object>> docs = new ArrayList<>();. I am converting the array to json object using GSON method. I want to commit this data to solr. How do i do that? I have read solrj but not getting idea how to get it to work.

2 Answers 2

5

With the solrj client you create SolrInputDocument objects and post them to SolrServer instance be it a HttpSolrServer or a EmbeddedSolrServer. The SolrInputDocument is a name value pair collection the would be equivalent to the json you are trying to post. As described at 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.

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);
Sign up to request clarification or add additional context in comments.

2 Comments

That is a great answer but where are you setting your Solr core name?
Please note that you must add the command before your json string like: {"add": {"doc":{"userName": "Bill", "stars": 4, "review_id": "-TsVN230RCkLYKBeLsuz7A", "type": "review"}} Or place it in a an array like `[ {your json} ]
3

For indexing/adding to solr by Java, try this. Solr use REST like API to operate the index data.

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}}

2 Comments

i tried your code and receiving this response {"responseHeader": {"status": 400,"QTime": 0},"error": {"msg": "Unknown command: userSearchQuery [18]","code": 400}} userSearchQuery is my first key in my JSON input
Can't thank you enough brother. your arguements ?wt=json&commit=true after solr url helped me solve my problem. Otherwise, I was getting no result on executing the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.