首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用FlexSearch匹配精确的模式

如何使用FlexSearch匹配精确的模式
EN

Stack Overflow用户
提问于 2021-10-19 15:53:09
回答 2查看 527关注 0票数 0

是否有一种方法可以让FlexSearch (https://github.com/nextapps-de/flexsearch)只查找包含确切字符序列的结果?我试着像建议的那样把分辨率提高到25度,阈值在22度左右,也尽量深入地玩,但我总是得到一些接近的单词(有时有点远,但至少长度是匹配的),但并不总是完全符合我的顺序。

我的索引词有时是3个字母缩略词,所以它可能会扰乱上下文搜索。

如果您使用以下代码片段,通过输入CTD,您将得到CTD (ok)和CDT (not )。如果您进入CAA,您将得到CAA (ok)和候选人(not )。

代码语言:javascript
复制
        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])
                    }
                }
            }());
代码语言:javascript
复制
<!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>

我可能遗漏了一些东西,但我相信有一种方法可以让库生成一个、dumber、索引和确切的匹配,而不是这样的智能匹配。

EN

回答 2

Stack Overflow用户

发布于 2021-10-22 12:40:34

这符合你的要求吗?

附注:

为了避免浪费时间搜索差异:我只更改了FlexSearch.Index调用选项。

代码语言:javascript
复制
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])
    }
  }
}());
代码语言:javascript
复制
<!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>

票数 1
EN

Stack Overflow用户

发布于 2021-10-28 21:52:02

为了给Daniele Ricci的回答添加更多的想法:有时候“简单”匹配器仍然会发现假阳性(如果我们正在寻找严格的模式),如果请求包含数字。同样,我的方法可能太经验主义,无法得到一个完全正确的行为,但这里有一个例子,产生假阳性。将标记化更改为严格改进了这种情况,但随后就失去了对部分单词的匹配。默认匹配似乎是假阳性较少的那个..。还有一些还..。:(

如果你输入C3,或C0或4,你会得到它们.

代码语言:javascript
复制
var data = ["CA", "VIS-CD", "CATDIR-U"];

(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])
    }
  }
}());
代码语言:javascript
复制
<!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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69634046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档