--- /dev/null
+/**\r
+ * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.\r
+ *\r
+ * This library is free software; you can redistribute it and/or modify it under\r
+ * the terms of the GNU Lesser General Public License as published by the Free\r
+ * Software Foundation; either version 2.1 of the License, or (at your option)\r
+ * any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
+ * details.\r
+ */\r
+\r
+package com.liferay.portal.search.solr;\r
+\r
+import com.liferay.portal.kernel.log.Log;\r
+import com.liferay.portal.kernel.log.LogFactoryUtil;\r
+import com.liferay.portal.kernel.search.Document;\r
+import com.liferay.portal.kernel.search.DocumentImpl;\r
+import com.liferay.portal.kernel.search.Field;\r
+import com.liferay.portal.kernel.search.IndexWriter;\r
+import com.liferay.portal.kernel.search.SearchContext;\r
+import com.liferay.portal.kernel.search.SearchException;\r
+import com.liferay.portal.kernel.util.LocaleUtil;\r
+import com.liferay.portal.kernel.util.StringBundler;\r
+import com.liferay.portal.kernel.util.StringPool;\r
+import com.liferay.portal.kernel.util.Validator;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+import java.util.Locale;\r
+import java.util.Map;\r
+\r
+import org.apache.solr.client.solrj.SolrServer;\r
+import org.apache.solr.common.SolrInputDocument;\r
+\r
+/**\r
+ * @author Bruno Farache\r
+ */\r
+public class SolrIndexWriterImpl implements IndexWriter {\r
+\r
+ public void addDocument(SearchContext searchContext, Document document)\r
+ throws SearchException {\r
+\r
+ try {\r
+ _solrServer.add(getSolrInputDocument(document));\r
+\r
+ /* let Solr handle the commit\r
+ if (_commit) {\r
+ _solrServer.commit();\r
+ }\r
+ */\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+\r
+ throw new SearchException(e.getMessage());\r
+ }\r
+ }\r
+\r
+ public void addDocuments(\r
+ SearchContext searchContext, Collection<Document> documents)\r
+ throws SearchException {\r
+\r
+ try {\r
+ Collection<SolrInputDocument> solrInputDocuments =\r
+ getSolrInputDocuments(documents);\r
+\r
+ if (solrInputDocuments.isEmpty()) {\r
+ return;\r
+ }\r
+\r
+ _solrServer.add(solrInputDocuments);\r
+\r
+ if (_commit) {\r
+ _solrServer.commit();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+\r
+ throw new SearchException(e.getMessage());\r
+ }\r
+ }\r
+\r
+ public void deleteDocument(SearchContext searchContext, String uid)\r
+ throws SearchException {\r
+\r
+ try {\r
+ _solrServer.deleteById(uid);\r
+\r
+ if (_commit) {\r
+ _solrServer.commit();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+\r
+ throw new SearchException(e.getMessage());\r
+ }\r
+ }\r
+\r
+ public void deleteDocuments(\r
+ SearchContext searchContext, Collection<String> uids)\r
+ throws SearchException {\r
+\r
+ for (String uid : uids) {\r
+ deleteDocument(searchContext, uid);\r
+ }\r
+ }\r
+\r
+ public void deletePortletDocuments(\r
+ SearchContext searchContext, String portletId)\r
+ throws SearchException {\r
+\r
+ try {\r
+ long companyId = searchContext.getCompanyId();\r
+\r
+ StringBundler sb = null;\r
+\r
+ if (companyId > 0) {\r
+ sb = new StringBundler(9);\r
+\r
+ sb.append(StringPool.PLUS);\r
+ sb.append(Field.COMPANY_ID);\r
+ sb.append(StringPool.COLON);\r
+ sb.append(companyId);\r
+ sb.append(StringPool.SPACE);\r
+ }\r
+\r
+ if (sb == null) {\r
+ sb = new StringBundler(4);\r
+ }\r
+\r
+ sb.append(StringPool.PLUS);\r
+ sb.append(Field.PORTLET_ID);\r
+ sb.append(StringPool.COLON);\r
+ sb.append(portletId);\r
+\r
+ _solrServer.deleteByQuery(sb.toString());\r
+\r
+ if (_commit) {\r
+ _solrServer.commit();\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+\r
+ throw new SearchException(e.getMessage());\r
+ }\r
+ }\r
+\r
+ public void setCommit(boolean commit) {\r
+ _commit = commit;\r
+ }\r
+\r
+ public void setSolrServer(SolrServer solrServer) {\r
+ _solrServer = solrServer;\r
+ }\r
+\r
+ public void updateDocument(SearchContext searchContext, Document document)\r
+ throws SearchException {\r
+\r
+ deleteDocument(searchContext, document.getUID());\r
+\r
+ addDocument(searchContext, document);\r
+ }\r
+\r
+ public void updateDocuments(\r
+ SearchContext searchContext, Collection<Document> documents)\r
+ throws SearchException {\r
+\r
+ for (Document document : documents) {\r
+ deleteDocument(searchContext, document.getUID());\r
+ }\r
+\r
+ addDocuments(searchContext, documents);\r
+ }\r
+\r
+ protected SolrInputDocument getSolrInputDocument(Document document) {\r
+ SolrInputDocument solrInputDocument = new SolrInputDocument();\r
+\r
+ Collection<Field> fields = document.getFields().values();\r
+\r
+ for (Field field : fields) {\r
+ String name = field.getName();\r
+ float boost = field.getBoost();\r
+ if(boost<1)\r
+ boost=1;\r
+ if (!field.isLocalized()) {\r
+ for (String value : field.getValues()) {\r
+ if (Validator.isNull(value)) {\r
+ continue;\r
+ }\r
+\r
+ solrInputDocument.addField(name, value.trim(), boost);\r
+ }\r
+ }\r
+ else {\r
+ Map<Locale, String> localizedValues =\r
+ field.getLocalizedValues();\r
+\r
+ for (Map.Entry<Locale, String> entry :\r
+ localizedValues.entrySet()) {\r
+\r
+ String value = entry.getValue();\r
+\r
+ if (Validator.isNull(value)) {\r
+ continue;\r
+ }\r
+\r
+ Locale locale = entry.getKey();\r
+\r
+ String languageId = LocaleUtil.toLanguageId(locale);\r
+\r
+ String defaultLanguageId = LocaleUtil.toLanguageId(\r
+ LocaleUtil.getDefault());\r
+\r
+ if (languageId.equals(defaultLanguageId)) {\r
+ solrInputDocument.addField(name, value.trim(), boost);\r
+ }\r
+\r
+ String localizedName = DocumentImpl.getLocalizedName(\r
+ locale, name);\r
+\r
+ solrInputDocument.addField(\r
+ localizedName, value.trim(), boost);\r
+ }\r
+ }\r
+ }\r
+\r
+ return solrInputDocument;\r
+ }\r
+\r
+ protected Collection<SolrInputDocument> getSolrInputDocuments(\r
+ Collection<Document> documents) {\r
+\r
+ List<SolrInputDocument> solrInputDocuments =\r
+ new ArrayList<SolrInputDocument>(documents.size());\r
+\r
+ for (Document document : documents) {\r
+ SolrInputDocument solrInputDocument = getSolrInputDocument(\r
+ document);\r
+\r
+ solrInputDocuments.add(solrInputDocument);\r
+ }\r
+\r
+ return solrInputDocuments;\r
+ }\r
+\r
+ private static Log _log = LogFactoryUtil.getLog(SolrIndexWriterImpl.class);\r
+\r
+ private boolean _commit;\r
+ private SolrServer _solrServer;\r
+\r
+}
\ No newline at end of file