Results 1 to 5 of 5
  1. #1
    PLehmann is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default search string with special characters

    Hello,

    I am using Lucene version 5.0.0.
    In my search string is a minus character like “test-”.
    I read that the minus sign is a special character in Lucene. So I have to escape that sign like that way:

    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

    To do that I use the QueryParser.escape method
    query = parser.parse(QueryParser.escape(searchString));

    I use the classic Analyzer because I noticed that the standard Analyzer has some problems with escaping special characters.

    The problem is that the Parser deletes the special characters and so the Query has the term
    content:test

    How can I set up the parser and searcher to search for the real value “test-“
    I also created my own query with the content test\- but that didn’t work as well.
    I recieved 0 results but my index has entries like
    Test-VRF
    Test-IPLS

    I am really confused about the problematic

    Regards
    Philipp
    Last edited by PLehmann; 03-04-2015 at 11:49 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: search string with special characters

    I am not familiar with Lucene but sometimes one needs to double escape the characters in the string. What happens if you try use the following?

    "test\\-"

    It all depends on what subsequent processors of the string expect to see after each process. It also depends on how the strings are being constructed. In Java you can't simply do this:

    String foo = "test\-"; // syntax error.

    You may even need to do this:

    String foo = "test\\\\-";

    But then again, you may already know this and/or I am completely off base.

    Regards,
    Jim
    Last edited by jim829; 03-04-2015 at 03:28 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: search string with special characters

    In this case that QueryParser.escape(searchString) should be adding the escaping characters though.

    You're feeding the result directly into another method; what does only the call to this static helper function output?

    Java Code:
    System.out.println(QueryParser.escape(searchString));
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    PLehmann is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: search string with special characters

    Quote Originally Posted by jim829 View Post
    I am not familiar with Lucene but sometimes one needs to double escape the characters in the string. What happens if you try use the following?

    "test\\-"

    It all depends on what subsequent processors of the string expect to see after each process. It also depends on how the strings are being constructed. In Java you can't simply do this:

    String foo = "test\-"; // syntax error.

    You may even need to do this:

    String foo = "test\\\\-";

    But then again, you may already know this and/or I am completely off base.

    Regards,
    Jim

    Thanks for your advice.
    The QueryParser.escape(s) Method does all the escaping stuff. In the lucene documentation it said that I just have to call this method but it didnt work.
    For example I insert the String test- and the methode give a String back test\-
    The problem is that the
    QueryParser.parse parse method creates a query like with the test. It delete the \- characters.
    If I create my own Query with Query query = new WildcardQuery("content", "test\-*") I get no result.
    But I have values like Test-VRF or Test-IPLS in the Index.

    The problem is that the searcher didn't find any of them.
    I didnt figure out why there are no matches.
    I expact that I got two result in minimum.

  5. #5
    PLehmann is offline Member
    Join Date
    Mar 2015
    Posts
    3
    Rep Power
    0

    Default Re: search string with special characters

    The goal to fix this issue is to store the value content in an NOT_ANALYZED way. Field fieldType = new Field(key.toLowerCase(),value, Field.Store.YES, Field.Index.NOT_ANALYZED);

    Someone who has the same problem has to take care how to store the contents in the index. To request the result create a query in this way searchString = QueryParser.escape(searchString); and use for example a WhitespaceAnalyzer.

Similar Threads

  1. Search with special characters returns nothing
    By girirajgupta in forum Lucene
    Replies: 5
    Last Post: 02-19-2014, 03:11 PM
  2. Replies: 3
    Last Post: 01-12-2012, 10:33 AM
  3. Special Characters on GUI
    By keo in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2011, 10:40 PM
  4. Search string for non-alphabetic characters
    By turnergirl24 in forum New To Java
    Replies: 5
    Last Post: 10-16-2009, 02:02 AM
  5. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •