Update documents in Solr from Node

Datetime:2016-08-23 02:00:58          Topic: Solr           Share

If you start out with Solr, the easiest way to post data is to use the provided command line scripts, but at some point you may want to move to posting data yourself, from within your application.

In Node, you can do this with a POST request, but there are a few caveats:

  • Don’t set Content-Length – this prevents chunked requests, and Jetty will cut them off, preventing your JSON from being parsed
  • Make sure you’re not posting null values to array fields (this seems to cause exceptions in Solr
  • Use the /update/json/docs endpoint – this can handle nested JSON structures
  • Make sure you set the ‘id’ field correctly, so that Solr knows how to overwrite existing docs
  • In this example, I’ve used the command syntax for adding documents (this would let you move to a system where you partially update documents, if that is desired)
const postOptions = {
    host: 'localhost',
    port: 8983,
    path: '/solr/talks/update/json/docs',
    method: 'POST',
    headers: {
      'Content-Type': 'text/json'
    }
  }
 
  const postRequest = http.request(postOptions, (res) => {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
      console.log('Response: ' + chunk);
    });
    res.on('end', onResult);
  });
 
  const postData = JSON.stringify(
    {
      add: {
        doc: data,
        overwrite: true,
        commitWithin: 1000,
        boost: 1
      }
    }
  );
 
  postRequest.write(postData);
  postRequest.end();
Looking for a new book to read? My younger sister is writing an imaginative series of sci-fi novels , set in a futuristic world with a novel form of space travel.




About List