8

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?

  • I have no confidence I am looking at what you actually intended. Can you please lose the italics, and enclose code elements (especially your search terms, etc) [in backticks (\`)](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)? Looking at `*title:*Klammer[affe]**`, I have no idea which are supposed to be italics, and which are supposed to be actual asterisks – femtoRgon Nov 19 '13 at 17:53
  • possible duplicate of [how to index and search phrase query with special characters in lucene.net?](http://stackoverflow.com/questions/17610067/how-to-index-and-search-phrase-query-with-special-characters-in-lucene-net) – Dreamwalker Nov 20 '13 at 10:13

2 Answers2

20

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

Nick T
  • 311
  • 2
  • 5
  • 1
    Great suggestion. Use the built in implementation. Avoid writing this code + unit tests yourself. – Razor Oct 19 '16 at 00:27
7

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?

Dreamwalker
  • 3,032
  • 4
  • 30
  • 60
  • Don't forget that starting from Lucene 4.0 "/" is also a special character (used in regex). – Seb D. May 22 '14 at 15:45