Graceful native bluebird-promise-based javascript implementation of the standard Sphinx API for fulltext searching based on top of sphinxapi
- Install module from npm:
$ npm install --save sphinx-promise
- Setup your sphinx configuration file and run it
Include:
var Sphinx = require('sphinx-promise');
or if you prefer es6/7 syntax:
import Sphinx from 'sphinx-promise';
Create instance:
const sphinx = new Sphinx(); // it uses default host (localhost) & port (9312)
or if you wanna set up your server configuration add:
const sphinx = new Sphinx({
host: 'localhost', // default sphinx host
port: 9312 // default sphinx TCP port
});
or
const sphinx = new Sphinx();
sphinx.setConfig({
host: 'localhost',
port: 9312
});
let query = 'word | anotherword';
sphinx.query(query).then(result => {
console.log(result);
}).catch(console.error.bind(console));
or in es7:
let query = 'word | anotherword';
let result = await sphinx.query(query);
console.log(result);
You can learn how to set up a filter from the official documentation.
let query = 'computer';
let filters = [{
attr: 'authorid', // attribute's name
values: [ 2, 12, 34 ], // multi-valued type in Sphinx
exclude: false // optional parameter, default is false
}];
sphinx.query(query, { filters }).then(result => {
console.log(result.matches); // array of objects with document's ids
});
or multiple filters:
let query = 'love';
let filters = [{
attr: 'authorid', // attribute's name
values: [ 2, 12, 34 ], // multi-valued type in Sphinx
exclude: false // optional parameter, default is false
}, {
attr: 'categoryid',
values: [ 1321 ]
}];
sphinx.query(query, { filters }).then(result => {
console.log(result.matches); // array of objects with document's ids
});
you can include query string into your option's object just like here:
sphinx.query({ query, filters }).then(result => {
console.log(result.matches); // array of objects with document's ids
});
Another query example with specifyingindex
or comment
(for logs):
let index = 'editions'; // indexes, default is '*'
let comment = 'Debug query'; // you can find the string in your query logs
sphinx.query({ query, filters, index, comment }).then(result => {
console.log(result.matches); // array of objects with document's ids
});
If you want get only array of ids from a result, just add the resultAsIds: true
boolean parameter.
sphinx.query({ query, filters, resultAsIds: true }).then(result => {
console.log(result); // `result` is array of ids now
});
This module supports chains of queries on top of promises as well.
Basic usage of addQuery
& runQueries
:
let queries = [{
query: 'cats'
}, {
query: 'cars',
filters: [{
attr: 'authorid',
values: [ 394 ]
}]
}, {
query: 'sleepy foxes',
filters: [{
attr: 'authorid',
values: [ 854, 1557 ]
}, {
attr: 'categoryid',
values: [ 2 ],
exclude: false
}],
index: 'main, delta',
comment: 'Test query'
}];
queries.forEach(query => sphinx.addQuery(query));
Sphinx#addQuery
returns an index from array that will be returned after Sphinx#runQueries
execution.
To get results just invoke runQueries
function:
sphinx.runQueries().then(results => {
// `results` are array in the appropriate order
})
More complex example:
sphinx.runQueries().tap(results => {
console.log('Results length:', results.length); // just log the length of result & go on
}).map(result => {
return sphinx.getIdsFromResult(result); // get an array of ids from single result
}).spread((first, second, third) => {
// `first`, `second` & `third` are "smeared" results now
// each argument is an array of ids
})
const sphinx = new Sphinx();
const params = {
index: 'books',
limits: {
offset: 0, // default is 0
limit: 100 // default is 20 as documented
},
matchMode: Sphinx.SPH_MATCH_ANY
}
/**
* e. g. getting user from db, search books by user's name
* and then collate books by their ids
*/
async function getUsersBooks(userId) {
let user = await User.findOne(userId);
let result = await sphinx.query(user.name, params);
let ids = sphinx.getIdsFromResult(result); // or include `resultAsIds: true` in options
return Books.findAll({
where: {
id: {
$in: ids
}
}
});
}
try {
let books = await getUsersBooks(1);
} catch (error) {
console.error(error); // catching errors
}
- Implement other methods such as
setSelect
,addFilterString
,addFilterRange
etc. - Add a full description about each method from documentation.
$ mocha
sphinxapi https://github.com/Inist-CNRS/node-sphinxapi
MIT ยฉ 2016 Alexander Belov