Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to escape a special character? #271

Closed
meltuhamy opened this issue May 30, 2017 · 10 comments
Closed

How to escape a special character? #271

meltuhamy opened this issue May 30, 2017 · 10 comments

Comments

@meltuhamy
Copy link

e.g. Is it possible to search for the character '^' using the search method? I do not want it to be interpreted as a search modifier (boost).

@olivernn
Copy link
Owner

At the moment the search string does not provide support for escaping characters. This is something that I want to add at some point.

In the mean time you can use lunr.Index#query directly to perform a search with a '^' character:

idx.query(function (q) {
  q.term("foo^")
})

@meltuhamy
Copy link
Author

meltuhamy commented May 30, 2017

Thanks @olivernn , I actually looked at that before but then I was stuck trying to get the best of both worlds, e.g. title:n^2 - where I want to search the field 'title' but I want to search for the term 'n^2' without boosting anything. Any ideas as to how I can do this?

@olivernn
Copy link
Owner

Everything that is possible with a search string is possible with the lunr.Index#query method, lunr.Index#search is implemented on top of it.

idx.query(function (q) {
  q.term("n^2", { fields: ["title"] })
})

The documentation shows how to add fields, boosts etc.

@meltuhamy
Copy link
Author

great, thanks @olivernn . Do you know how I can escape the wildcard * character? q.term('*') does a wildcard search...

@olivernn
Copy link
Owner

Its not possible to escape the wildcard character at the moment, and I think it'll be a bit tricky to implement. I'll think about the best way to do so when implementing escape sequences in the search string, but "*" has special meaning in a number of places that might be harder to sidestep.

@olivernn
Copy link
Owner

olivernn commented Jun 4, 2017

The latest alpha (2.1.0-alpha.2) includes support for escape query characters, e.g "12:00pm" will do a search for "12:00pm" rather than complain about a missing field called "12".

This does not include support for escaping asterisks "*", that requires some more fundamental changes that I'm not inclined to work on, yet.

As ever, try it out and let me know any feedback.

@olivernn
Copy link
Owner

I've just published 2.1.0 which includes support for escaping special characters.

@jeckep
Copy link

jeckep commented Aug 27, 2019

Excuse me, but I couldn't find the instruction how to use escaping for special charactes, could you please point me.

I try to search term "C#". But

idx.query(function (q) {
  q.term("C#")
});

didn't work.
I am also searching for possibility to search terms with characters: ":", "."

Thank you.

@wangpin34
Copy link

I cannot find any instruction about escaping the modifiers as well. @olivernn Any updates? thanks.

@wereHamster
Copy link

@wangpin34 I found this in our code, not sure where I got it from:

str.replace(/[~^\-+]/g, (x) => `\\${x}`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants