Results 1 to 7 of 7
  1. #1
    djgovins is offline Member
    Join Date
    Jun 2011
    Location
    India
    Posts
    10
    Rep Power
    0

    Default Escaping Special Characters

    Hi,

    I am using Lucene version 3.1
    Previously I had trouble regarding special characters as when I entered "---" as my input, it gave the following error


    Caused by: org.apache.lucene.queryParser.ParseException: Encountered " "-" "- "" at line 1, column 1.
    Was expecting one of:
    "(" ...
    "*" ...
    <QUOTED> ...
    <TERM> ...
    <PREFIXTERM> ...
    <WILDTERM> ...
    "[" ...
    "{" ...
    <NUMBER> ...
    <TERM> ...
    "*" ...

    To overcome this, I used escape() method of the QueryParser and worked fine. But now, unlike previously, the search for "item*" yielded no results
    because I guess it escaped the asterisk character too. How should I go about preventing this from happening ? I am using MultiFieldQueryParser.

    Code snippet is as follows :

    String escaped = MultiFieldQueryParser.escape(queryString);
    Query query1 = parser.parse(escaped);
    TopDocs results = searcher.search(query);




    Govind

  2. #2
    djgovins is offline Member
    Join Date
    Jun 2011
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    Correction : In the line 3 of the snippet it is query1

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default

    Searching for "Lucene Escaping Special Characters" gave some very pertinent results. Is Google down in your part of the world?

    db

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by DarrylBurke View Post
    Searching for "Lucene Escaping Special Characters" gave some very pertinent results. Is Google down in your part of the world?
    The OP probably needs to try this.

  5. #5
    djgovins is offline Member
    Join Date
    Jun 2011
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    Any proper suggestions, I did the above crap before I posted my doubt onto the forums.

    For those who still have some difficulty understanding my doubt,

    I want a way to prevent escaping of characters like : and - that have significance in specifying the queries in Lucene search.

  6. #6
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi djgovins. Don't pay attention on useless posts.
    About your questions,
    First. I should you update version Lucene. Current version is 3.3.0. I had the same problems with earlier version.
    Second. I think that your question touch more design then coding. You can make some options in user interface, where user can inter search work and point some specifically criteria.
    For instance. You said that you have a field in your index, which has "Item" name and you pointer that user want to make searching by only this field. For this reason user inter word like this "Item:word*".
    It means that make search only by field "Item" all word, which can start with word. But when you handle this query by escape methods you escape all special chars by search and don't have correct result.
    I offer to make few fields in user interface, where user can point what key user want to search for what fields and what approach is used.
    I.e you have thee fields. fields-by-search, key-search, approach-search, where fields-by-search and approach-search has fix options and user can't inter bad chars in this fields.
    Then you make search like that
    Java Code:
     
    String query = field-by-search + ":" + QueryParser.escape(user-input) + approach-search;
    I hope this info help you.
    Regard.
    Last edited by Petr; 07-05-2011 at 05:41 PM.
    Skype: petrarsentev
    http://TrackStudio.com

  7. #7
    djgovins is offline Member
    Join Date
    Jun 2011
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    Thanks a lot Petr.

    Actually I escaped only the first character (that causes the Parser to give an exception), that worked.

Similar Threads

  1. Special Characters on GUI
    By keo in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2011, 10:40 PM
  2. Special characters question
    By alexalex1234 in forum Advanced Java
    Replies: 1
    Last Post: 03-24-2011, 11:33 AM
  3. Replies: 1
    Last Post: 11-30-2010, 05:41 PM
  4. XML with special characters
    By Kaizah in forum XML
    Replies: 1
    Last Post: 11-06-2009, 02:26 PM
  5. special characters
    By ravian in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 01:28 PM

Tags for this Thread

Posting Permissions

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