1
22
23 package com.liferay.portal.kernel.search;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.messaging.DestinationNames;
28 import com.liferay.portal.kernel.messaging.Message;
29 import com.liferay.portal.kernel.messaging.MessageBusException;
30 import com.liferay.portal.kernel.messaging.MessageBusUtil;
31 import com.liferay.portal.kernel.search.messaging.SearchRequest;
32
33
39 public class SearchEngineUtil {
40
41 public static final int ALL_POS = -1;
42
43 public static void addDocument(long companyId, Document doc)
44 throws SearchException {
45
46 _instance._addDocument(companyId, doc);
47 }
48
49 public static void deleteDocument(long companyId, String uid)
50 throws SearchException {
51
52 _instance._deleteDocument(companyId, uid);
53 }
54
55 public static void deletePortletDocuments(long companyId, String portletId)
56 throws SearchException {
57
58 _instance._deletePortletDocuments(companyId, portletId);
59 }
60
61 public static void init(
62 IndexSearcher defaultIndexSearcher, IndexWriter defaultIndexWriter) {
63
64 _instance._init(defaultIndexSearcher, defaultIndexWriter);
65 }
66
67 public static boolean isIndexReadOnly() {
68 return _instance._isIndexReadOnly();
69 }
70
71 public static Hits search(long companyId, Query query, int start, int end)
72 throws SearchException {
73
74 return _instance._search(companyId, query, start, end);
75 }
76
77 public static Hits search(
78 long companyId, Query query, Sort sort, int start, int end)
79 throws SearchException {
80
81 return _instance._search(companyId, query, sort, start, end);
82 }
83
84 public static void updateDocument(long companyId, String uid, Document doc)
85 throws SearchException {
86
87 _instance._updateDocument(companyId, uid, doc);
88 }
89
90 private SearchEngineUtil() {
91 }
92
93 private void _addDocument(long companyId, Document doc)
94 throws SearchException {
95
96 _messageBusIndexWriter.addDocument(companyId, doc);
97 }
98
99 private void _deleteDocument(long companyId, String uid)
100 throws SearchException {
101
102 _messageBusIndexWriter.deleteDocument(companyId, uid);
103 }
104
105 private void _deletePortletDocuments(long companyId, String portletId)
106 throws SearchException {
107
108 _messageBusIndexWriter.deletePortletDocuments(companyId, portletId);
109 }
110
111 private void _init(
112 IndexSearcher messageBusIndexSearcher,
113 IndexWriter messageBusIndexWriter) {
114
115 _messageBusIndexSearcher = messageBusIndexSearcher;
116 _messageBusIndexWriter = messageBusIndexWriter;
117 }
118
119 private boolean _isIndexReadOnly() {
120 if (_indexReadOnly != null) {
121 return _indexReadOnly.booleanValue();
122 }
123
124 try {
125 SearchRequest searchRequest = new SearchRequest();
126
127 searchRequest.setCommand(SearchRequest.COMMAND_INDEX_ONLY);
128
129 _indexReadOnly = (Boolean)MessageBusUtil.sendSynchronizedMessage(
130 DestinationNames.SEARCH_READER, new Message(searchRequest));
131
132 if (_indexReadOnly == null) {
133 _indexReadOnly = Boolean.FALSE;
134 }
135
136 return _indexReadOnly.booleanValue();
137 }
138 catch (MessageBusException mbe) {
139 if (_log.isWarnEnabled()) {
140 _log.warn("Unable to check index status", mbe);
141 }
142
143 return false;
144 }
145 }
146
147 private Hits _search(long companyId, Query query, int start, int end)
148 throws SearchException {
149
150 return _messageBusIndexSearcher.search(companyId, query, start, end);
151 }
152
153 private Hits _search(
154 long companyId, Query query, Sort sort, int start, int end)
155 throws SearchException {
156
157 return _messageBusIndexSearcher.search(
158 companyId, query, sort, start, end);
159 }
160
161 private void _updateDocument(long companyId, String uid, Document doc)
162 throws SearchException {
163
164 _messageBusIndexWriter.updateDocument(companyId, uid, doc);
165 }
166
167 private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
168
169 private static SearchEngineUtil _instance = new SearchEngineUtil();
170
171 private IndexSearcher _messageBusIndexSearcher;
172 private IndexWriter _messageBusIndexWriter;
173 private Boolean _indexReadOnly;
174
175 }