1   /**
2    * Copyright (c) 2000-2008 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.search.lucene;
24  
25  import com.liferay.portal.kernel.messaging.Destination;
26  import com.liferay.portal.kernel.messaging.DestinationNames;
27  import com.liferay.portal.kernel.messaging.MessageBusUtil;
28  import com.liferay.portal.kernel.messaging.ParallelDestination;
29  import com.liferay.portal.kernel.messaging.SerialDestination;
30  import com.liferay.portal.kernel.search.Document;
31  import com.liferay.portal.kernel.search.Hits;
32  import com.liferay.portal.kernel.search.Query;
33  import com.liferay.portal.kernel.search.SearchEngine;
34  import com.liferay.portal.kernel.search.SearchException;
35  import com.liferay.portal.kernel.search.Sort;
36  import com.liferay.portal.search.lucene.messaging.LuceneReaderMessageListener;
37  import com.liferay.portal.search.lucene.messaging.LuceneWriterMessageListener;
38  
39  /**
40   * <a href="LuceneSearchEngineUtil.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Bruno Farache
43   *
44   */
45  public class LuceneSearchEngineUtil {
46  
47      public static void addDocument(long companyId, Document doc)
48          throws SearchException {
49  
50          _engine.getWriter().addDocument(companyId, doc);
51      }
52  
53      public static void deleteDocument(long companyId, String uid)
54          throws SearchException {
55  
56          _engine.getWriter().deleteDocument(companyId, uid);
57      }
58  
59      public static void deletePortletDocuments(long companyId, String portletId)
60          throws SearchException {
61  
62          _engine.getWriter().deletePortletDocuments(companyId, portletId);
63      }
64  
65      public static SearchEngine getSearchEngine() {
66          return _engine;
67      }
68  
69      public static void init() {
70          if (_engine != null) {
71              return;
72          }
73  
74          _engine = new LuceneSearchEngineImpl();
75  
76          Destination searchReadDestination = new ParallelDestination(
77              DestinationNames.SEARCH_READER);
78  
79          MessageBusUtil.addDestination(searchReadDestination);
80  
81          searchReadDestination.register(new LuceneReaderMessageListener());
82  
83          Destination searchWriteDestination = new SerialDestination(
84              DestinationNames.SEARCH_WRITER);
85  
86          MessageBusUtil.addDestination(searchWriteDestination);
87  
88          searchWriteDestination.register(new LuceneWriterMessageListener());
89      }
90  
91      public static boolean isIndexReadOnly() {
92          return _engine.isIndexReadOnly();
93      }
94  
95      public static Hits search(long companyId, Query query, int start, int end)
96          throws SearchException {
97  
98          return _engine.getSearcher().search(companyId, query, start, end);
99      }
100 
101     public static Hits search(
102             long companyId, Query query, Sort sort, int start, int end)
103         throws SearchException {
104 
105         return _engine.getSearcher().search(companyId, query, sort, start, end);
106     }
107 
108     public static void updateDocument(long companyId, String uid, Document doc)
109         throws SearchException {
110 
111         _engine.getWriter().updateDocument(companyId, uid, doc);
112     }
113 
114     private static SearchEngine _engine;
115 
116 }