1
22
23 package com.liferay.portal.search.lucene;
24
25 import com.liferay.portal.kernel.search.Document;
26 import com.liferay.portal.kernel.search.Field;
27 import com.liferay.portal.kernel.search.IndexWriter;
28 import com.liferay.portal.kernel.search.SearchException;
29
30 import java.io.IOException;
31
32 import java.util.Collection;
33
34 import org.apache.lucene.index.Term;
35
36
44 public class LuceneIndexWriterImpl implements IndexWriter {
45
46 public void addDocument(long companyId, Document doc)
47 throws SearchException {
48
49 org.apache.lucene.index.IndexWriter writer = null;
50
51 try {
52 writer = LuceneUtil.getWriter(companyId);
53
54 writer.addDocument(_getLuceneDocument(doc));
55 }
56 catch (IOException ioe) {
57 throw new SearchException(ioe);
58 }
59 finally {
60 if (writer != null) {
61 LuceneUtil.write(companyId);
62 }
63 }
64 }
65
66 public void deleteDocument(long companyId, String uid)
67 throws SearchException {
68
69 try {
70 LuceneUtil.deleteDocuments(companyId, new Term(Field.UID, uid));
71 }
72 catch (IOException ioe) {
73 throw new SearchException(ioe);
74 }
75 }
76
77 public void deletePortletDocuments(long companyId, String portletId)
78 throws SearchException {
79
80 try {
81 LuceneUtil.deleteDocuments(
82 companyId, new Term(Field.PORTLET_ID, portletId));
83 }
84 catch (IOException ioe) {
85 throw new SearchException(ioe);
86 }
87 }
88
89 public void updateDocument(long companyId, String uid, Document doc)
90 throws SearchException {
91
92 deleteDocument(companyId, uid);
93
94 addDocument(companyId, doc);
95 }
96
97 private org.apache.lucene.document.Document _getLuceneDocument(
98 Document doc) {
99
100 org.apache.lucene.document.Document luceneDoc =
101 new org.apache.lucene.document.Document();
102
103 Collection<Field> values = doc.getFields().values();
104
105 for (Field field : values) {
106 if (field.isTokenized()) {
107 for (String value : field.getValues()) {
108 LuceneUtil.addText(luceneDoc, field.getName(), value);
109 }
110 }
111 else {
112 for (String value : field.getValues()) {
113 LuceneUtil.addKeyword(luceneDoc, field.getName(), value);
114 }
115 }
116 }
117
118 return luceneDoc;
119 }
120
121 }