lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael D. Curtin" <>
Subject Re: MultiField Query
Date Mon, 17 Jul 2006 00:03:30 GMT wrote:
> I am using Lucene 2.0 and trying to use the MultiFieldQueryParser 
> in my search.
> I want to limit my search to documents which have "silly"
> in "field1" ...within that subset of documents, I want documents which
> have
> "example" in "field2" OR "field3"
> The code fragment below is my attempt at this ...code blows
> on the : Query query = qp.parse(... 
> statement ...
> Besides blowing, I believe that the MUST / MUST for field2 and field3
> is inappropriate ...I really want to say ..if field1 has "silly"
> return documents with "example" in field2 OR field3.
> Any suggestions for accomplishing this ? 
> Someone suggested BooleanQuery but I was not sure how to merge
> that concept in with the MultiFieldQueryParser ..

I can think of two ways to do what you want, one with a parsed query and one 
without.  With a parsed query:

Query q = QueryParser.parse("+field1:silly +(field2:example field3:example)",
                             "field1", new StopAnalyzer());

If you already know the exact structure of the query, then you can just build 
it up yourself, i.e. without the QueryParser, like so:

TermQuery t1 = new TermQuery(new Term("field1", "silly"));
TermQuery t2 = new TermQuery(new Term("field2", "example"));
TermQuery t3 = new TermQuery(new Term("field3", "example"));
Query subq = new BooleanQuery();
subq.add(t2, BooleanClause.Occur.Should);
subq.add(t3, BooleanClause.Occur.Should);
Query q = new BooleanQuery();
q.add(t1,   BooleanClause.Occur.MUST);
q.add(subq, BooleanClause.Occur.MUST);

Good luck!


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message