1 package org.contineo.searchengine; 2 3 import java.io.BufferedReader ; 4 import java.io.InputStreamReader ; 5 6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.de.GermanAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.queryParser.QueryParser; 10 import org.apache.lucene.search.Hits; 11 import org.apache.lucene.search.IndexSearcher; 12 import org.apache.lucene.search.Query; 13 import org.apache.lucene.search.Searcher; 14 import org.contineo.core.config.SettingConfigurator; 15 16 class SearchFiles 17 { 18 public static void main(String [] args) 19 { 20 SettingConfigurator conf = new SettingConfigurator(); 21 try 22 { 23 Searcher searcher = new IndexSearcher(conf.getValue("indexdir")); 24 Analyzer analyzer = new GermanAnalyzer(); 25 26 BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); 27 while (true) 28 { 29 System.out.print("Query: "); 30 String line = in.readLine(); 31 32 if (line == null || line.length() == 0) 33 break; 34 35 Query query = QueryParser.parse(line, "content", analyzer); 36 System.out.println("Searching for: " + query.toString("content")); 37 38 Hits hits = searcher.search(query); 39 String summary = ""; 40 41 System.out.println(hits.length() + " total matching documents"); 42 43 final int HITS_PER_PAGE = 10; 44 for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) 45 { 46 int end = Math.min(hits.length(), start + HITS_PER_PAGE); 47 for (int i = start; i < end; i++) 48 { 49 Document doc = hits.doc(i); 50 String path = doc.get("path"); 51 summary = doc.get("summary"); 52 if (path != null) 54 { 55 System.out.println(i + ". " + path); 56 System.out.println(i + ". " + hits.score(i)); 57 System.out.println(i + ". " + summary); 58 } 59 else 60 { 61 String url = doc.get("path"); 62 summary = doc.get("summary"); 63 if (url != null) 65 { 66 System.out.println(i + ". " + url); 67 System.out.println("score - " + hits.score(i)); 68 System.out.println("title - " + doc.get("name")); 69 System.out.println("summary - " + summary); 70 } 71 else 72 { 73 System.out.println(i + ". " + "No Path or URL for this document"); 74 } 75 } 76 } 77 78 if (hits.length() > end) 79 { 80 System.out.print("more (y/n) ? "); 81 line = in.readLine(); 82 if (line == null || line.length() == 0 || line.charAt(0) == 'n') 83 break; 84 } 85 } 86 } 87 searcher.close(); 88 } 89 catch (Exception e) 90 { 91 System.out.println(" caught a " + e.getClass() + 92 "\n with message: " + e.getMessage()); 93 } 94 } 95 } 96 | Popular Tags |