Gary Sieling

Update documents in Solr from Node

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:

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();
Exit mobile version