npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sphinx-promise

v1.0.7

Published

Promise-based native javascript implementation of the standard Sphinx API

Downloads

27

Readme

sphinx-promise NPM version dependencies Status devDependencies Status Status

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 = 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
})

Setting up limits & match mode

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
}

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