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.kernel.search;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  /**
21   * <a href="SearchEngineUtil.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Bruno Farache
24   */
25  public class SearchEngineUtil {
26  
27      /**
28       * @deprecated Use {@link
29       *             com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}.
30       */
31      public static final int ALL_POS = -1;
32  
33      public static void addDocument(long companyId, Document doc)
34          throws SearchException {
35  
36          if (isIndexReadOnly()) {
37              return;
38          }
39  
40          if (_log.isDebugEnabled()) {
41              _log.debug("Add document " + doc.toString());
42          }
43  
44          _searchEngine.getWriter().addDocument(companyId, doc);
45      }
46  
47      public static void deleteDocument(long companyId, String uid)
48          throws SearchException {
49  
50          if (isIndexReadOnly()) {
51              return;
52          }
53  
54          _searchEngine.getWriter().deleteDocument(companyId, uid);
55      }
56  
57      public static void deletePortletDocuments(long companyId, String portletId)
58          throws SearchException {
59  
60          if (isIndexReadOnly()) {
61              return;
62          }
63  
64          _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
65      }
66  
67      public static PortalSearchEngine getPortalSearchEngine() {
68          return _portalSearchEngine;
69      }
70  
71      public static SearchEngine getSearchEngine() {
72          return _searchEngine;
73      }
74  
75      public static boolean isIndexReadOnly() {
76          return _portalSearchEngine.isIndexReadOnly();
77      }
78  
79      public static Hits search(long companyId, Query query, int start, int end)
80          throws SearchException {
81  
82          if (_log.isDebugEnabled()) {
83              _log.debug("Search query " + query.toString());
84          }
85  
86          return _searchEngine.getSearcher().search(
87              companyId, query, _DEFAULT_SORT, start, end);
88      }
89  
90      public static Hits search(
91              long companyId, Query query, Sort sort, int start, int end)
92          throws SearchException {
93  
94          if (_log.isDebugEnabled()) {
95              _log.debug("Search query " + query.toString());
96          }
97  
98          return _searchEngine.getSearcher().search(
99              companyId, query, new Sort[] {sort}, start, end);
100     }
101 
102     public static Hits search(
103             long companyId, Query query, Sort[] sorts, int start, int end)
104         throws SearchException {
105 
106         if (_log.isDebugEnabled()) {
107             _log.debug("Search query " + query.toString());
108         }
109 
110         return _searchEngine.getSearcher().search(
111             companyId, query, sorts, start, end);
112     }
113 
114     public static void setIndexReadOnly(boolean indexReadOnly) {
115         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
116     }
117 
118     public static void updateDocument(long companyId, String uid, Document doc)
119         throws SearchException {
120 
121         if (isIndexReadOnly()) {
122             return;
123         }
124 
125         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
126     }
127 
128     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
129         _portalSearchEngine = portalSearchEngine;
130     }
131 
132     public void setSearchEngine(SearchEngine searchEngine) {
133         _searchEngine = searchEngine;
134     }
135 
136     private static final Sort[] _DEFAULT_SORT = new Sort[] {
137         new Sort(null, Sort.SCORE_TYPE, false),
138         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
139     };
140 
141     private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
142 
143     private static PortalSearchEngine _portalSearchEngine;
144     private static SearchEngine _searchEngine;
145 
146 }