{"id":20517177,"url":"https://github.com/iprit/sphinx-promise","last_synced_at":"2025-04-14T00:54:29.543Z","repository":{"id":47989427,"uuid":"69412860","full_name":"IPRIT/sphinx-promise","owner":"IPRIT","description":":mag_right::cat2: Native promise-based module for Sphinx search engine","archived":false,"fork":false,"pushed_at":"2022-12-04T04:46:14.000Z","size":295,"stargazers_count":4,"open_issues_count":7,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T14:55:27.948Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/sphinx-promise","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IPRIT.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-28T01:21:07.000Z","updated_at":"2019-07-26T09:28:50.000Z","dependencies_parsed_at":"2023-01-24T03:00:46.996Z","dependency_job_id":null,"html_url":"https://github.com/IPRIT/sphinx-promise","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IPRIT%2Fsphinx-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IPRIT%2Fsphinx-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IPRIT%2Fsphinx-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IPRIT%2Fsphinx-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IPRIT","download_url":"https://codeload.github.com/IPRIT/sphinx-promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248804783,"owners_count":21164131,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T21:34:14.263Z","updated_at":"2025-04-14T00:54:29.520Z","avatar_url":"https://github.com/IPRIT.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sphinx-promise [![NPM version][npm-image]][npm-url] [![dependencies Status][depstat-image]][depstat-url] [![devDependencies Status Status][deVdepstat-image]][deVdepstat-url]\n\nGraceful native [bluebird](http://bluebirdjs.com/docs/getting-started.html)-promise-based javascript implementation of the standard [Sphinx](http://sphinxsearch.com/) API for fulltext searching based on top of [sphinxapi](https://www.npmjs.com/package/sphinxapi)\n\n![](https://4.bp.blogspot.com/-55kzheOMWfg/VzYvr6MC4rI/AAAAAAAAAP8/fZFAnstd93cqNr7f8E7ESN9TpnmgbrWoACLcB/s1600/%25D1%2581%25D0%25BA%25D0%25B0%25D1%2587%25D0%25B0%25D0%25BD%25D0%25BD%25D1%258B%25D0%25B5%2B%25D1%2584%25D0%25B0%25D0%25B9%25D0%25BB%25D1%258B%2B%25282%2529.png)  ![](http://www.tivix.com/uploads/images/logo_1.focus-none.max-256x256_6cCD0N8.png)\n\n\n## Install\n\n* Install module from npm:\n```\n$ npm install --save sphinx-promise\n```\n\n* [Setup](http://sphinxsearch.com/docs/current.html#confgroup-source) your sphinx configuration file and run it\n\n## Usage\n\nInclude:\n\n```js\nvar Sphinx = require('sphinx-promise');\n```\nor if you prefer es6/7 syntax:\n\n```js\nimport Sphinx from 'sphinx-promise';\n```\n\nCreate instance:\n```js\nconst sphinx = new Sphinx(); // it uses default host (localhost) \u0026 port (9312)\n```\nor if you wanna set up your server configuration add:\n```js\nconst sphinx = new Sphinx({\n\thost: 'localhost', // default sphinx host\n\tport: 9312 // default sphinx TCP port\n});\n```\nor\n```js\nconst sphinx = new Sphinx();\nsphinx.setConfig({\n\thost: 'localhost',\n\tport: 9312\n});\n```\n\n### Basic usage:\n\n```js\nlet query = 'word | anotherword';\n\nsphinx.query(query).then(result =\u003e {\n\tconsole.log(result);\n}).catch(console.error.bind(console));\n```\nor in es7:\n```js\nlet query = 'word | anotherword';\n\nlet result = await sphinx.query(query);\nconsole.log(result);\n```\n\n### Setting up filters\nYou can learn how to [set up a filter](http://sphinxsearch.com/docs/current.html#api-func-setfilter) from [the official documentation](http://sphinxsearch.com/docs/current.html).\n\n```js\nlet query = 'computer';\nlet filters = [{\n\tattr: 'authorid', // attribute's name\n\tvalues: [ 2, 12, 34 ], // multi-valued type in Sphinx\n\texclude: false // optional parameter, default is false\n}];\n\nsphinx.query(query, { filters }).then(result =\u003e {\n\tconsole.log(result.matches); // array of objects with document's ids\n});\n```\nor multiple filters:\n```js\nlet query = 'love';\nlet filters = [{\n\tattr: 'authorid', // attribute's name\n\tvalues: [ 2, 12, 34 ], // multi-valued type in Sphinx\n\texclude: false // optional parameter, default is false\n}, {\n\tattr: 'categoryid',\n\tvalues: [ 1321 ]\n}];\n\nsphinx.query(query, { filters }).then(result =\u003e {\n\tconsole.log(result.matches); // array of objects with document's ids\n});\n```\nyou can include query string into your option's object just like here:\n```js\nsphinx.query({ query, filters }).then(result =\u003e {\n\tconsole.log(result.matches); // array of objects with document's ids\n});\n```\nAnother query example with specifying`index`or `comment`(for logs):\n```js\nlet index = 'editions'; // indexes, default is '*'\nlet comment = 'Debug query'; // you can find the string in your query logs\n\nsphinx.query({ query, filters, index, comment }).then(result =\u003e {\n\tconsole.log(result.matches); // array of objects with document's ids\n});\n```\nIf you want get only array of ids from a result, just add the `resultAsIds: true` boolean parameter.\n```js\nsphinx.query({ query, filters, resultAsIds: true }).then(result =\u003e {\n\tconsole.log(result); // `result` is array of ids now\n});\n```\n\n### Chain queries\nThis module supports chains of queries on top of promises as well.\n\nBasic usage of `addQuery` \u0026 `runQueries`:\n```js\nlet queries = [{\n\tquery: 'cats'\n}, {\n\tquery: 'cars',\n\tfilters: [{\n\t    attr: 'authorid',\n\t    values: [ 394 ]\n\t}]\n}, {\n\tquery: 'sleepy foxes',\n\tfilters: [{\n\t    attr: 'authorid',\n\t    values: [ 854, 1557 ]\n\t}, {\n\t    attr: 'categoryid',\n\t    values: [ 2 ],\n\t    exclude: false\n\t}],\n\tindex: 'main, delta',\n\tcomment: 'Test query'\n}];\n\nqueries.forEach(query =\u003e sphinx.addQuery(query));\n```\n`Sphinx#addQuery` returns an index from array that will be returned after `Sphinx#runQueries` execution.\n\nTo get results just invoke `runQueries` function:\n```js\nsphinx.runQueries().then(results =\u003e {\n\t// `results` are array in the appropriate order\n})\n```\nMore complex example:\n```js\nsphinx.runQueries().tap(results =\u003e {\n\tconsole.log('Results length:', results.length); // just log the length of result \u0026 go on\n}).map(result =\u003e {\n\treturn sphinx.getIdsFromResult(result); // get an array of ids from single result\n}).spread((first, second, third) =\u003e {\n\t// `first`, `second` \u0026 `third` are \"smeared\" results now\n\t// each argument is an array of ids\n})\n```\n### Setting up limits \u0026 match mode\n\n```js\nconst sphinx = new Sphinx();\nconst params = {\n    index: 'books',\n\tlimits: {\n\t    offset: 0, // default is 0\n\t    limit: 100 // default is 20 as documented\n\t},\n\tmatchMode: Sphinx.SPH_MATCH_ANY\n}\n\n/**\n * e. g. getting user from db, search books by user's name\n * and then collate books by their ids\n */\nasync function getUsersBooks(userId) {\n    let user = await User.findOne(userId);\n    let result = await sphinx.query(user.name, params);\n    let ids = sphinx.getIdsFromResult(result); // or include `resultAsIds: true` in options\n    return Books.findAll({\n        where: {\n            id: {\n                $in: ids\n            }\n        }\n    });\n}\n\ntry {\n    let books = await getUsersBooks(1);\n} catch (error) {\n    console.error(error); // catching errors\n}\n```\n\n## Todo\n\n* Implement other methods such as `setSelect`, `addFilterString`, `addFilterRange` etc.\n* Add a full description about each method from [documentation](http://sphinxsearch.com/docs/current.html).\n\n## Tests\n\n```js\n$ mocha\n```\n\n## Also\n\nsphinxapi [https://github.com/Inist-CNRS/node-sphinxapi](https://github.com/Inist-CNRS/node-sphinxapi)\n\n\n## License\n\n[MIT](https://github.com/IPRIT/sphinx-promise/LICENCE.md) © 2016 Alexander Belov\n\n\n[npm-url]: https://www.npmjs.com/package/sphinx-promise\n[npm-image]: https://img.shields.io/npm/v/sphinx-promise.svg\n\n[depstat-url]: https://david-dm.org/IPRIT/sphinx-promise\n[depstat-image]: https://img.shields.io/david/IPRIT/sphinx-promise.svg\n\n[deVdepstat-url]: https://david-dm.org/IPRIT/sphinx-promise?type=dev\n[deVdepstat-image]: https://img.shields.io/david/dev/IPRIT/sphinx-promise.svg\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiprit%2Fsphinx-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiprit%2Fsphinx-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiprit%2Fsphinx-promise/lists"}