161
162{
163
164Documentd=hits.doc(i);
165
166Stringdname=d.get("title");
167
168System.out.print(dname+"");
169
170}
171
172System.out.println();
173
174System.out.println();
175
176}
177
178}
179
180}
181
182}
183
184在代码11.4中使用TermQuery进行检索的运行结果如图11-8所示。
185
186注意:
字段值是区分大小写的,因此在查询时必须注意大小写的匹配。
187
188从图11-8中可以看出,代码11.4两次分别以“word1”和“doc1”为关键字进行检索,并且都只得到了一个检索结果。
189
190在代码11.4中通过构建TermQuery的对象,两次完成了对关键字的查找。
两次查找过程中不同的是,第一次构建的TermQuery是查找“name”这个字段,而第二次构建的TermQuery则查找的是“title”这个字段。
191
19211.4.2“与或”搜索—BooleanQuery
193BooleanQuery也是实际开发过程中经常使用的一种Query。
它其实是一个组合的Query,在使用时可以把各种Query对象添加进去并标明它们之间的逻辑关系。
在本节中所讨论的所有查询类型都可以使用BooleanQuery综合起来。
BooleanQuery本身来讲是一个布尔子句的容器,它提供了专门的API方法往其中添加子句,并标明它们之间的关系,以下代码为BooleanQuery提供的用于添加子句的API接口:
194
195publicvoidadd(Queryquery,booleanrequired,booleanprohibited);
196
197注意:
BooleanQuery是可以嵌套的,一个BooleanQuery可以成为另一个BooleanQuery的条件子句。
198
199下面以11.5为例来介绍进行“与”操作的布尔型查询。
200
201代码11.5BooleanQueryTest1.java
202
203packagech11;
204
205importorg.apache.lucene.analysis.standard.StandardAnalyzer;
206
207importorg.apache.lucene.document.Document;
208
209importorg.apache.lucene.document.Field;
210
211importorg.apache.lucene.index.IndexWriter;
212
213importorg.apache.lucene.index.Term;
214
215importorg.apache.lucene.search.BooleanQuery;
216
217importorg.apache.lucene.search.Hits;
218
219importorg.apache.lucene.search.IndexSearcher;
220
221importorg.apache.lucene.search.Query;
222
223importorg.apache.lucene.search.TermQuery;
224
225
226
227publicclassBooleanQueryTest1
228
229{
230
231publicstaticvoidmain(String[]args)throwsException{
232
233//生成新的Document对象
234
235Documentdoc1=newDocument();
236
237doc1.add(Field.Text("name","word1word2word3"));
238
239doc1.add(Field.Keyword("title","doc1"));
240
241
242
243Documentdoc2=newDocument();
244
245doc2.add(Field.Text("name","word1word4word5"));
246
247doc2.add(Field.Keyword("title","doc2"));
248
249
250
251Documentdoc3=newDocument();
252
253doc3.add(Field.Text("name","word1word2word6"));
254
255doc3.add(Field.Keyword("title","doc3"));
256
257
258
259//生成索引书写器
260
261IndexWriterwriter=newIndexWriter("c:
\\index",newStandardAnalyzer(),true);
262
263//添加到索引中
264
265writer.addDocument(doc1);
266
267writer.addDocument(doc2);
268
269writer.addDocument(doc3);
270
271writer.close();
272
273
274
275Queryquery1=null;
276
277Queryquery2=null;
278
279BooleanQueryquery=null;
280
281Hitshits=null;
282
283
284
285//生成IndexSearcher对象
286
287IndexSearchersearcher=newIndexSearcher("c:
\\index");
288
289
290
291query1=newTermQuery(newTerm("name","word1"));
292
293query2=newTermQuery(newTerm("name","word2"));
294
295
296
297//构造一个布尔查询
298
299query=newBooleanQuery();
300
301
302
303//添加两个子查询
304
305query.add(query1,true,false);
306
307query.add(query2,true,false);
308
309
310
311hits=searcher.search(query);
312
313printResult(hits,"word1和word2");
314
315
316
317}
318
319
320
321publicstaticvoidprintResult(Hitshits,Stringkey)throwsException
322
323{
324
325System.out.println("查找\""+key+"\":
");
326
327if(hits!
=null)
328
329{
330
331if(hits.length()==0)
332
333{
334
335System.out.println("没有找到任何结果");
336
337}
338
339else
340
341{
342
343System.out.println("找到"+hits.length()+"个结果");
344
345for(inti=0;i346
347{
348
349Documentd=hits.doc(i);
350
351Stringdname=d.get("title");
352
353System.out.print(dname+"");
354
355}
356
357System.out.println();
358
359System.out.println();
360
361}
362
363}
364
365}
366
367}
368
369代码11.5首先构造了两个TermQuery,然后构造了一个BooleanQuery的对象,并将两个TermQuery当成它的查询子句加入Boolean查询中。
370
371再来看一下BooleanQuery的add方法,除了它的第一个参数外,它还有另外两个布尔型的参数。
第1个参数的意思是当前所加入的查询子句是否必须满足,第2个参数的意思是当前所加入的查询子句是否不需要满足。
这样,当这两个参数分别选择true和false时,会有4种不同的组合。
372
373true&false:
表明当前加入的子句是必须要满足的。
374
37