helios

SOLR client for Nodejs

helios - a nodejs-solr client

Well, this is a little neater version of the already available nodejs-solr clients. I figured out that a lot of problems lies in creating queries and documents. The powerful thing of this library would be the queryBuilder & document class.

Releases are available for download from GitHub. Alternatively, you can install using Node Package Manager (npm) as helios:

npm install helios
var helios = require('helios')
var solr_client = new helios.client({
  host : 'localhost', // Insert your client host 
  port : 8989,
  path : '/solr', // Insert your client solr path 
  timeout : 1000  // Optional request timeout 
});
 
var queryBuilder = new helios.queryBuilder();
 
solr_client.select(queryBuilder.simpleQuery({
  op : 'OR',
  df : 'field_name',
  q : 'keyword1 keyword2'
}), function(errres) {
  if (err) console.log(err);
  console.log(JSON.parse(res)); // yes, it returns in raw format, you need to JSON.parse it 
});

helios.queryBuilder

helios.client

helios.document


var queryBuilder = new Helios.queryBuilder();

All the methods can be used together and can be get as a combined query string by using the toString() method

queryBuilder.simpleQuery({
  op : 'OR',
  df : 'field_name',
  q : 'keyword1 keyword2'
})
queryBuilder.facetQuery({
  'facet' : 'true',
  'facet.date' : 'timestamp',
  'facet.date.start' : 'NOW/DAY-5DAYS',
  'facet.date.end' : 'NOW/DAY+1DAY',
  'facet.date.gap' : '+1DAY'
});

It just returns the query you have made as a string

It can be initialized by helios.client() or by passing an options Object as an argument like helios.client(options).

try {
  var solr_client = new helios.client({
    host : 'localhost', // Insert your client host 
    port : 8983,
    path : '/solr' // Insert your client solr path 
  });
catch (e) {
  console.log(e);
}

options can have

  • host
  • port
  • path
  • proxy : { host : "proxy_host", port : 55 }
  • timeout : 10000 in milliseconds

It accepts two arguments:

  • query string OR query object OR an instance of helios.queryBuilder
  • callback (err, res)
solr_client.select({
  q : "field_name:value",
  rows : 25,
  start : 50
  // and so on 
}, function(errres) {
  if (err) console.log(err);
  var result = JSON.parse(res); // told you, you have to JSON.parse the res 
});

It accepts 3 arguments:

  • solrdoc : an instance of helios.document
  • commit_flag : boolean
  • callback(err) : a callback which is given an error message upon failure
var solrdoc = new helios.document();
solrdoc.addField('field_name1', 'value1');
solrdoc.addField('field_name2', 'value2');
 
solr_client.addDoc(solrdoc, true, function(err) {
  if (err) console.log(err);
});

It accepts 3 arguments:

  • solrdoc : an instance of helios.document
  • commit_flag : boolean
  • callback(err) : a callback which is given an error message upon failure

NOTES: Use 'set' when you want to update a field value and use 'add' for add a value to multivalue fields.

var solrdoc = new helios.document();
// update the field_name2 
solrdoc.setField('field_name2', 'value1updated', null, 'set');
// add the field_name3 
solrdoc.setField('field_name3', 'value3', null, 'add');
// add the field_name4 with boost=1 
solrdoc.setField('field_name4', 'value4', 1 /*boost*/, 'set');
 
solr_client.updateDoc(solrdoc, true, function(err) {
  if (err) console.log(err);
});
 
Or equivalent:
 
var solrdoc = new helios.document();
// update the field_name2 
solrdoc.setField('field_name2', 'value1updated');
// add the field_name3 
solrdoc.setField('field_name3', 'value3');
// add the field_name4 with boost=1 
solrdoc.setField('field_name4', 'value4');
 
// setting the updates and boost 
doc.setFieldUpdate('field_name2', 'set');
doc.setFieldUpdate('field_name3', 'add');
doc.setFieldUpdate('field_name4', 'set');
doc.setFieldBoost('field_name4', 1);
 
solr_client.updateDoc(solrdoc, true, function(err) {
  if (err) console.log(err);
});
var solrdoc = new helios.document();
// delete the field_name4 
doc.setFieldDelete('field_name4');
 
solr_client.updateDoc(solrdoc, true, function(err) {
  if (err) console.log(err);
});

It accepts 4 arguments:

  • id : The Solr document id. This is defined in the schema.
  • values : The list of documents to delete. This could be a string or an array of strings.
  • commit_flag : boolean
  • callback(err) : a callback which is given an error message upon failure
var solrdoc = new helios.document();
 
// delete the document with id=1 
solr_client.deleteDoc('id', '1' true, function(err) {
  if (err) console.log(err);
});
 
// delete the documents with id=2, id=3 and id=4 
solr_client.deleteDoc('id', ['2', '3', '4'], true, function(err) {
  if (err) console.log(err);
});

It accepts 4 arguments:

  • query : The "deleteDocByQuery" uses the Lucene query parser by default. Please refer to Solr documentation for more details.
  • commit_flag : boolean
  • maxAffected : Before to delete the docs deleteDocByQuery will check how many docs will be deleted. If this is greather than the the maxAffected value the method will stop. Set this value to 0 if you want to delete all the docs affected by the query.
  • callback(err) : a callback which is given an error message upon failure
var solrdoc = new helios.document();
 
// delete all the documents where name = 'Peter' and cancel the delete if there are are more than 18 documents. 
solr_client.deleteDocByQuery('name:Peter', commit, 18, function(err) {
  if(err) {
    console.log('error: ', err);
  } else {
    console.log('Documents deleted!');
  }
});
 
// delete ALL the documents where name = 'Peter' (Be careful when setting maxAffected to 0, you could delete your whole database when query=*:*) 
solr_client.deleteDocByQuery('name:Peter', commit, 0, function(err) {
  if(err) {
    console.log('error: ', err);
  } else {
    console.log('Documents deleted!');
  }
});

This class will ease the steps required to make a document to be added to solr.

var solr_doc = new helios.document();

This returns the document's boost

This sets the document's boost to a given float

solr_doc.setBoost(2.112);

It accepts the following arguments:

  • field_name
  • value the value of the field_name
  • boost the boost to be set for field_name

This method is helpful in adding values to a multi=true field

solr_doc.setMultiValue('field_name', 'value1', 2);
solr_doc.setMultiValue('field_name', 'value2', 1.5);

note that adding boost every time in the setMultiValue for the same field_name actually results in a compound value which is the multiplication of the boosts added eg. for the above case: 2 * 1.5 = 3

This method returns the value set for field_name

solr_doc.getField('field_name');

This methods adds field_name whose value is value and boost is boost.

solr_doc.setField('field_name1', 'value1', 1.21);
solr_doc.setField('field_name2', 2121);

This method returns the boost of field_name

solr_doc.getFieldBoost("field_name");

This method sets the boost for field name field_name

solr_doc.setFieldBoost('field_name', 2.121);

Returns a key-value object of all the fields and their boosts

This method returns the update type ('set' or 'add') of field_name

solr_doc.getFieldUpdate("field_name");

This method sets the update type ('set' or 'add') for field name field_name

solr_doc.setFieldUpdate('field_name', 'set');

Returns a key-value object of all the fields and their update types

This method returns the delete setup for a field_name

solr_doc.getFieldDelete("field_name");

This method sets the field name field_name to delete from the Solr doc

solr_doc.setFieldDelete('field_name');

Returns a key-value object of all the fields to delete from the Solr doc

This clears the all the fields, fieldBoosts as well as the documentBoost

This method converts the current helios.document into a solr readable XML

solr_doc.toXML();

Issues

I know there are a lot of pending tasks and modifications required in this library. If you find any bug or a feature that you want, please submit an issue. I will respond to it as soon as I can and make required changes to it.

If you like this module, please do star it :)

npm star helios