KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > SearchTestForDuplicates


1 package org.apache.lucene;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.lucene.store.*;
22 import org.apache.lucene.document.*;
23 import org.apache.lucene.analysis.*;
24 import org.apache.lucene.index.*;
25 import org.apache.lucene.search.*;
26 import org.apache.lucene.queryParser.*;
27
28 class SearchTestForDuplicates {
29
30   static final String JavaDoc PRIORITY_FIELD ="priority";
31   static final String JavaDoc ID_FIELD ="id";
32   static final String JavaDoc HIGH_PRIORITY ="high";
33   static final String JavaDoc MED_PRIORITY ="medium";
34   static final String JavaDoc LOW_PRIORITY ="low";
35
36   public static void main(String JavaDoc[] args) {
37     try {
38       Directory directory = new RAMDirectory();
39       Analyzer analyzer = new SimpleAnalyzer();
40       IndexWriter writer = new IndexWriter(directory, analyzer, true);
41
42       final int MAX_DOCS = 225;
43
44       for (int j = 0; j < MAX_DOCS; j++) {
45         Document d = new Document();
46         d.add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY));
47         d.add(Field.Text(ID_FIELD, Integer.toString(j)));
48         writer.addDocument(d);
49       }
50       writer.close();
51
52       // try a search without OR
53
Searcher searcher = new IndexSearcher(directory);
54       Hits hits = null;
55
56       QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer);
57
58       Query query = parser.parse(HIGH_PRIORITY);
59       System.out.println("Query: " + query.toString(PRIORITY_FIELD));
60
61       hits = searcher.search(query);
62       printHits(hits);
63
64       searcher.close();
65
66       // try a new search with OR
67
searcher = new IndexSearcher(directory);
68       hits = null;
69
70       parser = new QueryParser(PRIORITY_FIELD, analyzer);
71
72       query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
73       System.out.println("Query: " + query.toString(PRIORITY_FIELD));
74
75       hits = searcher.search(query);
76       printHits(hits);
77
78       searcher.close();
79
80     } catch (Exception JavaDoc e) {
81       System.out.println(" caught a " + e.getClass() +
82                          "\n with message: " + e.getMessage());
83     }
84   }
85
86   private static void printHits( Hits hits ) throws IOException JavaDoc {
87     System.out.println(hits.length() + " total results\n");
88     for (int i = 0 ; i < hits.length(); i++) {
89       if ( i < 10 || (i > 94 && i < 105) ) {
90         Document d = hits.doc(i);
91         System.out.println(i + " " + d.get(ID_FIELD));
92       }
93     }
94   }
95
96 }
97
Popular Tags