KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 /* Created on Jul 20, 2003 */
19 package org.apache.roller.business.search;
20
21 import org.apache.lucene.analysis.Analyzer;
22 import org.apache.lucene.analysis.Token;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.index.Term;
25 import org.apache.roller.business.IndexManagerImpl;
26
27 import java.io.IOException JavaDoc;
28 import java.io.StringReader JavaDoc;
29
30 /**
31  * Class containing helper methods.
32  * @author Mindaugas Idzelis (min@idzelis.com)
33  */

34 public class IndexUtil {
35     
36     /**
37      * Create a lucene term from the first token of the input string.
38      *
39      * @param field The lucene document field to create a term with
40      * @param input The input you wish to convert into a term
41      * @return Lucene search term
42      */

43     public static final Term getTerm(String JavaDoc field, String JavaDoc input) {
44         if (input==null || field==null) return null;
45         Analyzer analyer = IndexManagerImpl.getAnalyzer();
46         TokenStream tokens = analyer.tokenStream(field,
47                 new StringReader JavaDoc(input));
48         
49         Token token = null;
50         Term term = null;
51         try {
52             token = tokens.next();
53         } catch (IOException JavaDoc e) {}
54         if (token!=null) {
55             String JavaDoc termt = token.termText();
56             term = new Term(field,termt);
57         }
58         return term;
59     }
60     
61 }
62
Popular Tags