1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.kernel.search;
24  
25  /**
26   * <a href="SearchEngineUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Bruno Farache
29   *
30   */
31  public class SearchEngineUtil {
32  
33      /**
34       * @deprecated Use
35       * <code>com.liferay.portal.kernel.dao.orm.QueryUtil.ALL_POS</code>.
36       */
37      public static final int ALL_POS = -1;
38  
39      public static void addDocument(long companyId, Document doc)
40          throws SearchException {
41  
42          if (isIndexReadOnly()) {
43              return;
44          }
45  
46          _searchEngine.getWriter().addDocument(companyId, doc);
47      }
48  
49      public static void deleteDocument(long companyId, String uid)
50          throws SearchException {
51  
52          if (isIndexReadOnly()) {
53              return;
54          }
55  
56          _searchEngine.getWriter().deleteDocument(companyId, uid);
57      }
58  
59      public static void deletePortletDocuments(long companyId, String portletId)
60          throws SearchException {
61  
62          if (isIndexReadOnly()) {
63              return;
64          }
65  
66          _searchEngine.getWriter().deletePortletDocuments(companyId, portletId);
67      }
68  
69      public static PortalSearchEngine getPortalSearchEngine() {
70          return _portalSearchEngine;
71      }
72  
73      public static SearchEngine getSearchEngine() {
74          return _searchEngine;
75      }
76  
77      public static boolean isIndexReadOnly() {
78          return _portalSearchEngine.isIndexReadOnly();
79      }
80  
81      public static void register(String name) {
82          _searchEngine.register(name);
83      }
84  
85      public static void registerDefaultSearchEngine() {
86          SearchEngineUtil.register(_defaultSearchEngineName);
87      }
88  
89      public static Hits search(long companyId, Query query, int start, int end)
90          throws SearchException {
91  
92          return _searchEngine.getSearcher().search(
93              companyId, query, _DEFAULT_SORT, start, end);
94      }
95  
96      public static Hits search(
97              long companyId, Query query, Sort sort, int start, int end)
98          throws SearchException {
99  
100         return _searchEngine.getSearcher().search(
101             companyId, query, new Sort[] {sort}, start, end);
102     }
103 
104     public static Hits search(
105             long companyId, Query query, Sort[] sorts, int start, int end)
106         throws SearchException {
107 
108         return _searchEngine.getSearcher().search(
109             companyId, query, sorts, start, end);
110     }
111 
112     public static void setIndexReadOnly(boolean indexReadOnly) {
113         _portalSearchEngine.setIndexReadOnly(indexReadOnly);
114     }
115 
116     public static void unregister(String fromName) {
117         _searchEngine.unregister(fromName);
118     }
119 
120     public static void updateDocument(long companyId, String uid, Document doc)
121         throws SearchException {
122 
123         if (isIndexReadOnly()) {
124             return;
125         }
126 
127         _searchEngine.getWriter().updateDocument(companyId, uid, doc);
128     }
129 
130     public void setDefaultSearchEngineName(String defaultSearchEngineName) {
131         _defaultSearchEngineName = defaultSearchEngineName;
132     }
133 
134     public void setPortalSearchEngine(PortalSearchEngine portalSearchEngine) {
135         _portalSearchEngine = portalSearchEngine;
136     }
137 
138     public void setSearchEngine(SearchEngine searchEngine) {
139         _searchEngine = searchEngine;
140     }
141 
142     private static final Sort[] _DEFAULT_SORT = new Sort[] {
143         new Sort(null, Sort.SCORE_TYPE, false),
144         new Sort(Field.MODIFIED, Sort.LONG_TYPE, true)
145     };
146 
147     private static String _defaultSearchEngineName;
148     private static PortalSearchEngine _portalSearchEngine;
149     private static SearchEngine _searchEngine;
150 
151 }