首页 > 解决方案 > 如何使用 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>

我可能在那里遗漏了一些东西,但我相信有一种方法可以让库生成一个更的索引和完全匹配,而不是这样一个匹配紧密的智能索引。

标签: javascriptsearch

解决方案


符合您的要求吗?

附言

为了避免浪费时间搜索差异:我只更改了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>


推荐阅读