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