Overview
Fuse.js is a search engine written in JavaScript.
It is very useful as a search engine when building frontend-only applications.
This time, when implementing exact non-match search using Fuse.js, I needed to craft the query carefully, so here are my notes.
Advanced Search
Fuse.js supports various types of searches, including exact/partial match and partial non-match. These are described on the following page.
https://fusejs.io/examples.html#extended-search
A Japanese translation is also available in the following article:
https://qiita.com/Sashimimochi/items/4972b3dc333c6e5fb866#Extended Search
However, for exact non-match search, the query needed some special handling.
Exact Non-Match Search
For example, to search for items where the label field does not contain the exact string “悪党” (villain), I was able to partially achieve this with the following query. It searches for items that “do not start with 悪党” OR “do not end with 悪党.”
{
"$or": [
{
"label": "!^悪党" # "does not start with 悪党"
},
{
"label": "!悪党$" # "does not end with 悪党"
}
]
}
However, note that this query is not perfect – it will also exclude items with values like “悪党と悪党” (villain and villain).
Summary
There may be some misunderstandings in my explanation, but I hope this serves as a useful reference.