001
014
015 package com.liferay.portal.search.lucene;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.search.Document;
020 import com.liferay.portal.kernel.search.DocumentImpl;
021 import com.liferay.portal.kernel.search.Field;
022 import com.liferay.portal.kernel.search.IndexWriter;
023 import com.liferay.portal.kernel.search.SearchContext;
024 import com.liferay.portal.kernel.search.SearchException;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.Validator;
027
028 import java.io.IOException;
029
030 import java.util.Collection;
031 import java.util.Locale;
032 import java.util.Map;
033
034 import org.apache.lucene.index.Term;
035
036
042 public class LuceneIndexWriterImpl implements IndexWriter {
043
044 public void addDocument(
045 SearchContext searchContext, Document document)
046 throws SearchException {
047
048 try {
049 LuceneHelperUtil.addDocument(
050 searchContext.getCompanyId(), _getLuceneDocument(document));
051
052 if (_log.isDebugEnabled()) {
053 _log.debug("Added document " + document.get(Field.UID));
054 }
055 }
056 catch (IOException ioe) {
057 throw new SearchException(ioe);
058 }
059 }
060
061 public void addDocuments(
062 SearchContext searchContext, Collection<Document> documents)
063 throws SearchException {
064
065 for (Document document : documents) {
066 addDocument(searchContext, document);
067 }
068 }
069
070 public void deleteDocument(SearchContext searchContext, String uid)
071 throws SearchException {
072
073 try {
074 LuceneHelperUtil.deleteDocuments(
075 searchContext.getCompanyId(), new Term(Field.UID, uid));
076
077 if (_log.isDebugEnabled()) {
078 _log.debug("Deleted document " + uid);
079 }
080 }
081 catch (IOException ioe) {
082 throw new SearchException(ioe);
083 }
084 }
085
086 public void deleteDocuments(
087 SearchContext searchContext, Collection<String> uids)
088 throws SearchException {
089
090 for (String uid : uids) {
091 deleteDocument(searchContext, uid);
092 }
093 }
094
095 public void deletePortletDocuments(
096 SearchContext searchContext, String portletId)
097 throws SearchException {
098
099 try {
100 LuceneHelperUtil.deleteDocuments(
101 searchContext.getCompanyId(), new Term(Field.PORTLET_ID,
102 portletId));
103 }
104 catch (IOException ioe) {
105 throw new SearchException(ioe);
106 }
107 }
108
109 public void updateDocument(
110 SearchContext searchContext, Document document)
111 throws SearchException {
112
113 try {
114 LuceneHelperUtil.updateDocument(
115 searchContext.getCompanyId(),
116 new Term(Field.UID, document.getUID()),
117 _getLuceneDocument(document));
118
119 if (_log.isDebugEnabled()) {
120 _log.debug("Updated document " + document.get(Field.UID));
121 }
122 }
123 catch (IOException ioe) {
124 throw new SearchException(ioe);
125 }
126 }
127
128 public void updateDocuments(
129 SearchContext searchContext, Collection<Document> documents)
130 throws SearchException {
131
132 for (Document document : documents) {
133 updateDocument(searchContext, document);
134 }
135 }
136
137 private void _addLuceneFieldable(
138 org.apache.lucene.document.Document luceneDocument, String name,
139 boolean numeric, boolean tokenized, float boost, String value) {
140
141 org.apache.lucene.document.Fieldable luceneFieldable = null;
142
143 if (numeric) {
144 luceneFieldable = LuceneFields.getNumber(name, value);
145 }
146 else {
147 if (tokenized) {
148 luceneFieldable = LuceneFields.getText(name, value);
149 }
150 else {
151 luceneFieldable = LuceneFields.getKeyword(name, value);
152 }
153 }
154
155 luceneFieldable.setBoost(boost);
156
157 luceneDocument.add(luceneFieldable);
158 }
159
160 private org.apache.lucene.document.Document _getLuceneDocument(
161 Document document) {
162
163 org.apache.lucene.document.Document luceneDocument =
164 new org.apache.lucene.document.Document();
165
166 Collection<Field> fields = document.getFields().values();
167
168 for (Field field : fields) {
169 String name = field.getName();
170 boolean numeric = field.isNumeric();
171 boolean tokenized = field.isTokenized();
172 float boost = field.getBoost();
173
174 if (!field.isLocalized()) {
175 for (String value : field.getValues()) {
176 if (Validator.isNull(value)) {
177 continue;
178 }
179
180 _addLuceneFieldable(
181 luceneDocument, name, numeric, tokenized, boost, value);
182 }
183 }
184 else {
185 Map<Locale, String> localizedValues =
186 field.getLocalizedValues();
187
188 for (Map.Entry<Locale, String> entry :
189 localizedValues.entrySet()) {
190
191 String value = entry.getValue();
192
193 if (Validator.isNull(value)) {
194 continue;
195 }
196
197 Locale locale = entry.getKey();
198
199 String languageId = LocaleUtil.toLanguageId(locale);
200
201 String defaultLanguageId = LocaleUtil.toLanguageId(
202 LocaleUtil.getDefault());
203
204 if (languageId.equals(defaultLanguageId)) {
205 _addLuceneFieldable(
206 luceneDocument, name, numeric, tokenized, boost,
207 value);
208 }
209
210 String localizedName = DocumentImpl.getLocalizedName(
211 locale, name);
212
213 _addLuceneFieldable(
214 luceneDocument, localizedName, numeric, tokenized,
215 boost, value);
216 }
217 }
218
219 }
220
221 return luceneDocument;
222 }
223
224 private static Log _log = LogFactoryUtil.getLog(
225 LuceneIndexWriterImpl.class);
226
227 }