solr-node
Simple Solr Node Client Project
Install
npm install solr-node
Usage
Create Client
var SolrNode = require('solr-node');
var client = new SolrNode({
host: '127.0.0.1',
port: '8983',
core: 'test',
protocol: 'http'
});
require('log4js').getLogger('solr-node').level = 'DEBUG';
Search
Search can be executed with a simple text query or an object query.
Text
Text queries are similar to what one would find on the SOLR Core UI, EX:
From the URL: http://localhost:8080/solr/products/select?q=*%3A*&wt=json
The Query would be:
*:*&wt=json
NOTE: url decoded ':' from %3A
.
Object
Object based queries can be simple or complex using chaining. Each method of the Query object returns an instance of itself.
Examples:
Simple:
client.query().q({text:'test', title:'test'});
Complex and chained:
client.query()
.q({text:'test', title:'test'})
.addParams({
wt: 'json',
indent: true
})
.start(1)
.rows(1)
;
Query Examples
var strQuery = client.query().q('text:test');
var objQuery = client.query().q({text:'test', title:'test'});
var myStrQuery = 'q=text:test&wt=json';
solrClient.search(strQuery, function (err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.response);
});
solrClient.search(objQuery, function (err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.response);
});
solrClient.search(myStrQuery, function (err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.response);
});
Update
var data = {
text: 'test',
title: 'test'
};
client.update(data, function(err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.responseHeader);
});
Delete
var strQuery = 'id:testid'
var objQuery = {id:'testid'}
client.delete(strQuery, function(err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.responseHeader);
});
client.delete(objQuery, function(err, result) {
if (err) {
console.log(err);
return;
}
console.log('Response:', result.responseHeader);
});
Promise support
Skip the callback to get a promise back. ie:
var result = solrClient.search(query)
.then(function(result) {
console.log('Response:', result.response);
})
.catch(function(err) {
console.error(err);
});
You can also use async
/await
:
try {
const result = await solrClient.search(query);
console.log('Response:', result.response);
} catch(e) {
console.error(err);
}
Test & Coverage & Docs
gulp