olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.87k stars 547 forks source link

Permissive mode (ignore commands without needing escape) #435

Closed Arteneko closed 4 years ago

Arteneko commented 4 years ago

I have a blog which contains a few pages with the characters : or ^ in their title.

For example, searching for php: raises an exception, which forces me to return an empty search result set, while the string php: ... exists in a few titles and category labels.

Is there a way to let lunr consider command characters such as : as "normal" characters in case of error, i.e. to also return a result set with the closest approximate search?

olivernn commented 4 years ago

'Special' characters can be escaped by using a leading backslash, e.g.

php\:

Does this solve the problem you are having while searching?

Arteneko commented 4 years ago

My issue is around UX.

I got the part where I have to escape, but if I use the library to provide some search suggestions to users which may be limited in terms of knowledge around IT, the biggest question I'll receive would be "why does php work but php: suddenly hides everything?"

olivernn commented 4 years ago

My issue is around UX.

I'm not sure that lunr can solve specific UX problems related to you application.

At its most simplest you can use the lunr.QueryParseError that is thrown when a malformed (in your case unescaped) query is passed to display an appropriate error message.

Alternatively you could implement your search on top of lunr.Index#Query which offers a more programmatic way of constructing queries using a lunr.Query. You could then use whatever interface provides the UX you are looking for to drive the queries.