使用標準查詢分析器(你可以看看的relevant documentation),你可以使用語法類似於數據庫的查詢,如:
(country:brazil OR country:france OR country:china) AND (other search criteria)
或者,簡化了一下:
country:(brazil OR france OR china) AND (other search criteria)
或者,Lucene還支持使用+/-而不是AND/OR語法編寫的查詢。我發現這個語法對於Lucene查詢更具表現力。這種形式的當量是:
+country:(brazil france china) +(other search criteria)
如果手動構造查詢,你確實可以嵌套BooleanQueries創建一個類似的結構,使用正確的BooleanClauses建立您指定的和/或邏輯:
Query countryQuery = new BooleanQuery();
countryQuery.add(new TermQuery(new Term("country","brazil")),BooleanClause.Occur.SHOULD);
countryQuery.add(new TermQuery(new Term("country","france")),BooleanClause.Occur.SHOULD);
countryQuery.add(new TermQuery(new Term("country","china")),BooleanClause.Occur.SHOULD);
Query otherStuffQuery = //Set up the other query here,
//or get it from a query parser, or something
Query rootQuery = new BooleanQuery();
rootQuery.add(countryQuery, BooleanClause.Occur.MUST);
rootQuery.add(otherStuffQuery, BooleanClause.Occur.MUST);
感謝您提供詳細的回覆,以及分組文檔的完美鏈接。 – Kevin 2013-03-25 23:46:16
謝謝!它確實幫了很大忙。 – 2017-10-11 14:32:02