1 package org.apache.lucene; 2 3 18 19 import java.io.IOException ; 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 PRIORITY_FIELD ="priority"; 31 static final String ID_FIELD ="id"; 32 static final String HIGH_PRIORITY ="high"; 33 static final String MED_PRIORITY ="medium"; 34 static final String LOW_PRIORITY ="low"; 35 36 public static void main(String [] 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 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 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 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 { 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 |