有没有办法让FlexSearch (https://github.com/nextapps-de/flexsearch)只查找包含精确字符序列(包括数字字符)的结果?
文档在那里:https://flexsearch.net/docs/flexsearch-manual.pdf有一个叫做分析器的第35页,它似乎给出了如何做的提示,但在最后有一个TODO注释,当期望分析器列表时,我们可以尝试替代。
下面的线程只适用于普通字符:https://stackoverflow.com/a/69677055/2143734
或者,如果你知道任何等效的高效和浏览器兼容的搜索API,它只是我似乎错过了一些东西!
如果您输入C3、1、C0或4,您将获得结果,尽管这些数字都不会出现...
var data = Array.from(Array(1000000).keys()).map(String);
data.unshift("CA", "VIS-CD", "CATDIR-U", "UE5", "GAE");
(function() {
const index = new FlexSearch.Index({
tokenize: "full",
matcher: "default",
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>
发布于 2021-11-02 12:35:51
find only results that contains exact character sequence
好的,所以我没有使用Array.prototype.includes逻辑,而是使用了缓存系统来进行您想要的搜索:D
var data = Array.from(Array(1000000).keys()).map(String);
data.unshift("CA", "VIS-CD", "CATDIR-U", "UE5", "GAE");
//in essence, you can just change the condition(match_condition) to append a result based on whatever other condition you want
//the edit has me changing the match condition :D
//the edit also has me attempting a cache block(for speed)
(function() {
var suggestions = document.getElementById("suggestions");
var userinput = document.getElementById("userinput");
var cacheText={}, resultLimit=100, length=Symbol(null) //I'm doing caching based on how you want results(as in your snippet this part will lag a bit)
for(let i=0;i<data.length;i++){
if(typeof data[i]!="string"){continue}
for(let j=0;j<data[i].length;j++){
let string="" //for caching all direct text indexes
for(let k=0;k<=j;k++){string+=data[i][k]}
if(!cacheText[string]){
cacheText[string]={[data[i]]:true}
cacheText[string][length]=1 //length measurement
}
else{
if(cacheText[string][length]==resultLimit){continue}
cacheText[string][data[i]]=true
cacheText[string][length]++ //adding to length
}
}
}
userinput.addEventListener("input", show_results, true);
function show_results() {
var {value}=this, values={}
let match_condition=(text)=> cacheText[value]?cacheText[value][text]:false && value!="" //match condition(that you can change to whatever other logic)
for(let i=0; i<suggestions.children.length; i++){
//the purpose of this loop is to only remove elements that won't be on the new result list
let matchCondition=()=>
!suggestions.children[i]? true: //test(if child still exists)
match_condition(suggestions.children[i].innerText) //condition(in this case if data exactly includes user input)
while(!matchCondition()){ suggestions.children[i].remove() } //remove only those which won't match up to the condition
if(!suggestions.children[i]){break} //end loop(since if this is true, there is no child left)
values[suggestions.children[i].innerText]=true //to indicate that this value already exists when doing the second loop below
}
for(let i=0; i<data.length; i++){
//the purpose of this loop is to append UNIQUE results
if(match_condition(data[i]) && !values[data[i]]){
var child=document.createElement('div')
child.innerText=data[i]
suggestions.appendChild(child)
}
}
}
}());<body>
<input type="text" id="userinput" placeholder="Search by keyword..." />
<br></br>
<div id="suggestions"></div>
</body>
https://stackoverflow.com/questions/69808373
复制相似问题