KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > search > IndexUtil


1 /*
2  * Created on Jul 20, 2003
3  *
4  * Authored by: Mindaugas Idzelis (min@idzelis.com)
5  */

6 package org.roller.business.search;
7
8 import org.apache.lucene.analysis.Analyzer;
9 import org.apache.lucene.analysis.Token;
10 import org.apache.lucene.analysis.TokenStream;
11 import org.apache.lucene.index.Term;
12 import org.roller.business.IndexManagerImpl;
13
14 import java.io.IOException JavaDoc;
15 import java.io.StringReader JavaDoc;
16
17 /**
18  * @author aim4min
19  *
20  * Class containing helper methods.
21  */

22 public class IndexUtil {
23
24     /**
25      * Create a lucene term from the first token of the input string.
26      *
27      * @param field The lucene document field to create a term with
28      * @param input The input you wish to convert into a term
29      * @return Lucene search term
30      */

31     public static final Term getTerm(String JavaDoc field, String JavaDoc input) {
32         if (input==null || field==null) return null;
33         Analyzer analyer = IndexManagerImpl.getAnalyzer();
34         TokenStream tokens = analyer.tokenStream(field,
35             new StringReader JavaDoc(input));
36         
37         Token token = null;
38         Term term = null;
39         try {
40             token = tokens.next();
41         } catch (IOException JavaDoc e) {}
42         if (token!=null) {
43             String JavaDoc termt = token.termText();
44             term = new Term(field,termt);
45         }
46         return term;
47     }
48
49 }
50
Popular Tags