You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Ro...@ferguson.com on 2006/07/16 20:25:19 UTC

MultiField Query

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

.
.
.
 
if (fsDir != null) {
 
 IndexSearcher is = new IndexSearcher(fsDir);
 MultiFieldQueryParser qp == new MultiFieldQueryParser(searchFields,new
StopAnalyzer());
 
 // Is a Lucene IndexSearcher object available
 
 if (qp != null) {
  
     String [] searchTerms   = {"silly",  "example"};             //
search terms
     String [] searchFields  = {"field1", "field2", "field3"};    //
search field names
 
     //   silly MUST occur in field1 - example SHOULD occur in field2
     BooleanClause.Occur [] booleanClauses = 
                             { BooleanClause.Occur.MUST,    
                               BooleanClause.Occur.MUST,
                               BooleanClause.Occur.MUST};
 
     Query query = qp.parse(searchTerms, searchFields, booleanClauses,
new StopAnalyzer());
 
     is.search(query);
 
 
Thanks
 
 

Re: MultiField Query

Posted by "Michael D. Curtin" <mi...@curtin.com>.
Rod.Madden@ferguson.com 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!

--MDC

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