@nguyen-thanh-tung/minisearch-synonyms
v1.0.0
Published
Use MiniSearch with synonyms
Downloads
33
Maintainers
Readme
MiniSearch Synonyms
A tiny module that lets you add keyword synonyms to your MiniSearch queries.
Installation
Using npm
:
npm install --save minisearch-synonyms
Using yarn
:
yarn add minisearch-synonyms
You can also access the package via a CDN:
<script src="https://www.unpkg.com/minisearch-synonyms@latest/dist/index.global.js"></script>
Remember install minisearch
as described in the MiniSearch docs.
Usage
Import minisearch-synonyms
alongside minisearch
:
import MiniSearch from 'minisearch';
import MiniSearchSynonyms from 'minisearch-synonyms';
Create an instance of MiniSearchSynonyms
and define your synonyms:
const synonyms = new MiniSearchSynonyms([
['car', 'auto', 'automobile', 'vehicle'],
['bike', 'bicycle']
]);
When searching, pass your query through the expandQuery
method:
const query = 'blue car';
const queryWithSynonyms = synonyms.expandQuery(query);
// => {
// combineWith: 'OR',
// queries: [
// 'blue car',
// 'blue auto',
// 'blue automobile',
// 'blue vehicle'
// ],
// }
const results = miniSearch.search(queryWithSynonyms);
You can add synonyms at any time:
synonyms.addSynonyms(['blue', 'azure', 'navy']);
Use the removeSynonyms
method to remove entire groups of synonyms:
synonyms.removeSynonyms('car');
// => removes the entire group of synonyms that includes 'car'
MiniSearchSynonyms supports multiple terms separated by whitespace or punctuation:
synonyms.addSynonyms(['ai', 'artificial intelligence']);
const query = 'the dangers of ai';
const queryWithSynonyms = synonyms.expandQuery(query);
// => {
// combineWith: 'OR',
// queries: [
// 'the dangers of ai',
// 'the dangers of artificial intelligence',
// ],
// }
License
This project is licensed under the MIT License. See the LICENSE file for details.