01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. The ASF licenses this file to You
04: * under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.search.IndexManagerImpl;
26:
27: import java.io.IOException;
28: import java.io.StringReader;
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 field, String input) {
44: if (input == null || field == null)
45: return null;
46: Analyzer analyer = IndexManagerImpl.getAnalyzer();
47: TokenStream tokens = analyer.tokenStream(field,
48: new StringReader(input));
49:
50: Token token = null;
51: Term term = null;
52: try {
53: token = tokens.next();
54: } catch (IOException e) {
55: }
56: if (token != null) {
57: String termt = token.termText();
58: term = new Term(field, termt);
59: }
60: return term;
61: }
62:
63: }
|