1
22
23 package com.liferay.portal.search.lucene;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.search.Document;
28 import com.liferay.portal.kernel.search.Field;
29 import com.liferay.portal.kernel.search.IndexWriter;
30 import com.liferay.portal.kernel.search.SearchException;
31 import com.liferay.portal.kernel.util.Validator;
32
33 import java.io.IOException;
34
35 import java.util.Collection;
36
37 import org.apache.lucene.index.Term;
38
39
47 public class LuceneIndexWriterImpl implements IndexWriter {
48
49 public void addDocument(long companyId, Document document)
50 throws SearchException {
51
52 try {
53 LuceneHelperUtil.addDocument(
54 companyId, _getLuceneDocument(document));
55
56 if (_log.isDebugEnabled()) {
57 _log.debug("Added document " + document.get(Field.UID));
58 }
59 }
60 catch (IOException ioe) {
61 throw new SearchException(ioe);
62 }
63 }
64
65 public void deleteDocument(long companyId, String uid)
66 throws SearchException {
67
68 try {
69 LuceneHelperUtil.deleteDocuments(
70 companyId, new Term(Field.UID, uid));
71
72 if (_log.isDebugEnabled()) {
73 _log.debug("Deleted document " + uid);
74 }
75 }
76 catch (IOException ioe) {
77 throw new SearchException(ioe);
78 }
79 }
80
81 public void deletePortletDocuments(long companyId, String portletId)
82 throws SearchException {
83
84 try {
85 LuceneHelperUtil.deleteDocuments(
86 companyId, new Term(Field.PORTLET_ID, portletId));
87 }
88 catch (IOException ioe) {
89 throw new SearchException(ioe);
90 }
91 }
92
93 public void updateDocument(long companyId, String uid, Document document)
94 throws SearchException {
95
96 try {
97 LuceneHelperUtil.updateDocument(
98 companyId, new Term(Field.UID, uid),
99 _getLuceneDocument(document));
100
101 if (_log.isDebugEnabled()) {
102 _log.debug("Updated document " + document.get(Field.UID));
103 }
104 }
105 catch (IOException ioe) {
106 throw new SearchException(ioe);
107 }
108 }
109
110 private org.apache.lucene.document.Document _getLuceneDocument(
111 Document document) {
112
113 org.apache.lucene.document.Document luceneDocument =
114 new org.apache.lucene.document.Document();
115
116 Collection<Field> fields = document.getFields().values();
117
118 for (Field field : fields) {
119 String name = field.getName();
120 boolean tokenized = field.isTokenized();
121 float boost = field.getBoost();
122
123 for (String value : field.getValues()) {
124 if (Validator.isNull(value)) {
125 continue;
126 }
127
128 org.apache.lucene.document.Field luceneField = null;
129
130 if (tokenized) {
131 luceneField = LuceneFields.getText(name, value);
132 }
133 else {
134 luceneField = LuceneFields.getKeyword(name, value);
135 }
136
137 luceneField.setBoost(boost);
138
139 luceneDocument.add(luceneField);
140 }
141 }
142
143 return luceneDocument;
144 }
145
146 private static Log _log =
147 LogFactoryUtil.getLog(LuceneIndexWriterImpl.class);
148
149 }