FAQ
I have a BooleanQuery that looks like this:



BooleanQuery query = new BooleanQuery();



TermQuery term1 = new TermQuery(new Term(ID, "1234"));

TermQuery term2 = new TermQuery(new Term(ID, "2344"));

TermQuery term2 = new TermQuery(new Term(ID, "2323"));

TermQuery termLocation = new TermQuery(new Term(LOCATION, "A1"));

TermQuery termLanguage = new TermQuery(new Term(LANGUAGE, "ENU"));



query.add(term1, BooleanClause.Occur.Should);

query.add(term2, BooleanClause.Occur.Should);

query.add(term3, BooleanClause.Occur.Should);

query.add(termLocation, BooleanClause.Occur.MUST);

query.add(termLanguage, BooleanClause.Occur.MUST);



It produces this:



ID: 1234 ID:2344 ID:2323 +LOCATION:A1 +LANGUAGE:ENU



I just want results that have:

ID: 1234 OR 2344 OR 2323

LOCATION: A1

LANGUAGE: ENU



This query returns everything from my index. How would I create a query
that will only return results the must have LOCATION and LANGUAGE and
have only those three IDs.

Search Discussions

  • Michael D. Curtin at Jul 6, 2006 at 8:05 pm

    Van Nguyen wrote:

    I just want results that have:

    ID: 1234 OR 2344 OR 2323

    LOCATION: A1

    LANGUAGE: ENU

    This query returns everything from my index. How would I create a query
    that will only return results the must have LOCATION and LANGUAGE and
    have only those three IDs.
    I think you'll need to put the three ID-based TermQuerys into a
    sub-BooleanQuery instead of the top-level query. Something like this:

    BooleanQuery subquery = new BooleanQuery();
    subquery.add(term1, BooleanClause.Occur.Should);
    subquery.add(term2, BooleanClause.Occur.Should);
    subquery.add(term3, BooleanClause.Occur.Should);
    query.add(subquery, BooleanClause.Occur.MUST);

    Good luck!

    --MDC

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Van Nguyen at Jul 10, 2006 at 6:47 pm
    That worked... thanks!

    -----Original Message-----
    From: Michael D. Curtin
    Sent: Thursday, July 06, 2006 1:04 PM
    To: java-user@lucene.apache.org
    Subject: Re: BooleanQuery question

    Van Nguyen wrote:
    I just want results that have:

    ID: 1234 OR 2344 OR 2323

    LOCATION: A1

    LANGUAGE: ENU

    This query returns everything from my index. How would I create a query
    that will only return results the must have LOCATION and LANGUAGE and
    have only those three IDs.
    I think you'll need to put the three ID-based TermQuerys into a
    sub-BooleanQuery instead of the top-level query. Something like this:

    BooleanQuery subquery = new BooleanQuery();
    subquery.add(term1, BooleanClause.Occur.Should);
    subquery.add(term2, BooleanClause.Occur.Should);
    subquery.add(term3, BooleanClause.Occur.Should);
    query.add(subquery, BooleanClause.Occur.MUST);

    Good luck!

    --MDC

    ---------------------------------------------------------------------
    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
  • Dragon Fly at Jul 19, 2006 at 2:13 pm
    Thank you very much.
    From: "Erick Erickson" <erickerickson@gmail.com>
    Reply-To: java-user@lucene.apache.org
    To: java-user@lucene.apache.org
    Subject: Re: Empty fields ...
    Date: Wed, 19 Jul 2006 09:48:04 -0400

    Try something like

    TermDocs termDocs = reader.termDocs();
    termDocs.seek(new Term("<relevant field name here>", ""));
    while (termDocs.next()) {
    bits.set(termDocs.doc());
    }

    I *think* (and I'm remembering things folks wrote, haven't done this
    myself)
    that the empty string for the Term matches all terms. If not, you might
    have
    to wrap in in an outer loop that loops through all the elements, something
    like

    bits = new BitSet(reader.maxDoc());

    TermDocs termDocs = reader.termDocs();
    FilteredTermEnum fEnum = new FilteredTermEnum(reader, new
    Term(field, ""));

    for (Term term = null; (term = fEnum.term()) != null; fEnum.next())
    {
    termDocs.seek(new Term(
    field,
    term.text()));

    while (termDocs.next()) {
    bits.set(termDocs.doc());
    }
    }



    That said, it may be best for you to loop through each document and add
    that
    doc to the relevant filters if it had the fields you're interested in.
    You'd
    only be fetching each document once, so it'd only be one loop. I don't know
    enough about relative efficiencies to make a call here, probably depends
    upon how many docs you're dealing with. I'd stop at the first solution that
    works with acceptable performance unless you expect your corpus to grow
    significantly.... And since this is done in off hours, there's not a
    pressing reason to go with the very most efficient solution unless it takes
    a too long or you expect to have orders of magnitued more documents in your
    index eventually.

    Best
    Erick
    _________________________________________________________________
    Is your PC infected? Get a FREE online computer virus scan from McAfee®
    Security. http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Nicolas Labrot at Jul 19, 2006 at 3:59 pm
    Hi,

    I have made a simple class that parse an XML boolean expression to
    create predefined query .

    Here is an unroll construction from an xml topic which reduce the search
    on path "/bssrs" and exclude the file "abstract.htm" :

    subsubTermQuery1 = new TermQuery(new Term("FILE", "abstract.htm"));
    subsubBoolQuery1.add( new BooleanClause(subsubTermQuery1 ,
    BooleanClause.Occur.SHOULD) );
    subBoolQuery1.add( new BooleanClause(subsubBoolQuery1,
    BooleanClause.Occur.MUST_NOT) );

    subTermQuery2 = new TermQuery(new Term("PATH", "/bssrs"));
    subBoolQuery2.add( new BooleanClause(subTermQuery2 ,
    BooleanClause.Occur.SHOULD) );

    boolQuery.add( new BooleanClause(subBoolQuery1, BooleanClause.Occur.MUST) );
    boolQuery.add( new BooleanClause(subBoolQuery2,
    BooleanClause.Occur.MUST) );

    That give me after boolQuery.toString() this expression :
    +(-(FILE:abstract.htm)) +(PATH:/bssrs)

    If I search with boolQuery, Lucene doesn't find anything.
    If I modify by hand the query from "+(-(FILE:abstract.htm))
    +(PATH:/bssrs)" to "-(FILE:abstract.htm) +(PATH:/bssrs)", Lucene find
    the correct list of document.

    Does somebody know why ?

    Thanks in advance,

    Nicolas






    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Chris Hostetter at Jul 19, 2006 at 4:25 pm
    : If I search with boolQuery, Lucene doesn't find anything.
    : If I modify by hand the query from "+(-(FILE:abstract.htm))
    : +(PATH:/bssrs)" to "-(FILE:abstract.htm) +(PATH:/bssrs)", Lucene find
    : the correct list of document.
    :
    : Does somebody know why ?

    you can't have a boolean query containing only MUST_NOT clauses (which is
    what (-(FILE:abstract.htm)) is. it matches no documents, so the mandatory
    qualification on it causes the query to fail for all docs.


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



    -Hoss


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Nicolas Labrot at Jul 19, 2006 at 4:31 pm
    In my mind this restriction only apply on a query with just a MUST_NOT
    clause and not to a composed query. I've wrong.

    thanks a lot,

    Nicolas
    : If I search with boolQuery, Lucene doesn't find anything.
    : If I modify by hand the query from "+(-(FILE:abstract.htm))
    : +(PATH:/bssrs)" to "-(FILE:abstract.htm) +(PATH:/bssrs)", Lucene find
    : the correct list of document.
    :
    : Does somebody know why ?

    you can't have a boolean query containing only MUST_NOT clauses (which is
    what (-(FILE:abstract.htm)) is. it matches no documents, so the mandatory
    qualification on it causes the query to fail for all docs.


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



    -Hoss


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

    --
    Nicolas LABROT
    Département Ingénierie des Systèmes

    4DCONCEPT (http://www.4dconcept.fr)
    41-43 Avenue du Centre
    78180 Montigny le Bretonneux
    FRANCE
    Tél. : +33 (0) 1 61 08 50 20 / Fax. : +33 (0) 1 61 38 24 41

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Chris Hostetter at Jul 19, 2006 at 4:35 pm
    : In my mind this restriction only apply on a query with just a MUST_NOT
    : clause and not to a composed query. I've wrong.

    right ... it's an issue for any BooleanQuery, regardless of how that query
    may be wrapped in other boolean queries.




    -Hoss


    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Paul Borgermans at Jul 21, 2006 at 2:44 pm
    Hi
    you can't have a boolean query containing only MUST_NOT clauses (which is
    what (-(FILE:abstract.htm)) is. it matches no documents, so the mandatory
    qualification on it causes the query to fail for all docs.
    This is true for the search queries, but it makes sense in a query
    filter IMHO. I encountered this and had to add a MUST clause on the
    Filter with a trivial field in order not to break the Filter returning
    zero documents.

    Isn't this a conceptual bug in the case of constructing a filter along a query?

    Regards

    Paul

    ---------------------------------------------------------------------
    To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
    For additional commands, e-mail: java-user-help@lucene.apache.org
  • Chris Hostetter at Jul 22, 2006 at 8:16 am
    : >you can't have a boolean query containing only MUST_NOT clauses (which is
    : >what (-(FILE:abstract.htm)) is. it matches no documents, so the mandatory
    : >qualification on it causes the query to fail for all docs.
    :
    : This is true for the search queries, but it makes sense in a query
    : filter IMHO. I encountered this and had to add a MUST clause on the
    : Filter with a trivial field in order not to break the Filter returning
    : zero documents.
    :
    : Isn't this a conceptual bug in the case of constructing a filter along a
    : query?

    I'm a little confused by what you mean, but i suspect it realates to one
    of the following two issues...

    1) a QueryFilter just provides a simple wrapper arround a Query to make
    developing a "Filter" (ie: a cachable mechanism for identifing documents
    without scoring) easy if you already have a Query with the logic you want
    -- wrapping a QueryFilter arround a BooleanQuery doesn't change any
    intrinsic rules about how a BooleanQuery works -- it just uses the results
    in a differnet way.

    2) It's trivial to write a Filter that selects all items which do *not*
    match a Query -- by writting a Filter that flips the bits on a BitSet from
    another filter and wrapping that arround a QueryFilter -- but in the
    context of "search" you still need a Query to execute to positively select
    and score some documents. Your filter is only consulted to determine if
    those documents are allowed. It's the same problem as the BooleanQuery -
    to do a search you need to score some documents and saying "these
    documents should be excluded" isn't enough information. If all you care
    about is the set of documents (and not any scoring) then call the bits
    method on your filter directly.



    -Hoss


    ---------------------------------------------------------------------
    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
postedJul 6, '06 at 7:53p
activeJul 22, '06 at 8:16a
posts10
users6
websitelucene.apache.org

People

Translate

site design / logo © 2022 Grokbase