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