FAQ
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.



Govind
*

*--
No trees were harmed in the creation of this message, but several thousand
electrons were mildly inconvenienced.

Search Discussions

  • Adriano Crestani at Jul 4, 2011 at 5:17 pm
    Hi Govind,

    escape() method should only be used to escape term, not the query itself. If
    the user is entering the query, it's his responsibility to escape the query.
    On Mon, Jul 4, 2011 at 4:21 AM, govind bhardwaj wrote:

    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.



    Govind
    *

    *--
    No trees were harmed in the creation of this message, but several thousand
    electrons were mildly inconvenienced.
  • Govind bhardwaj at Jul 5, 2011 at 7:13 am
    Hi,
    I'm not getting you. escape() method takes String as an argument.

    The code snippet I am using is as follows

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

    *Please point out where I may be going wrong.

    Govind
    On Mon, Jul 4, 2011 at 10:16 AM, Adriano Crestani wrote:

    Hi Govind,

    escape() method should only be used to escape term, not the query itself.
    If
    the user is entering the query, it's his responsibility to escape the
    query.
    On Mon, Jul 4, 2011 at 4:21 AM, govind bhardwaj wrote:

    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.



    Govind
    *

    *--
    No trees were harmed in the creation of this message, but several thousand
    electrons were mildly inconvenienced.


    --
    No trees were harmed in the creation of this message, but several thousand
    electrons were mildly inconvenienced.
  • Adriano Crestani at Jul 5, 2011 at 4:12 pm
    Hi Govind,

    I think you are wrong by assuming escape should receive a query string as
    parameter. However, it's meant to receive a term as parameter, which will
    later be used to create a query. See the example below:

    // this is bad, since +, &, * and - will be escaped
    String query = "+lucene&solr -apache*";
    String escapedQuery = MultiQueryParser.escape(query);
    Query q = queryparser.parse(escapedQuery,...);

    // this is good, the operators +, * and - will not be escaped
    String term1 = "lucene&solr";
    String term2 = "apache"
    String term1Escaped = MultiQueryParser.escape(term1);
    String term2Escaped = MultiQueryParser.escape(term2);
    String escapedQuery = "+" + term1Escaped + " " + term2Escaped + "*";
    Query q = queryparser.parse(escapedQuery,...);

    As you can see in the second example, escape method should be used to escape
    terms, not the entire query. The method should only be used to make sure
    syntax characters are escaped and not treated as query operator, so it's
    wrong to assume escape() will not escape * in the query term*.
    On Tue, Jul 5, 2011 at 3:12 AM, govind bhardwaj wrote:

    Hi,
    I'm not getting you. escape() method takes String as an argument.

    The code snippet I am using is as follows

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

    *Please point out where I may be going wrong.

    Govind

    On Mon, Jul 4, 2011 at 10:16 AM, Adriano Crestani <
    adrianocrestani@gmail.com
    wrote:
    Hi Govind,

    escape() method should only be used to escape term, not the query itself.
    If
    the user is entering the query, it's his responsibility to escape the
    query.

    On Mon, Jul 4, 2011 at 4:21 AM, govind bhardwaj <govinsb90@gmail.com>
    wrote:
    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.



    Govind
    *

    *--
    No trees were harmed in the creation of this message, but several thousand
    electrons were mildly inconvenienced.


    --
    No trees were harmed in the creation of this message, but several thousand
    electrons were mildly inconvenienced.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupjava-user @
categorieslucene
postedJul 4, '11 at 8:22a
activeJul 5, '11 at 4:12p
posts4
users2
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase