1.0.25 • Published 3 years ago

vietnamese-text-search v1.0.25

Weekly downloads
26
License
MIT
Repository
github
Last release
3 years ago

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

APIDescription
.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.

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.12

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago