1.0.25 • Published 5 years ago
vietnamese-text-search v1.0.25
vietnamese-search
Text search for Vietnamese.
Install
npm install vietnamese-text-searchUsage
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
TextObject should has format like { textId: '123', text: 'Son môi siêu thấm hút', addedScore: 0.1 } if options
textKeyNameandtextValueNameare not be set when initializing the instance. Otherwise, the object should has format { textKeyName: textKey, textValueName: textValue, addedScore: 0.1 }.addedScore of a text object is 0.0 if a text object not include it.
1.0.25
5 years ago
1.0.24
5 years ago
1.0.23
5 years ago
1.0.22
5 years ago
1.0.21
5 years ago
1.0.19
5 years ago
1.0.18
5 years ago
1.0.17
5 years ago
1.0.16
5 years ago
1.0.15
5 years ago
1.0.14
5 years ago
1.0.13
5 years ago
1.0.9
5 years ago
1.0.8
5 years ago
1.0.11
5 years ago
1.0.10
5 years ago
1.0.12
5 years ago
1.0.7
5 years ago
1.0.6
5 years ago
1.0.5
5 years ago
1.0.4
5 years ago
1.0.3
5 years ago
1.0.2
5 years ago
1.0.1
5 years ago
1.0.0
5 years ago