Friday, 5 November 2021

How to match exact numerical pattern using FlexSearch

Is there a way to have FlexSearch (https://github.com/nextapps-de/flexsearch) find only results that contains exact character sequence including numerical characters ?

The following thread works fine only for plain characters : https://stackoverflow.com/a/69677055/2143734

Or if you know any equivalent efficient and browser compatible search API, it is just that I seem to miss something there !

If you input C3, 1, C0 or 4, you get results although none of these numbers appears...

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>


from How to match exact numerical pattern using FlexSearch

No comments:

Post a Comment