Package Exports
- sphinx-promise
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (sphinx-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
sphinx-promise

Graceful bluebird-promise-based Sphinx API module for fulltext searching

Install
- Install module from npm:
$ npm install --save sphinx-promise- Setup your sphinx configuration file and run it
Usage
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
});Basic usage:
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);Setting up filters
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 specifyingindexor 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
});Chain queries
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
})Tests
$ mochaLicense
MIT © 2016 Alexander Belov