lucaong / minisearch

Tiny and powerful JavaScript full-text search engine for browser and Node
https://lucaong.github.io/minisearch/
MIT License
4.81k stars 137 forks source link

Question: How to set searchOptions to search keywords? #194

Closed Daotin closed 1 year ago

Daotin commented 1 year ago

Hi, I have a problem, can you help me. Thanks.

my doc is:

[
 {
    "name": "一路向北",
    "album": "十一月的萧邦",
    "lrc": [
      "倒视镜里的世界",
      "越来越远的道别",
      "你转身向背"
    ]
  }
]

when I search 倒视镜里的世界 , it can be matched. But when I search 倒视镜, It will not be match.

So, How can it be matched when searching for 倒视镜? Finally, I want to achieve the effect of full-text search.

I want use fuzzy to solve it, but I dont know how to set.


Part of the code is as follows (using Vue3):

const doc = [
 {
    "name": "一路向北",
    "album": "十一月的萧邦",
    "lrc": [
      "倒视镜里的世界",
      "越来越远的道别",
      "你转身向背"
    ]
  }
];

const miniSearch = shallowRef(
  new MiniSearch({
    idField: "name",
    fields: ["album", "lrc"],
    storeFields: ["name", "lrc"],
    searchOptions: {},
    extractField: (document, fieldName) => {
      if (Array.isArray(document[fieldName])) {
        return document[fieldName].join(" ");
      } else {
        return document[fieldName];
      }
    },
  })
)

miniSearch.value.addAll(doc);

// when click search
function handleSearch() {
  results.value = miniSearch.value.search('倒视镜')
}

Appreciate your help, Thanks :-)

lucaong commented 1 year ago

Hi @Daotin , If I understand correctly, what you want is prefix search:

const miniSearch = shallowRef(
  new MiniSearch({
    idField: "name",
    fields: ["album", "lrc"],
    storeFields: ["name", "lrc"],
    searchOptions: {
      prefix: true, // prefix search, “foo” matches “foobar”
      fuzzy: true // fuzzy search, “foo” matches “fox”
    },
    extractField: (document, fieldName) => {
      if (Array.isArray(document[fieldName])) {
        return document[fieldName].join(" ");
      } else {
        return document[fieldName];
      }
    },
  })
)

Does this meet your needs?

Daotin commented 1 year ago

Hi @lucaong, Maybe I didn't express clearly. for example, a string like 你转身向背, when I search 转身 ( The Key words in the middle of the whole sentence), I hope to be able to match. So, probably not the prefix search.

lucaong commented 1 year ago

Ah, I see. Then unfortunately MiniSearch by default does not offer this feature. The options allow to:

I hope this helps :)

lucaong commented 1 year ago

Here is a quick example of what I mean: https://jsbin.com/poloselexa/1/edit?js,console

In the example, I use processTerm to index all suffixes of length > 3 of each term, then apply prefix search. This results in matching also partial terms in the middle of words.

Daotin commented 1 year ago

OK. It works! Thank you and Have a great day. 😀

lucaong commented 1 year ago

Great to hear it helped :)