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 Field getDate(String field) {
037 return getDate(field, new Date());
038 }
039
040 public static Field getDate(String field, Date date) {
041 if (date == null) {
042 return getDate(field);
043 }
044 else {
045 return new Field(
046 field,
047 DateTools.dateToString(date, DateTools.Resolution.SECOND),
048 Field.Store.YES, Field.Index.NOT_ANALYZED);
049 }
050 }
051
052 public static Field getFile(String field, byte[] bytes, String fileExt) {
053 LuceneFileExtractor fileExtractor =
054 (LuceneFileExtractor)InstancePool.get(
055 PropsValues.LUCENE_FILE_EXTRACTOR);
056
057 return fileExtractor.getFile(field, bytes, fileExt);
058 }
059
060 public static Field getFile(String field, File file, String fileExt)
061 throws IOException {
062
063 LuceneFileExtractor fileExtractor =
064 (LuceneFileExtractor)InstancePool.get(
065 PropsValues.LUCENE_FILE_EXTRACTOR);
066
067 return fileExtractor.getFile(field, file, fileExt);
068 }
069
070 public static Field getFile(String field, InputStream is, String fileExt) {
071 LuceneFileExtractor fileExtractor =
072 (LuceneFileExtractor)InstancePool.get(
073 PropsValues.LUCENE_FILE_EXTRACTOR);
074
075 return fileExtractor.getFile(field, is, fileExt);
076 }
077
078 public static Field getKeyword(String field, double keyword) {
079 return getKeyword(field, String.valueOf(keyword));
080 }
081
082 public static Field getKeyword(String field, long keyword) {
083 return getKeyword(field, String.valueOf(keyword));
084 }
085
086 public static Field getKeyword(String field, Long keyword) {
087 return getKeyword(field, keyword.longValue());
088 }
089
090 public static Field getKeyword(String field, String keyword) {
091
092
093 Field fieldObj = new Field(
094 field, keyword, Field.Store.YES, Field.Index.NOT_ANALYZED);
095
096
097
098 return fieldObj;
099 }
100
101 public static NumericField getNumber(
102 String field, String number, Class<? extends Number> clazz) {
103
104 NumericField numericField = new NumericField(
105 field, Field.Store.YES, true);
106
107 if (clazz.equals(Double.class)) {
108 numericField.setDoubleValue(GetterUtil.getDouble(number));
109 }
110 else if (clazz.equals(Float.class)) {
111 numericField.setFloatValue(GetterUtil.getFloat(number));
112 }
113 else if (clazz.equals(Integer.class)) {
114 numericField.setIntValue(GetterUtil.getInteger(number));
115 }
116 else {
117 numericField.setLongValue(GetterUtil.getLong(number));
118 }
119
120 return numericField;
121 }
122
123 public static Field getText(String field, String text) {
124 return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
125 }
126
127 public static Field getText(String field, StringBuilder sb) {
128 return getText(field, sb.toString());
129 }
130
131 public static String getUID(String portletId, long field1) {
132 return getUID(portletId, String.valueOf(field1));
133 }
134
135 public static String getUID(String portletId, long field1, String field2) {
136 return getUID(portletId, String.valueOf(field1), field2);
137 }
138
139 public static String getUID(String portletId, Long field1) {
140 return getUID(portletId, field1.longValue());
141 }
142
143 public static String getUID(String portletId, Long field1, String field2) {
144 return getUID(portletId, field1.longValue(), field2);
145 }
146
147 public static String getUID(String portletId, String field1) {
148 return getUID(portletId, field1, null);
149 }
150
151 public static String getUID(
152 String portletId, String field1, String field2) {
153
154 return getUID(portletId, field1, field2, null);
155 }
156
157 public static String getUID(
158 String portletId, String field1, String field2, String field3) {
159
160 String uid = portletId + _UID_PORTLET + field1;
161
162 if (field2 != null) {
163 uid += _UID_FIELD + field2;
164 }
165
166 if (field3 != null) {
167 uid += _UID_FIELD + field3;
168 }
169
170 return uid;
171 }
172
173 private static final String _UID_FIELD = "_FIELD_";
174
175 private static final String _UID_PORTLET = "_PORTLET_";
176
177 }