• Views
  • Iteration Report
  • My Iteration Report
  •  
OMERO.clients
  • Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Change
  • Next Change →

Changeset 5536 for branches/OmeroEditor/src/search/IndexTermFinder.java

Show
Ignore:
Timestamp:
07/10/08 16:31:28 (4 months ago)
Author:
will
Message:

New tree model...

Files:
1 modified

  • branches/OmeroEditor/src/search/IndexTermFinder.java (modified) (2 diffs)

Legend:

Unmodified
Added
Removed
  • branches/OmeroEditor/src/search/IndexTermFinder.java

    r5530 r5536  
    2424 
    2525 
     26import java.io.IOException; 
     27import java.util.ArrayList; 
    2628import java.util.Date; 
    2729 
     30import org.apache.lucene.index.CorruptIndexException; 
    2831import org.apache.lucene.index.IndexReader; 
    2932import org.apache.lucene.index.TermEnum; 
    … …  
    6164        public static String field = "contents"; 
    6265         
    63           public static void main(String[] args) throws Exception { 
     66          public static String[] getMatchingTerms(String searchString) { 
    6467                   
    6568                   
    6669                  Date start = new Date(); 
    6770                   
    68                 String searchString = "ant"; 
     71                searchString = searchString.toLowerCase(); 
    6972                   
    7073                String searchField = "contents"; 
     74                 
     75                ArrayList<String> matchingTerms = new ArrayList<String>(); 
    7176 
    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                } 
    73113 
    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++; 
    90119            } 
    91120             
    92             Date end = new Date(); 
    93               System.out.println("Search took " + (end.getTime() - start.getTime()) + 
    94                           " milliseconds"); 
    95                
    96             reader.close(); 
     121            return results; 
    97122          } 
    98123} 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/