2 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portal.search.solr;
\r
17 import com.liferay.portal.kernel.log.Log;
\r
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
19 import com.liferay.portal.kernel.search.Document;
\r
20 import com.liferay.portal.kernel.search.DocumentImpl;
\r
21 import com.liferay.portal.kernel.search.Field;
\r
22 import com.liferay.portal.kernel.search.IndexWriter;
\r
23 import com.liferay.portal.kernel.search.SearchContext;
\r
24 import com.liferay.portal.kernel.search.SearchException;
\r
25 import com.liferay.portal.kernel.util.LocaleUtil;
\r
26 import com.liferay.portal.kernel.util.StringBundler;
\r
27 import com.liferay.portal.kernel.util.StringPool;
\r
28 import com.liferay.portal.kernel.util.Validator;
\r
30 import java.util.ArrayList;
\r
31 import java.util.Collection;
\r
32 import java.util.List;
\r
33 import java.util.Locale;
\r
34 import java.util.Map;
\r
36 import org.apache.solr.client.solrj.SolrServer;
\r
37 import org.apache.solr.common.SolrInputDocument;
\r
40 * @author Bruno Farache
\r
42 public class SolrIndexWriterImpl implements IndexWriter {
\r
44 public void addDocument(SearchContext searchContext, Document document)
\r
45 throws SearchException {
\r
48 _solrServer.add(getSolrInputDocument(document));
\r
50 /* let Solr handle the commit
\r
52 _solrServer.commit();
\r
56 catch (Exception e) {
\r
59 throw new SearchException(e.getMessage());
\r
63 public void addDocuments(
\r
64 SearchContext searchContext, Collection<Document> documents)
\r
65 throws SearchException {
\r
68 Collection<SolrInputDocument> solrInputDocuments =
\r
69 getSolrInputDocuments(documents);
\r
71 if (solrInputDocuments.isEmpty()) {
\r
75 _solrServer.add(solrInputDocuments);
\r
78 _solrServer.commit();
\r
81 catch (Exception e) {
\r
84 throw new SearchException(e.getMessage());
\r
88 public void deleteDocument(SearchContext searchContext, String uid)
\r
89 throws SearchException {
\r
92 _solrServer.deleteById(uid);
\r
95 _solrServer.commit();
\r
98 catch (Exception e) {
\r
101 throw new SearchException(e.getMessage());
\r
105 public void deleteDocuments(
\r
106 SearchContext searchContext, Collection<String> uids)
\r
107 throws SearchException {
\r
109 for (String uid : uids) {
\r
110 deleteDocument(searchContext, uid);
\r
114 public void deletePortletDocuments(
\r
115 SearchContext searchContext, String portletId)
\r
116 throws SearchException {
\r
119 long companyId = searchContext.getCompanyId();
\r
121 StringBundler sb = null;
\r
123 if (companyId > 0) {
\r
124 sb = new StringBundler(9);
\r
126 sb.append(StringPool.PLUS);
\r
127 sb.append(Field.COMPANY_ID);
\r
128 sb.append(StringPool.COLON);
\r
129 sb.append(companyId);
\r
130 sb.append(StringPool.SPACE);
\r
134 sb = new StringBundler(4);
\r
137 sb.append(StringPool.PLUS);
\r
138 sb.append(Field.PORTLET_ID);
\r
139 sb.append(StringPool.COLON);
\r
140 sb.append(portletId);
\r
142 _solrServer.deleteByQuery(sb.toString());
\r
145 _solrServer.commit();
\r
148 catch (Exception e) {
\r
151 throw new SearchException(e.getMessage());
\r
155 public void setCommit(boolean commit) {
\r
159 public void setSolrServer(SolrServer solrServer) {
\r
160 _solrServer = solrServer;
\r
163 public void updateDocument(SearchContext searchContext, Document document)
\r
164 throws SearchException {
\r
166 deleteDocument(searchContext, document.getUID());
\r
168 addDocument(searchContext, document);
\r
171 public void updateDocuments(
\r
172 SearchContext searchContext, Collection<Document> documents)
\r
173 throws SearchException {
\r
175 for (Document document : documents) {
\r
176 deleteDocument(searchContext, document.getUID());
\r
179 addDocuments(searchContext, documents);
\r
182 protected SolrInputDocument getSolrInputDocument(Document document) {
\r
183 SolrInputDocument solrInputDocument = new SolrInputDocument();
\r
185 Collection<Field> fields = document.getFields().values();
\r
187 for (Field field : fields) {
\r
188 String name = field.getName();
\r
189 float boost = field.getBoost();
\r
192 if (!field.isLocalized()) {
\r
193 for (String value : field.getValues()) {
\r
194 if (Validator.isNull(value)) {
\r
198 solrInputDocument.addField(name, value.trim(), boost);
\r
202 Map<Locale, String> localizedValues =
\r
203 field.getLocalizedValues();
\r
205 for (Map.Entry<Locale, String> entry :
\r
206 localizedValues.entrySet()) {
\r
208 String value = entry.getValue();
\r
210 if (Validator.isNull(value)) {
\r
214 Locale locale = entry.getKey();
\r
216 String languageId = LocaleUtil.toLanguageId(locale);
\r
218 String defaultLanguageId = LocaleUtil.toLanguageId(
\r
219 LocaleUtil.getDefault());
\r
221 if (languageId.equals(defaultLanguageId)) {
\r
222 solrInputDocument.addField(name, value.trim(), boost);
\r
225 String localizedName = DocumentImpl.getLocalizedName(
\r
228 solrInputDocument.addField(
\r
229 localizedName, value.trim(), boost);
\r
234 return solrInputDocument;
\r
237 protected Collection<SolrInputDocument> getSolrInputDocuments(
\r
238 Collection<Document> documents) {
\r
240 List<SolrInputDocument> solrInputDocuments =
\r
241 new ArrayList<SolrInputDocument>(documents.size());
\r
243 for (Document document : documents) {
\r
244 SolrInputDocument solrInputDocument = getSolrInputDocument(
\r
247 solrInputDocuments.add(solrInputDocument);
\r
250 return solrInputDocuments;
\r
253 private static Log _log = LogFactoryUtil.getLog(SolrIndexWriterImpl.class);
\r
255 private boolean _commit;
\r
256 private SolrServer _solrServer;
\r