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, String 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, String 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 Message message = new Message(
126 new SearchRequest(SearchRequest.COMMAND_INDEX_ONLY));
127
128 _indexReadOnly = (Boolean)MessageBusUtil.sendSynchronizedMessage(
129 DestinationNames.SEARCH_READER, message);
130
131 if (_indexReadOnly == null) {
132 _indexReadOnly = Boolean.FALSE;
133 }
134
135 return _indexReadOnly.booleanValue();
136 }
137 catch (MessageBusException mbe) {
138 if (_log.isWarnEnabled()) {
139 _log.warn("Unable to check index status", mbe);
140 }
141
142 return false;
143 }
144 }
145
146 private Hits _search(long companyId, String query, int start, int end)
147 throws SearchException {
148
149 return _messageBusIndexSearcher.search(companyId, query, start, end);
150 }
151
152 private Hits _search(
153 long companyId, String query, Sort sort, int start, int end)
154 throws SearchException {
155
156 return _messageBusIndexSearcher.search(
157 companyId, query, sort, start, end);
158 }
159
160 private void _updateDocument(long companyId, String uid, Document doc)
161 throws SearchException {
162
163 _messageBusIndexWriter.updateDocument(companyId, uid, doc);
164 }
165
166 private static Log _log = LogFactoryUtil.getLog(SearchEngineUtil.class);
167
168 private static SearchEngineUtil _instance = new SearchEngineUtil();
169
170 private IndexSearcher _messageBusIndexSearcher;
171 private IndexWriter _messageBusIndexWriter;
172 private Boolean _indexReadOnly;
173
174 }