1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  
31  /**
32   * <a href="LuceneHelperUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Harry Mark
36   * @author Bruno Farache
37   */
38  public class LuceneHelperUtil {
39  
40      public static void addDate(Document doc, String field, Date value) {
41          doc.add(LuceneFields.getDate(field, value));
42      }
43  
44      public static void addDocument(long companyId, Document document)
45          throws IOException {
46  
47          getLuceneHelper().addDocument(companyId, document);
48      }
49  
50      public static void addExactTerm(
51          BooleanQuery booleanQuery, String field, boolean value) {
52  
53          addExactTerm(booleanQuery, field, String.valueOf(value));
54      }
55  
56      public static void addExactTerm(
57          BooleanQuery booleanQuery, String field, double value) {
58  
59          addExactTerm(booleanQuery, field, String.valueOf(value));
60      }
61  
62      public static void addExactTerm(
63          BooleanQuery booleanQuery, String field, int value) {
64  
65          addExactTerm(booleanQuery, field, String.valueOf(value));
66      }
67  
68      public static void addExactTerm(
69          BooleanQuery booleanQuery, String field, long value) {
70  
71          addExactTerm(booleanQuery, field, String.valueOf(value));
72      }
73  
74      public static void addExactTerm(
75          BooleanQuery booleanQuery, String field, short value) {
76  
77          addExactTerm(booleanQuery, field, String.valueOf(value));
78      }
79  
80      public static void addExactTerm(
81          BooleanQuery booleanQuery, String field, String value) {
82  
83          getLuceneHelper().addExactTerm(booleanQuery, field, value);
84      }
85  
86      public static void addRequiredTerm(
87          BooleanQuery booleanQuery, String field, boolean value) {
88  
89          addRequiredTerm(booleanQuery, field, String.valueOf(value));
90      }
91  
92      public static void addRequiredTerm(
93          BooleanQuery booleanQuery, String field, double value) {
94  
95          addRequiredTerm(booleanQuery, field, String.valueOf(value));
96      }
97  
98      public static void addRequiredTerm(
99          BooleanQuery booleanQuery, String field, int value) {
100 
101         addRequiredTerm(booleanQuery, field, String.valueOf(value));
102     }
103 
104     public static void addRequiredTerm(
105         BooleanQuery booleanQuery, String field, long value) {
106 
107         addRequiredTerm(booleanQuery, field, String.valueOf(value));
108     }
109 
110     public static void addRequiredTerm(
111         BooleanQuery booleanQuery, String field, short value) {
112 
113         addRequiredTerm(booleanQuery, field, String.valueOf(value));
114     }
115 
116     public static void addRequiredTerm(
117         BooleanQuery booleanQuery, String field, String value) {
118 
119         addRequiredTerm(booleanQuery, field, value, false);
120     }
121 
122     public static void addRequiredTerm(
123         BooleanQuery booleanQuery, String field, String value, boolean like) {
124 
125         getLuceneHelper().addRequiredTerm(booleanQuery, field, value, like);
126     }
127 
128     public static void addTerm(
129             BooleanQuery booleanQuery, String field, long value)
130         throws ParseException {
131 
132         addTerm(booleanQuery, field, String.valueOf(value));
133     }
134 
135     public static void addTerm(
136             BooleanQuery booleanQuery, String field, String value)
137         throws ParseException {
138 
139         addTerm(booleanQuery, field, value, false);
140     }
141 
142     public static void addTerm(
143             BooleanQuery booleanQuery, String field, String value,
144             boolean like)
145         throws ParseException {
146 
147         getLuceneHelper().addTerm(booleanQuery, field, value, like);
148     }
149 
150     public static void delete(long companyId) {
151         getLuceneHelper().delete(companyId);
152     }
153 
154     public static void deleteDocuments(long companyId, Term term)
155         throws IOException {
156 
157         getLuceneHelper().deleteDocuments(companyId, term);
158     }
159 
160     public static Analyzer getAnalyzer() {
161         return getLuceneHelper().getAnalyzer();
162     }
163 
164     public static LuceneHelper getLuceneHelper() {
165         return _luceneHelper;
166     }
167 
168     public static String[] getQueryTerms(Query query) {
169         return getLuceneHelper().getQueryTerms(query);
170     }
171 
172     public static IndexSearcher getSearcher(long companyId, boolean readOnly)
173         throws IOException {
174 
175         return getLuceneHelper().getSearcher(companyId, readOnly);
176     }
177 
178     public static String getSnippet(Query query, String field, String s)
179         throws IOException {
180 
181         return getSnippet(
182             query, field, s, 3, 80, "...", StringPool.BLANK, StringPool.BLANK);
183     }
184 
185     public static String getSnippet(
186             Query query, String field, String s, int maxNumFragments,
187             int fragmentLength, String fragmentSuffix, String preTag,
188             String postTag)
189         throws IOException {
190 
191         return getLuceneHelper().getSnippet(
192             query, field, s, maxNumFragments, fragmentLength, fragmentSuffix,
193             preTag, postTag);
194     }
195 
196     public static void updateDocument(
197             long companyId, Term term, Document document)
198         throws IOException {
199 
200         getLuceneHelper().updateDocument(companyId, term, document);
201     }
202 
203     public static void shutdown() {
204         getLuceneHelper().shutdown();
205     }
206 
207     public void setLuceneHelper(LuceneHelper luceneHelper) {
208         _luceneHelper = luceneHelper;
209     }
210 
211     private static LuceneHelper _luceneHelper;
212 
213 }