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(String field, String number) {
102 NumericField numericField = new NumericField(
103 field, Field.Store.YES, true);
104
105 numericField.setLongValue(GetterUtil.getLong(number));
106
107 return numericField;
108 }
109
110 public static Field getText(String field, String text) {
111 return new Field(field, text, Field.Store.YES, Field.Index.ANALYZED);
112 }
113
114 public static Field getText(String field, StringBuilder sb) {
115 return getText(field, sb.toString());
116 }
117
118 public static String getUID(String portletId, long field1) {
119 return getUID(portletId, String.valueOf(field1));
120 }
121
122 public static String getUID(String portletId, long field1, String field2) {
123 return getUID(portletId, String.valueOf(field1), field2);
124 }
125
126 public static String getUID(String portletId, Long field1) {
127 return getUID(portletId, field1.longValue());
128 }
129
130 public static String getUID(String portletId, Long field1, String field2) {
131 return getUID(portletId, field1.longValue(), field2);
132 }
133
134 public static String getUID(String portletId, String field1) {
135 return getUID(portletId, field1, null);
136 }
137
138 public static String getUID(
139 String portletId, String field1, String field2) {
140
141 return getUID(portletId, field1, field2, null);
142 }
143
144 public static String getUID(
145 String portletId, String field1, String field2, String field3) {
146
147 String uid = portletId + _UID_PORTLET + field1;
148
149 if (field2 != null) {
150 uid += _UID_FIELD + field2;
151 }
152
153 if (field3 != null) {
154 uid += _UID_FIELD + field3;
155 }
156
157 return uid;
158 }
159
160 private static final String _UID_FIELD = "_FIELD_";
161
162 private static final String _UID_PORTLET = "_PORTLET_";
163
164 }