FAQ
Hello,

I am experiencing a strange behaviour when trying to query the same thing
via
BooleanQuery vs. via the know-it-all QueryParser class. Precisely, the index
contains
the document:
"12,Visual C++,4.2" with the field layout: ID,name,version(thus, "12" is
the ID field, "Visual C++"
is the name field and "4.2" is the version field).
The search string is "Visual C++" for the name field.

The following test, using QueryParser, goes fine:

public final void testUsingQueryParser()
{
IndexSearcher recordSearcher;
Query q;
QueryParser parser = new QueryParser("name", new StandardAnalyzer());
try
{
q = parser.parse("name:visual +name:c++");

Directory directory =
FSDirectory.getDirectory(<some_path_to_index>);
recordSearcher = new IndexSearcher(directory);

Hits h = recordSearcher.search(q);

assertEquals(1, h.length());
assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
}
catch(Exception exn)
{
fail("Exception occurred.");
}
}

But this one, using a BooleanQuery, fails.

public final void testUsingTermQuery()
{
IndexSearcher recordSearcher;
BooleanQuery bq = new BooleanQuery();

bq.add(new TermQuery(new Term("name", "visual")),
BooleanClause.Occur.SHOULD);
bq.add(new TermQuery(new Term("name", "c++")), BooleanClause.Occur.MUST);

try
{
Directory directory =
FSDirectory.getDirectory(<some_path_to_index>);
recordSearcher = new IndexSearcher(directory);

Hits h = recordSearcher.search(bq);

assertEquals(1, h.length()); // fails, saying it expects 0 !!!
assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
}
catch(Exception exn)
{
fail("Eexception occurred.");
}
}

Rewriting the BooleanQuery and taking toString() yields the same String
given to QueryParser.parse() in the first test. I am using Lucene 2.3.0. Can
somebody explain the difference ?
--
View this message in context: http://www.nabble.com/QueryParser-vs.-BooleanQuery-tp19306087p19306087.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org

Search Discussions

  • Ian Lea at Sep 4, 2008 at 9:14 am
    Have a look at the index with Luke to see what has actually been
    indexed. StandardAnalyzer may well be removing the pluses, or you may
    need to escape them. And watch out for case - Visual != visual in
    term query land.


    --
    Ian.

    On Thu, Sep 4, 2008 at 9:46 AM, bogdan71 wrote:

    Hello,

    I am experiencing a strange behaviour when trying to query the same thing
    via
    BooleanQuery vs. via the know-it-all QueryParser class. Precisely, the index
    contains
    the document:
    "12,Visual C++,4.2" with the field layout: ID,name,version(thus, "12" is
    the ID field, "Visual C++"
    is the name field and "4.2" is the version field).
    The search string is "Visual C++" for the name field.

    The following test, using QueryParser, goes fine:

    public final void testUsingQueryParser()
    {
    IndexSearcher recordSearcher;
    Query q;
    QueryParser parser = new QueryParser("name", new StandardAnalyzer());
    try
    {
    q = parser.parse("name:visual +name:c++");

    Directory directory =
    FSDirectory.getDirectory(<some_path_to_index>);
    recordSearcher = new IndexSearcher(directory);

    Hits h = recordSearcher.search(q);

    assertEquals(1, h.length());
    assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
    }
    catch(Exception exn)
    {
    fail("Exception occurred.");
    }
    }

    But this one, using a BooleanQuery, fails.

    public final void testUsingTermQuery()
    {
    IndexSearcher recordSearcher;
    BooleanQuery bq = new BooleanQuery();

    bq.add(new TermQuery(new Term("name", "visual")),
    BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("name", "c++")), BooleanClause.Occur.MUST);

    try
    {
    Directory directory =
    FSDirectory.getDirectory(<some_path_to_index>);
    recordSearcher = new IndexSearcher(directory);

    Hits h = recordSearcher.search(bq);

    assertEquals(1, h.length()); // fails, saying it expects 0 !!!
    assertEquals(12, Integer.parseInt(h.doc(0).get("ID")));
    }
    catch(Exception exn)
    {
    fail("Eexception occurred.");
    }
    }

    Rewriting the BooleanQuery and taking toString() yields the same String
    given to QueryParser.parse() in the first test. I am using Lucene 2.3.0. Can
    somebody explain the difference ?
    --
    View this message in context: http://www.nabble.com/QueryParser-vs.-BooleanQuery-tp19306087p19306087.html
    Sent from the Lucene - Java Users mailing list archive at Nabble.com.


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • 叶双明 at Sep 5, 2008 at 3:30 am
    Indeed, StandardAnalyzer removing the pluses, so analyse 'c++' to 'c'.
    QueryParser include Term that been analysed.
    And BooleanQuery include Term that hasn't been analysed.
    I think this is the difference between they.



    2008/9/4 Ian Lea <ian.lea@gmail.com>
    Have a look at the index with Luke to see what has actually been
    indexed. StandardAnalyzer may well be removing the pluses, or you may
    need to escape them. And watch out for case - Visual != visual in
    term query land.


    --
    Ian.

    On Thu, Sep 4, 2008 at 9:46 AM, bogdan71 wrote:

    Hello,

    I am experiencing a strange behaviour when trying to query the same thing
    via
    BooleanQuery vs. via the know-it-all QueryParser class. Precisely, the index
    contains
    the document:
    "12,Visual C++,4.2" with the field layout: ID,name,version(thus, "12" is
    the ID field, "Visual C++"
    is the name field and "4.2" is the version field).
    The search string is "Visual C++" for the name field.

    The following test, using QueryParser, goes fine:

    public final void testUsingQueryParser()
    {
    IndexSearcher recordSearcher;
    Query q;
    QueryParser parser = new QueryParser("name", new
    StandardAnalyzer());
    try
    {
    q = parser.parse("name:visual +name:c++");

    Directory directory =
    FSDirectory.getDirectory(<some_path_to_index>);
    recordSearcher = new IndexSearcher(directory);

    Hits h = recordSearcher.search(q);

    assertEquals(1, h.length());
    assertEquals(12,
    Integer.parseInt(h.doc(0).get("ID")));
    }
    catch(Exception exn)
    {
    fail("Exception occurred.");
    }
    }

    But this one, using a BooleanQuery, fails.

    public final void testUsingTermQuery()
    {
    IndexSearcher recordSearcher;
    BooleanQuery bq = new BooleanQuery();

    bq.add(new TermQuery(new Term("name", "visual")),
    BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("name", "c++")),
    BooleanClause.Occur.MUST);
    try
    {
    Directory directory =
    FSDirectory.getDirectory(<some_path_to_index>);
    recordSearcher = new IndexSearcher(directory);

    Hits h = recordSearcher.search(bq);

    assertEquals(1, h.length()); // fails, saying it
    expects 0 !!!
    assertEquals(12,
    Integer.parseInt(h.doc(0).get("ID")));
    }
    catch(Exception exn)
    {
    fail("Eexception occurred.");
    }
    }

    Rewriting the BooleanQuery and taking toString() yields the same String
    given to QueryParser.parse() in the first test. I am using Lucene 2.3.0.Can
    somebody explain the difference ?
    --
    View this message in context:
    http://www.nabble.com/QueryParser-vs.-BooleanQuery-tp19306087p19306087.html
    Sent from the Lucene - Java Users mailing list archive at Nabble.com.


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupjava-user @
categorieslucene
postedSep 4, '08 at 8:47a
activeSep 5, '08 at 3:30a
posts3
users3
websitelucene.apache.org

3 users in discussion

Ian Lea: 1 post Bogdan71: 1 post 叶双明: 1 post

People

Translate

site design / logo © 2022 Grokbase