sphinx-promise
Graceful native bluebird-promise-based javascript implementation of the standard Sphinx API for fulltext searching based on top of sphinxapi
Install
- Install module from npm:
$ npm install --save sphinx-promise
- Setup your sphinx configuration file and run it
Usage
Include:
var Sphinx = ;
or if you prefer es6/7 syntax:
;
Create instance:
const sphinx = ; // it uses default host (localhost) & port (9312)
or if you wanna set up your server configuration add:
const sphinx = host: 'localhost' // default sphinx host port: 9312 // default sphinx TCP port;
or
const sphinx = ;sphinx;
Basic usage:
let query = 'word | anotherword'; sphinx;
or in es7:
let query = 'word | anotherword'; let result = await sphinx;console;
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;
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;
you can include query string into your option's object just like here:
sphinx;
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;
If you want get only array of ids from a result, just add the resultAsIds: true
boolean parameter.
sphinx;
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;
Sphinx#addQuery
returns an index from array that will be returned after Sphinx#runQueries
execution.
To get results just invoke runQueries
function:
sphinx
More complex example:
sphinx
Setting up limits & match mode
const sphinx = ;const params = index: 'books' limits: offset: 0 // default is 0 limit: 100 // default is 20 as documented matchMode: SphinxSPH_MATCH_ANY /** * e. g. getting user from db, search books by user's name * and then collate books by their ids */ { let user = await User; let result = await sphinx; let ids = sphinx; // or include `resultAsIds: true` in options return Books;} try let books = await ; catch error console; // catching errors
Todo
- Implement other methods such as
setSelect
,addFilterString
,addFilterRange
etc. - Add a full description about each method from documentation.
Tests
$ mocha
Also
sphinxapi https://github.com/Inist-CNRS/node-sphinxapi
License
MIT © 2016 Alexander Belov