Text search for vietnamese.
npm install vietnamese-text-searchText search for Vietnamese.
``sh`
npm install vietnamese-text-search
#### Commonjs
`javascript`
const TextSearch = require('vietnamese-text-search');
#### ES6
`javascript`
import TextSearch from 'vietnamese-text-search';
##### Initialize a TextSearch's instance for searching on product's names
`javascript
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
`javascriptTextSearch
// ...
const product = { id: '123', name: 'mặt nạ siêu thấm hút Aqua', addedScore: 0.1 };
// Because we initialized a '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
`javascriptaddedScore
// ...
// Search with options
const searchOptions = {
limit: 10,
thresholdScore: 1,
useAddedScore: true, // add when ranking text objects by their text scoreproducts
buckets: ['products'] // only search on bucket
};
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
`javascript
// ...
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
`javascript
// ...
const removeResult = await textSearch.removeTextObj('123', { bucket: 'products' });
console.log(removeResult);
// { nRemoved: 1, bucket: 'products' }
// ...
`
| API | Description |
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| .init(textObjs, options, cb) | Initialize a TextSearch's instance with options.textObjs
Params:
: Array of text objects (e.g. [{ id: '123, name: 'Son môi siêu thấm hút', addedScore: 0.1 }, ...]).options
: {key
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 of a text object.value
textValueName _(string, default: 'text')_: Field used as of a text object.TextSearch
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 's instance with textObjsaddedScore
useAddedScore _(boolean, default: false)_: from each text object will be added to its score when ranking results by the score.text
}
Return: _{Promise\
| .search(text, options, cb) | Search for .text
Params:
: Text to search (e.g. _son môi aqua_).options
: {buckets
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 .addedScore
useAddedScore _(boolean, default: false)_: from each text object will be added to its score when ranking results by the score.textObj
}
Return: _{Promise\<{ data: \[\[\
| .addTextObj(textObj, options, cb) | Add a new text object.
Params:
: Text object to add (e.g. _{ id: '123', name: 'Son môi siêu thấm hút' }_).options
: {textObjs
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:
: Array of text objects to add.options
: {textKey
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:
: 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
: {textObjs
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:
: This text objects will be merged with the old or added to a bucket if option upsert is true.options
_(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}, ...\]_)_
: {textKey
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:
: Text key of object to remove.options
: {textKey
forceRemove _(boolean, default: false)_: Do not throw an error if not found. textKeys
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:
: Remove text objects with these text keys.options
: {textKey
forceRemove _(boolean, default: false)_: Do not throw an error if not found. buckets
bucket _(string, default: 'default')_: Remove text object from this bucket.
}
Return: _{Promise\<{ nRemoved: number, details: {\*} }\>}_ |
| .removeBuckets(buckets) | Remove text bucket(s).
Params:
: 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 }}_ |
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 }_.
1. addedScore of a text object is 0.0 if a text object not include it.