javascript - 如何使用 FlexSearch 匹配精确模式
问题描述
有没有办法让 FlexSearch ( https://github.com/nextapps-de/flexsearch ) 只找到包含精确字符序列的结果?我尝试按照建议将分辨率设置为 25 并将阈值设置为 22,也使用深度,但我不断得到接近的单词(有时有点远,但至少长度匹配)但并不总是完全匹配我的序列。
我的索引词有时是 3 个字母的首字母缩写词,所以它可能会混淆上下文搜索。
如果您使用以下代码片段,通过输入 CTD,您将获得 CTD(ok)和 CDT(not ok)。如果您输入 CAA,您将获得 CAA(ok)和 Candidate(not ok)
var data =["CTD","CDT", "Candidate","CRT","CAA"];
(function(){
const index = new FlexSearch.Index({
charset: "latin:advanced",
tokenize: "full",
resolution : 25,
threshold : 22,
cache: true
});
for(var i = 0; i < data.length; i++){
index.add(i, data[i]);
}
var suggestions = document.getElementById("suggestions");
var userinput = document.getElementById("userinput");
userinput.addEventListener("input", show_results, true);
function show_results(){
var value = this.value;
var results = index.search(value);
var entry, childs = suggestions.childNodes;
var i = 0, len = results.length;
for(; i < len; i++){
entry = childs[i];
if(!entry){
entry = document.createElement("div");
suggestions.appendChild(entry);
}
entry.textContent = data[results[i]];
}
while(childs.length > len){
suggestions.removeChild(childs[i])
}
}
}());
<!doctype html>
<html>
<head>
<title>FlexSearch Sample</title>
<script src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@master/dist/flexsearch.compact.js"></script>
</head>
<body>
<input type="text" id="userinput" placeholder="Search by keyword...">
<br></br>
<div id="suggestions"></div>
</body>
</html>
我可能在那里遗漏了一些东西,但我相信有一种方法可以让库生成一个更笨的索引和完全匹配,而不是这样一个匹配紧密的智能索引。
解决方案
这符合您的要求吗?
附言
为了避免浪费时间搜索差异:我只更改了FlexSearch.Index
调用选项。
var data = ["CTD", "CDT", "Candidate", "CRT", "CAA"];
(function() {
const index = new FlexSearch.Index({
charset: "latin",
tokenize: "full",
matcher: "simple",
cache: true
});
for (var i = 0; i < data.length; i++) {
index.add(i, data[i]);
}
var suggestions = document.getElementById("suggestions");
var userinput = document.getElementById("userinput");
userinput.addEventListener("input", show_results, true);
function show_results() {
var value = this.value;
var results = index.search(value);
var entry, childs = suggestions.childNodes;
var i = 0,
len = results.length;
for (; i < len; i++) {
entry = childs[i];
if (!entry) {
entry = document.createElement("div");
suggestions.appendChild(entry);
}
entry.textContent = data[results[i]];
}
while (childs.length > len) {
suggestions.removeChild(childs[i])
}
}
}());
<!doctype html>
<html>
<head>
<title>FlexSearch Sample</title>
<script src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@master/dist/flexsearch.compact.js"></script>
</head>
<body>
<input type="text" id="userinput" placeholder="Search by keyword...">
<br></br>
<div id="suggestions"></div>
</body>
</html>
推荐阅读
- firebase - 如何在颤动的火力基地中创建进度条
- java - .JAR 文件在执行时是否不执行校验和检查?
- akka.net - Akka.Net,集群客户端:如何直接向集群中的参与者发送消息?
- oop - 在面向对象的编程中,无生命的对象应该有方法/行为吗?
- node.js - 从nodejs中的json对象创建嵌套的json对象
- vba - 从 Vbscript 启用 Outlook 宏
- css - 如何阻止 div 的内容随着屏幕尺寸的变化而缩小?
- jquery - 如何在 WordPress 的页脚中加载脚本
- angular - Datepicker:在小型显示器上截断
- c# - 如何检测一组值是否不同(唯一)而不将它们存储在内存中