一句話介紹
Web端最快且最具內存靈活性的全文搜索庫,零依賴性。
Github地址:https://github.com/nextapps-de/flexsearch
![FlexSearch.js:快速、零依賴的Javascript全文搜索庫](http://p2.ttnews.xyz/loading.gif)
github截圖
中文翻譯介紹
在原始搜索速度方面,FlexSearch優於每一個搜索庫,並提供靈活的搜索功能,如多字段搜索,語音轉換或部分匹配。 根據使用的選項,它還提供最高內存效率的索引。 FlexSearch引入了一種新的評分算法,稱為“上下文索引”,基於預先評分的詞典字典體系結構,與其他庫相比,實際執行的查詢速度提高了1,000,000倍。 FlexSearch還為您提供非阻塞異步處理模型以及Web工作者,以通過專用平衡線程並行地對索引執行任何更新或查詢。
安裝
可以到官網下載經過壓縮的js文件或者使用cdn,也可以使用npm安裝
//使用最新版:
//或者特定版
- npm安裝
npm install flexsearch
用法
- 創建一個索引
var index = new FlexSearch();
//或者
var index = FlexSearch.create();
//或者給定一個默認值
var index = new FlexSearch("speed");
//自定義配置
var index = new FlexSearch({
// default values:
encode: "balance",
tokenize: "forward",
threshold: 0,
async: false,
worker: false,
cache: false
});
//在或者
var index = new FlexSearch("memory", {
encode: "balance",
tokenize: "forward",
threshold: 0
});
- 將文本添加到索引
Index.add(id, string)
index.add(10025, "John Doe");
- 搜索
Index.search(string | options, <limit>, <callback>)/<callback>/<limit>
index.search("John");
- 限制數量
index.search("John", 10);
- 異步搜索
//基於回調函數
index.search("John", function(result){
// array of results
});
//基於Promise
index.search("John").then(function(result){
// array of results
});
//es6寫法
async function search(query){
const result = await index.search(query);
// ...
}
- 自定義搜索
index.search({
query: "John",
limit: 1000,
threshold: 5, // >= threshold
depth: 3, // <= depth
callback: function(results){
// ...
}
});
//或者
index.search("John", {
limit: 1000,
threshold: 5,
depth: 3
}, function(results){
// ....
});
- 分頁
var response = index.search("John Doe", {
limit: 5,
page: true
});
index.search("John Doe", {
limit: 10,
page: response.next
});//下一頁
- 建議
獲取查詢建議
index.search({
query: "John Doe",
suggest: true
});
啟用建議後,將填寫所有結果(直到限制,默認為1000),並按相關性排序相似的匹配。
- 更新
Index.update(id, string)
index.update(10025, "Road Runner");
- 移除
Index.remove(id)
index.remove(10025);
- 重置索引
index.clear();
- 銷燬
index.destroy();
- 重新初始化
Index.init(<options>)/<options>
//使用相同配置重新初始化
index.init();
//使用新的配置重新初始化
index.init({
/* options */
});
//重新初始化會銷燬舊的索引
- 獲取長度
var length = index.length;
- 獲取寄存器
var index = index.index;
寄存器的格式為“@”+ id
請不要手動修改寄存器,用作只讀即可
- 添加自定義匹配器
FlexSearch.registerMatcher({REGEX: REPLACE})
- 為所有實例添加全局匹配:
FlexSearch.registerMatcher({
'ä': 'a', // replaces all 'ä' to 'a'
'ó': 'o',
'[ûúù]': 'u' // replaces multiple
});
- 為特定實例添加私有匹配:
index.addMatcher({
'ä': 'a', // replaces all 'ä' to 'a'
'ó': 'o',
'[ûúù]': 'u' // replaces multiple
});
- 添加定製編碼
通過在索引創建/初始化期間傳遞函數來分配自定義編碼
var index = new FlexSearch({
encode: function(str){
// do something with str ...
return str;
}
});
編碼器函數獲取一個字符串作為參數,返回修改後的字符串
直接調用自定義編碼器:
var encoded = index.encode("sample text");
- 註冊全局編碼
FlexSearch.registerEncoder(name, encoder)
所有實例都可以共享/使用全局編碼
FlexSearch.registerEncoder("whitespace", function(str){
return str.replace(/\s/g, "");
});
初始化索引並分配全局編碼
var index = new FlexSearch({ encode: "whitespace" });
直接調用全局編碼
var encoded = FlexSearch.encode("whitespace", "sample text");
- 混合/擴展多個編碼
FlexSearch.registerEncoder('mixed', function(str){
str = this.encode("icase", str); // built-in
str = this.encode("whitespace", str); // custom
// do something additional with str ...
return str;
});
- 添加自定義標記
在創建/初始化期間定義私有自定義標記
var index = new FlexSearch({
tokenize: function(str){
return str.split(/\s-\//g);
}
});
- 添加特定於語言的詞幹分析器和/或過濾器
Stemmer: several linguistic mutations of the same word (e.g. "run" and "running")
Filter: a blacklist of words to be filtered out from indexing at all (e.g. "and", "to" or "be")
在創建/初始化期間分配私有自定義詞幹分析器或過濾器
var index = new FlexSearch({
stemmer: {
// object {key: replacement}
"ational": "ate",
"tional": "tion",
"enci": "ence",
"ing": ""
},
filter: [
// array blacklist
"in",
"into",
"is",
"isn't",
"it",
"it's"
]
});
使用自定義詞幹分析器
var index = new FlexSearch({
stemmer: function(value){
// apply some replacements
// ...
return value;
}
});
使用自定義過濾器
var index = new FlexSearch({
filter: function(value){
// just add values with length > 1 to the index
return value.length > 1;
}
});
或者將詞幹分析器/過濾器全局分配給一種語言
Stemmer作為對象(鍵值對)傳遞,過濾為數組
FlexSearch.registerLanguage("us", {
stemmer: { /* ... */ },
filter: [ /* ... */ ]
});
或者使用一些預定義的詞幹分析器或首選語言的過濾器
...
現在您可以在創建/初始化期間分配內置詞幹分析器
var index_en = new FlexSearch({
stemmer: "en",
filter: "en"
});
var index_de = new FlexSearch({
stemmer: "de",
filter: [ /* custom */ ]
});
在Node.js中,只需要語言包文件即可使用它們
require("flexsearch.js");
require("lang/en.js");
require("lang/de.js");
總結
本文知識大致翻譯了部分使用方法,更加強大和完整的用法參考官方Github文檔,裡面有更加詳細的用法!
![FlexSearch.js:快速、零依賴的Javascript全文搜索庫](http://p2.ttnews.xyz/loading.gif)
閱讀更多 最美分享Coder 的文章
關鍵字: 算法 GitHub JavaScript