How about using MultiFieldQueryParser. Here is a short main I wrote: Directory dir = new RAMDirectory(); Analyzer analyzer = new StandardAnalyzer(); IndexWriter writer = new IndexWriter(dir, analyzer); Document doc = new Document(); doc.add(new Field("field1", "green tree", Store.NO, Index.TOKENIZED )); writer.addDocument(doc); doc = new Document(); doc.add(new Field("field2", "green tree", Store.NO, Index.TOKENIZED )); writer.addDocument(doc); writer.close();
IndexSearcher searcher = new IndexSearcher(dir); MultiFieldQueryParser qp = new MultiFieldQueryParser(new String[] { "field1", "field2" }, analyzer); Query q = qp.parse("\"green tree\""); if (q instanceof BooleanQuery /* Basically this should almost always be true */) { BooleanClause[] clauses = ((BooleanQuery) q).getClauses(); for (int i = 0; i < clauses.length; i++) { clauses[i].setOccur(Occur.SHOULD); /* This is their setting by default though */ } } Hits hits = searcher.search(q); System.out.println(hits.length()); /* Should print 2. */ searcher.close(); On Nov 22, 2007 3:26 PM, Erick Erickson <[EMAIL PROTECTED]> wrote: > The semantics of the phrase query you're constructing probably aren't > what you think. As best I can infer, you are trying to do something > like > > "green tree" in field1 > or > "green tree" in field 2 > > but that's not even close to what you're constructing. > > It would help a show what the query you want is in some form > like that above before trying to code it, because the actual > query you're making is something like asking for the phrase > "word1 in field1 word1 in field2 word2 in field1 word2 in field2" > > Actually, I can't render the semantics of what you're doing int English. > And Lucene can't parse it either. > > I suspect you want something like > PhraseQuery pq1 > PhraseQuery pq2 > for (String word : words) { > pq1.add(...); > pq2.add(...) > } > BooleanQuery bq(). > bq.add(p1, .... SHOULD); > bq.add(p2, ... SHOULD); > > Index.search(bq); > > Best > Erick > > On Nov 22, 2007 7:17 AM, Rapthor <[EMAIL PROTECTED]> wrote: > > > > > There is no option to provide an Occur.SHOULD to the PhraseQuery. So > where > > does it go? I changed the source to look like this: > > > > PhraseQuery pq = new PhraseQuery(); > > for (String word : words) { > > for (String field : fields) { > > pq.add(new Term(field, word)); > > } > > } > > Hits hits = indexSearcher.search(pq); > > > > However, I get an exception: > > java.lang.IllegalArgumentException: All phrase terms must be in the same > > field: description:green tree > > > > I don't understand how to a) search for combinations of words like > "green > > tree", b) search in multiple fields (description, text, ...) and c) > search > > by a SHOULD restriction. > > -- > > View this message in context: > > http://www.nabble.com/AND-query-in-SHOULD-tf4855719.html#a13895700 > > Sent from the Lucene - Java Users mailing list archive at Nabble.com. > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > -- Regards, Shai Erera