How to perform a lucene query containing special character using QueryParser?

前端 未结 2 1275
感动是毒
感动是毒 2021-01-03 20:14

Here is the thing. I have a term stored in the index, which contains special character, such as \'-\', the simplest code is like this:

Document doc = new Doc         


        
相关标签:
2条回答
  • 2021-01-03 21:00

    I am not sure about this , but I guess you need to escape - with \ . As per the Lucene docs.

    The "-" or prohibit operator excludes documents that contain the term after the "-" symbol.

    Again ,

    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.

    Also remember, some characters you'll need to escape twice if they have special meaning in Java.

    0 讨论(0)
  • 2021-01-03 21:03

    you can add the value as addValue() instead of add or addText. and then search in the special character with a KyewordAnalyzer instead of Standard Analyzer. or Add the data with addValue() and while searching the data in luke, replace the special character with the wild card search character (?). I have tried both ways and works

    0 讨论(0)
提交回复
热议问题