Escaping special characters in wildcard search using Lucene.NET

10,847

Solution 1

From the lucene documentation

Escaping Special Characters

Lucene supports escaping special characters that are part of the query syntax. The current list special characters are

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /

To escape these character use the \ before the character. For example to search for (1+1):2 use the query:

\(1\+1\)\:2

So your query should be *Klammer\[affe\]*

But the standard analyzer deletes those characters so you need to index the original content differently.

See this related questions answer https://stackoverflow.com/a/17628127/956658. Another question with some info on changing the analyzing method How to perform a lucene query containing special character using QueryParser?

Solution 2

You could also use the Lucene implementation QueryParser.Escape(searchQuery).

Share:
10,847
Admin
Author by

Admin

Updated on July 17, 2022

Comments

  • Admin
    Admin almost 2 years

    I have looked for a long time for escaping special characters like #, {, , [, ], ... while in wildcard search in Lucene.NET 3.0.3.0, but I can´t find any possible solutions.

    I have index my documents using StandardAnalyzer. The field "title" has the attributs Field.Store.YES and Field.Index.ANALYZED.
    While searching I called MultiFieldQueryParser.Escape for my searchterm. The escaped query looks right but parsing the term remove the escaping characters. So my search can not find any results.

    searchterm: Klammer[affe]
    escaped searchterm: *Klammer\\[affe\\]*
    after parsing: title:*Klammer[affe]*

    So, how can I escape special characters in wildcard-Search?