Changeset 5536 for branches/OmeroEditor/src/search/IndexTermFinder.java
- Timestamp:
- 07/10/08 16:31:28 (4 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/OmeroEditor/src/search/IndexTermFinder.java
r5530 r5536 24 24 25 25 26 import java.io.IOException; 27 import java.util.ArrayList; 26 28 import java.util.Date; 27 29 30 import org.apache.lucene.index.CorruptIndexException; 28 31 import org.apache.lucene.index.IndexReader; 29 32 import org.apache.lucene.index.TermEnum; … … 61 64 public static String field = "contents"; 62 65 63 public static void main(String[] args) throws Exception{66 public static String[] getMatchingTerms(String searchString) { 64 67 65 68 66 69 Date start = new Date(); 67 70 68 String searchString = "ant";71 searchString = searchString.toLowerCase(); 69 72 70 73 String searchField = "contents"; 74 75 ArrayList<String> matchingTerms = new ArrayList<String>(); 71 76 72 IndexReader reader = IndexReader.open(index); 77 78 IndexReader reader; 79 try { 80 reader = IndexReader.open(index); 81 TermEnum terms = reader.terms(); 82 83 int termCounter = 0; 84 while (terms.next()) { 85 // ignore terms unless from the field of interest 86 String field = terms.term().field(); 87 if (! field.equals(searchField)) 88 continue; 89 90 String term = terms.term().toString(); 91 term = term.replaceFirst(field + ":", ""); 92 93 if (term.startsWith(searchString)) { 94 System.out.println(termCounter + " " + term); 95 termCounter++; 96 97 matchingTerms.add(term); 98 } 99 } 100 101 Date end = new Date(); 102 System.out.println("Search took " + (end.getTime() - start.getTime()) + 103 " milliseconds"); 104 105 reader.close(); 106 } catch (CorruptIndexException e) { 107 // TODO Auto-generated catch block 108 e.printStackTrace(); 109 } catch (IOException e) { 110 // TODO Auto-generated catch block 111 e.printStackTrace(); 112 } 73 113 74 TermEnum terms = reader.terms(); 75 76 int termCounter = 0; 77 while (terms.next()) { 78 // ignore terms unless from the field of interest 79 String field = terms.term().field(); 80 if (! field.equals(searchField)) 81 continue; 82 83 String term = terms.term().toString(); 84 term = term.replaceFirst(field + ":", ""); 85 86 if (term.startsWith(searchString)) { 87 System.out.println(termCounter + " " + term); 88 termCounter++; 89 } 114 String[] results = new String[matchingTerms.size()]; 115 int termIndex = 0; 116 for (String term : matchingTerms) { 117 results[termIndex] = term; 118 termIndex++; 90 119 } 91 120 92 Date end = new Date(); 93 System.out.println("Search took " + (end.getTime() - start.getTime()) + 94 " milliseconds"); 95 96 reader.close(); 121 return results; 97 122 } 98 123 }
