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