vietnamese-text-search
TypeScript icon, indicating that this package has built-in type declarations

1.0.25 • Public • Published

vietnamese-search

Text search for Vietnamese.

Install

npm install vietnamese-text-search

Usage

Import

Commonjs

const TextSearch = require('vietnamese-text-search');

ES6

import TextSearch from 'vietnamese-text-search';

Example

Initialize a TextSearch's instance for searching on product's names
import ProductObjs from './products.json';
import TextSearch from 'vietnamese-text-search';

(async () => {
  const options = {
    thresholdScore: 0.5,    // Default: 0.5
    limit: 30,              // Default: 30
    sortOrder: -1,          // Default: -1
    textKeyName: 'id',      // Default: 'textId'
    textValueName: 'name',  // Default: 'text'
    useAddedScore: false,   // Default: false
    autoGenBucket: true     // Default: true
  };
  // Initialize textSearch instance with `ProductObjs` and `options`
  const textSearch = await TextSearch.init(ProductObjs, options);
  ...
})
Add a new product to bucket products
// ...
const product = { id: '123', name: 'mặt nạ siêu thấm hút Aqua', addedScore: 0.1 };
// Because we initialized a `TextSearch`'s instance with {..., textKeyName: 'id', textValueName: 'name'},
// so any other product which added to the bucket later should has format { id: ..., name: ... }
const addResult = await textSearch.addNewTextObj(product, { bucket: 'products' });
console.log(addResult);

// { nAdded: 1, bucket: 'products' }
// ...
Search for mặt nạ Aqua
// ...
// Search with options
const searchOptions = {
  limit: 10,
  thresholdScore: 1,
  useAddedScore: true, // add `addedScore` when ranking text objects by their text score
  buckets: ['products'] // only search on bucket `products`
};
const searchResult = await textSearch.search('mặt nạ Aqua', searchOptions);
console.log(searchResult);

// {
//   data: [
//     // [id, score]
//     [ '123', 4.69999999999... ], 
//     ...
//   ],
//   sortOrder: -1,
//   thresholdScore: 1,
//   offset: 0,
//   limit: 10,
//   total: 100,
//   text: 'mặt nạ Aqua'
// }
// ...
Update the name and addedScore of a product
// ...
const upProduct = { id: '123', name: 'mặt nạ siêu thấm ngược Aqua', addedScore: 0.2 };
const updateResult = await textSearch.updateTextObj(upProduct.id, upProduct, {
  bucket: 'products'
});
console.log(updateResult);

// { nUpserted: 1, bucket: 'products' }
// ...
Remove a product
// ...
const removeResult = await textSearch.removeTextObj('123', { bucket: 'products' });
console.log(removeResult);

// { nRemoved: 1, bucket: 'products' }
// ...

APIs

API Description
.init(textObjs, options, cb) Initialize a TextSearch's instance with options.

Params:
textObjs: Array of text objects (e.g. [{ id: '123, name: 'Son môi siêu thấm hút', addedScore: 0.1 }, ...]).

options: {
   limit (number, default: 30): Limit number of search results.
   sortOrder (-1:Descending, 1: Ascending, default: -1): Order of results by score.
   thresholdScore (number, default: 0.5): Only results which have the text score >= this threshold should be returned.
   textKeyName (string, default: 'textId'): Field used as key of a text object.
   textValueName (string, default: 'text'): Field used as value of a text object.
   autoGenBucket (boolean, default: true): When Adding new text objects, Generate new bucket if not exist.
   bucket (string, default: 'default'): Bucket which text objects will be added into when initializing a TextSearch's instance with textObjs
   useAddedScore (boolean, default: false): addedScore from each text object will be added to its score when ranking results by the score.
  }

Return: {Promise<TextSearch>}
.search(text, options, cb) Search for text.

Params:
text: Text to search (e.g. son môi aqua).

options: {
   limit (number, default: 30): Limit number of search results.
   sortOrder (-1:Descending, 1: Ascending, default: -1): Order of results by score.
   thresholdScore (number, default: 0.5): Only results which have the text score >= this threshold should be returned.
   buckets (string, default: [all buckets]): Only search in this buckets.
   useAddedScore (boolean, default: false): addedScore from each text object will be added to its score when ranking results by the score.
  }

Return: {Promise<{ data: [[<textKey>, <score>], ...], total: number, text: string, ...[options] }>}
.addTextObj(textObj, options, cb) Add a new text object.

Params:
textObj: Text object to add (e.g. { id: '123', name: 'Son môi siêu thấm hút' }).

options: {
   bucket (string, default: 'default'): Add new text object to this bucket.
  }

Return: {Promise<{ nAdded: number, bucket: string }>}
.addManyTextObjs(textObjs, options, cb) Add many text objects.

Params:
textObjs: Array of text objects to add.

options: {
   bucket (string, default: 'default'): Add new text objects to this bucket.
  }

Return: {Promise<{ nAdded: number, details: {*} }>}
.updateTextObj(textKey, textObj, options, cb) Update a text object.

Params:
textKey: Text key of object to update.
textObj: This text object will be merged with the old or added to a bucket if option upsert is true.

options: {
   upsert (boolean, default: false): Add the text object if not exist.
   bucket (string, default: 'default'): Update text object in this bucket.
  }

Return: {Promise<{ nUpserted: number, bucket: string }>}
.updateManyTextObjs(textObjs, options, cb) Update many text objects.

Params:
textObjs: This text objects will be merged with the old or added to a bucket if option upsert is true.
(Note: Text objects must contain text key, the text key will be used to find the object need to update (e.g. [{id: '123', name: 'Son môi siêu thấm ngược Alan', addedScore: 0.2}, ...])

options: {
   upsert (boolean, default: false): Add the text object if not exist.
   bucket (string, default: 'default'): Update text objects in this bucket.
  }

Return: {Promise<{ nUpserted: number, details: {*} }>}
.removeTextObj(textKey, options, cb) Remove a text object.

Params:
textKey: Text key of object to remove.

options: {
   forceRemove (boolean, default: false): Do not throw an error if textKey not found.
   bucket (string, default: 'default'): Remove text object from this bucket.
  }

Return: {Promise<{ nUpserted: number, bucket: string }>}
.removeTextObjs(textKeys, options, cb) Remove many text objects.

Params:
textKeys: Remove text objects with these text keys.

options: {
   forceRemove (boolean, default: false): Do not throw an error if textKey not found.
   bucket (string, default: 'default'): Remove text object from this bucket.
  }

Return: {Promise<{ nRemoved: number, details: {*} }>}
.removeBuckets(buckets) Remove text bucket(s).

Params:
buckets: Remove a bucket (e.g. 'products') or remove many buckets (e.g. ['products', 'stores', 'companies']).

Return: {{ nRemoved: number, nRemains: number }}
.getStats() Get stats of the instance.

Return: {{ nObjects: number, nIndices: number }}

Notes

  1. TextObject should has format like { textId: '123', text: 'Son môi siêu thấm hút', addedScore: 0.1 } if options textKeyName and textValueName are not be set when initializing the instance. Otherwise, the object should has format { [textKeyName]: textKey, [textValueName]: textValue, addedScore: 0.1 }.

  2. addedScore of a text object is 0.0 if a text object not include it.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.25
    5
    • latest

Version History

Package Sidebar

Install

npm i vietnamese-text-search

Weekly Downloads

29

Version

1.0.25

License

MIT

Unpacked Size

87.2 kB

Total Files

16

Last publish

Collaborators

  • khacduylam