1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.search.lucene;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.io.IOException;
20  
21  import java.util.Date;
22  
23  import org.apache.lucene.analysis.Analyzer;
24  import org.apache.lucene.document.Document;
25  import org.apache.lucene.index.Term;
26  import org.apache.lucene.queryParser.ParseException;
27  import org.apache.lucene.search.BooleanQuery;
28  import org.apache.lucene.search.IndexSearcher;
29  import org.apache.lucene.search.Query;
30  import org.apache.lucene.util.Version;
31  
32  /**
33   * <a href="LuceneHelperUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Harry Mark
37   * @author Bruno Farache
38   */
39  public class LuceneHelperUtil {
40  
41      public static void addDate(Document doc, String field, Date value) {
42          doc.add(LuceneFields.getDate(field, value));
43      }
44  
45      public static void addDocument(long companyId, Document document)
46          throws IOException {
47  
48          getLuceneHelper().addDocument(companyId, document);
49      }
50  
51      public static void addExactTerm(
52          BooleanQuery booleanQuery, String field, boolean value) {
53  
54          addExactTerm(booleanQuery, field, String.valueOf(value));
55      }
56  
57      public static void addExactTerm(
58          BooleanQuery booleanQuery, String field, double value) {
59  
60          addExactTerm(booleanQuery, field, String.valueOf(value));
61      }
62  
63      public static void addExactTerm(
64          BooleanQuery booleanQuery, String field, int value) {
65  
66          addExactTerm(booleanQuery, field, String.valueOf(value));
67      }
68  
69      public static void addExactTerm(
70          BooleanQuery booleanQuery, String field, long value) {
71  
72          addExactTerm(booleanQuery, field, String.valueOf(value));
73      }
74  
75      public static void addExactTerm(
76          BooleanQuery booleanQuery, String field, short value) {
77  
78          addExactTerm(booleanQuery, field, String.valueOf(value));
79      }
80  
81      public static void addExactTerm(
82          BooleanQuery booleanQuery, String field, String value) {
83  
84          getLuceneHelper().addExactTerm(booleanQuery, field, value);
85      }
86  
87      public static void addRequiredTerm(
88          BooleanQuery booleanQuery, String field, boolean value) {
89  
90          addRequiredTerm(booleanQuery, field, String.valueOf(value));
91      }
92  
93      public static void addRequiredTerm(
94          BooleanQuery booleanQuery, String field, double value) {
95  
96          addRequiredTerm(booleanQuery, field, String.valueOf(value));
97      }
98  
99      public static void addRequiredTerm(
100         BooleanQuery booleanQuery, String field, int value) {
101 
102         addRequiredTerm(booleanQuery, field, String.valueOf(value));
103     }
104 
105     public static void addRequiredTerm(
106         BooleanQuery booleanQuery, String field, long value) {
107 
108         addRequiredTerm(booleanQuery, field, String.valueOf(value));
109     }
110 
111     public static void addRequiredTerm(
112         BooleanQuery booleanQuery, String field, short value) {
113 
114         addRequiredTerm(booleanQuery, field, String.valueOf(value));
115     }
116 
117     public static void addRequiredTerm(
118         BooleanQuery booleanQuery, String field, String value) {
119 
120         addRequiredTerm(booleanQuery, field, value, false);
121     }
122 
123     public static void addRequiredTerm(
124         BooleanQuery booleanQuery, String field, String value, boolean like) {
125 
126         getLuceneHelper().addRequiredTerm(booleanQuery, field, value, like);
127     }
128 
129     public static void addTerm(
130             BooleanQuery booleanQuery, String field, long value)
131         throws ParseException {
132 
133         addTerm(booleanQuery, field, String.valueOf(value));
134     }
135 
136     public static void addTerm(
137             BooleanQuery booleanQuery, String field, String value)
138         throws ParseException {
139 
140         addTerm(booleanQuery, field, value, false);
141     }
142 
143     public static void addTerm(
144             BooleanQuery booleanQuery, String field, String value,
145             boolean like)
146         throws ParseException {
147 
148         getLuceneHelper().addTerm(booleanQuery, field, value, like);
149     }
150 
151     public static void delete(long companyId) {
152         getLuceneHelper().delete(companyId);
153     }
154 
155     public static void deleteDocuments(long companyId, Term term)
156         throws IOException {
157 
158         getLuceneHelper().deleteDocuments(companyId, term);
159     }
160 
161     public static Analyzer getAnalyzer() {
162         return getLuceneHelper().getAnalyzer();
163     }
164 
165     public static LuceneHelper getLuceneHelper() {
166         return _luceneHelper;
167     }
168 
169     public static String[] getQueryTerms(Query query) {
170         return getLuceneHelper().getQueryTerms(query);
171     }
172 
173     public static IndexSearcher getSearcher(long companyId, boolean readOnly)
174         throws IOException {
175 
176         return getLuceneHelper().getSearcher(companyId, readOnly);
177     }
178 
179     public static String getSnippet(Query query, String field, String s)
180         throws IOException {
181 
182         return getSnippet(
183             query, field, s, 3, 80, "...", StringPool.BLANK, StringPool.BLANK);
184     }
185 
186     public static String getSnippet(
187             Query query, String field, String s, int maxNumFragments,
188             int fragmentLength, String fragmentSuffix, String preTag,
189             String postTag)
190         throws IOException {
191 
192         return getLuceneHelper().getSnippet(
193             query, field, s, maxNumFragments, fragmentLength, fragmentSuffix,
194             preTag, postTag);
195     }
196 
197     public static Version getVersion() {
198         return getLuceneHelper().getVersion();
199     }
200 
201     public static void updateDocument(
202             long companyId, Term term, Document document)
203         throws IOException {
204 
205         getLuceneHelper().updateDocument(companyId, term, document);
206     }
207 
208     public static void shutdown() {
209         getLuceneHelper().shutdown();
210     }
211 
212     public void setLuceneHelper(LuceneHelper luceneHelper) {
213         _luceneHelper = luceneHelper;
214     }
215 
216     private static LuceneHelper _luceneHelper;
217 
218 }