001
014
015 package com.liferay.portal.search.lucene;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.InstancePool;
019 import com.liferay.portal.util.PropsValues;
020
021 import java.io.File;
022 import java.io.IOException;
023 import java.io.InputStream;
024
025 import java.util.Date;
026
027 import org.apache.lucene.document.DateTools;
028 import org.apache.lucene.document.Field;
029 import org.apache.lucene.document.NumericField;
030
031
034 public class LuceneFields {
035
036 public static String getUID(String portletId, long field1) {
037 return getUID(portletId, String.valueOf(field1));
038 }
039
040 public static String getUID(String portletId, Long field1) {
041 return getUID(portletId, field1.longValue());
042 }
043
044 public static String getUID(String portletId, String field1) {
045 return getUID(portletId, field1, null);
046 }
047
048 public static String getUID(
049 String portletId, long field1, String field2) {
050
051 return getUID(portletId, String.valueOf(field1), field2);
052 }
053
054 public static String getUID(
055 String portletId, Long field1, String field2) {
056
057 return getUID(portletId, field1.longValue(), field2);
058 }
059
060 public static String getUID(
061 String portletId, String field1, String field2) {
062
063 return getUID(portletId, field1, field2, null);
064 }
065
066 public static String getUID(
067 String portletId, String field1, String field2, String field3) {
068
069 String uid = portletId + _UID_PORTLET + field1;
070
071 if (field2 != null) {
072 uid += _UID_FIELD + field2;
073 }
074
075 if (field3 != null) {
076 uid += _UID_FIELD + field3;
077 }
078
079 return uid;
080 }
081
082 public static Field getDate(String field) {
083 return getDate(field, new Date());
084 }
085
086 public static Field getDate(String field, Date date) {
087 if (date == null) {
088 return getDate(field);
089 }
090 else {
091 return new Field(
092 field,
093 DateTools.dateToString(date, DateTools.Resolution.SECOND),
094 Field.Store.YES, Field.Index.NOT_ANALYZED);
095 }
096 }
097
098 public static Field getFile(String field, InputStream is, String fileExt) {
099 LuceneFileExtractor fileExtractor =
100 (LuceneFileExtractor)InstancePool.get(
101 PropsValues.LUCENE_FILE_EXTRACTOR);
102
103 return fileExtractor.getFile(field, is, fileExt);
104 }
105
106 public static Field getFile(String field, byte[] bytes, String fileExt) {
107 LuceneFileExtractor fileExtractor =
108 (LuceneFileExtractor)InstancePool.get(
109 PropsValues.LUCENE_FILE_EXTRACTOR);
110
111 return fileExtractor.getFile(field, bytes, fileExt);
112 }
113
114 public static Field getFile(String field, File file, String fileExt)
115 throws IOException {
116
117 LuceneFileExtractor fileExtractor =
118 (LuceneFileExtractor)InstancePool.get(
119 PropsValues.LUCENE_FILE_EXTRACTOR);
120
121 return fileExtractor.getFile(field, file, fileExt);
122 }
123
124 public static Field getKeyword(String field, double keyword) {
125 return getKeyword(field, String.valueOf(keyword));
126 }
127
128 public static Field getKeyword(String field, long keyword) {
129 return getKeyword(field, String.valueOf(keyword));
130 }
131
132 public static Field getKeyword(String field, Long keyword) {
133 return getKeyword(field, keyword.longValue());
134 }
135
136 public static Field getKeyword(String field, String keyword) {
137
138
139 Field fieldObj = new Field(
140 field, keyword, Field.Store.YES, Field.Index.NOT_ANALYZED);
141
142
143
144 return fieldObj;
145 }
146
147 public static NumericField getNumber(String field, String number) {
148 NumericField numericField = new NumericField(
149 field, Field.Store.YES, true);
150
151 numericField.setLongValue(GetterUtil.getLong(number));
152
153 return numericField;
154 }
155
156 public static Field getText(String field, String text) {
157 return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
158 }
159
160 public static Field getText(String field, StringBuilder sb) {
161 return getText(field, sb.toString());
162 }
163
164 private static final String _UID_PORTLET = "_PORTLET_";
165
166 private static final String _UID_FIELD = "_FIELD_";
167
168 }