-
Notifications
You must be signed in to change notification settings - Fork 242
/
FieldMapping.java
237 lines (206 loc) · 7.13 KB
/
FieldMapping.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.cfg;
import java.lang.annotation.ElementType;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.analysis.util.TokenizerFactory;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Norms;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TermVector;
import org.hibernate.search.util.StringHelper;
/**
* @author Emmanuel Bernard
*/
public class FieldMapping {
private final SearchMapping mapping;
private final EntityDescriptor entity;
private final PropertyDescriptor property;
private final Map<String, Object> field;
public FieldMapping(PropertyDescriptor property, EntityDescriptor entity, SearchMapping mapping) {
this.mapping = mapping;
this.entity = entity;
this.property = property;
this.field = new HashMap<String, Object>();
property.addField( field );
}
/**
* @deprecated Only provided to avoid breaking the API in {@link NumericFieldMapping}, which extends this class.
* Do not extend this class, just re-implement methods.
*/
@Deprecated
FieldMapping(String fieldName, PropertyDescriptor property, EntityDescriptor entity, SearchMapping mapping) {
this.mapping = mapping;
this.entity = entity;
this.property = property;
Map<String, Object> existingField = getExistingField( property, fieldName );
if ( existingField != null ) {
this.field = existingField;
}
else {
this.field = new HashMap<String, Object>();
property.addField( field );
}
}
private static Map<String, Object> getExistingField(PropertyDescriptor property, String nameOfFieldToGet) {
if ( nameOfFieldToGet.isEmpty() ) {
nameOfFieldToGet = property.getName();
}
for ( Map<String, Object> field : property.getFields() ) {
String fieldName = (String) field.get( "name" );
if ( StringHelper.isEmpty( fieldName ) ) {
fieldName = property.getName();
}
if ( nameOfFieldToGet.equals( fieldName ) ) {
return field;
}
}
return null;
}
public FieldMapping name(String fieldName) {
field.put( "name", fieldName );
return this;
}
public FieldMapping store(Store store) {
field.put( "store", store );
return this;
}
public FieldMapping index(Index index) {
field.put( "index", index );
return this;
}
public FieldMapping analyze(Analyze analyze) {
field.put( "analyze", analyze );
return this;
}
public FieldMapping norms(Norms norms) {
field.put( "norms", norms );
return this;
}
public FieldMapping termVector(TermVector termVector) {
field.put( "termVector", termVector );
return this;
}
/**
* @deprecated Index-time boosting will not be possible anymore starting from Lucene 7.
* You should use query-time boosting instead, for instance by calling
* {@link org.hibernate.search.query.dsl.FieldCustomization#boostedTo(float) boostedTo(float)}
* when building queries with the Hibernate Search query DSL.
*/
@Deprecated
public FieldMapping boost(float boost) {
final Map<String, Object> boostAnn = new HashMap<String, Object>();
boostAnn.put( "value", boost );
field.put( "boost", boostAnn );
return this;
}
public FieldBridgeMapping bridge(Class<?> impl) {
return new FieldBridgeMapping( impl, field, this, property, entity, mapping );
}
/**
* @return An {@link org.apache.lucene.analysis.Analyzer} implementation.
* @deprecated Support for direct references to analyzer implementations
* by class will be removed in Hibernate Search 6.
* Use {@link #analyzer(String)} instead.
*/
@Deprecated
public FieldMapping analyzer(Class<?> analyzerClass) {
final Map<String, Object> analyzer = new HashMap<String, Object>();
analyzer.put( "impl", analyzerClass );
field.put( "analyzer", analyzer );
return this;
}
public FieldMapping analyzer(String analyzerDef) {
final Map<String, Object> analyzer = new HashMap<String, Object>();
analyzer.put( "definition", analyzerDef );
field.put( "analyzer", analyzer );
return this;
}
/**
* @return An {@link org.apache.lucene.analysis.Analyzer} implementation.
* @deprecated Support for direct references to normalizer implementations
* by class will be removed in Hibernate Search 6.
* Use {@link #normalizer(String)} instead.
*/
@Deprecated
public FieldMapping normalizer(Class<?> analyzerClass) {
final Map<String, Object> normalizer = new HashMap<String, Object>();
normalizer.put( "impl", analyzerClass );
field.put( "normalizer", normalizer );
return this;
}
public FieldMapping normalizer(String analyzerDef) {
final Map<String, Object> normalizer = new HashMap<String, Object>();
normalizer.put( "definition", analyzerDef );
field.put( "normalizer", normalizer );
return this;
}
public FieldMapping indexNullAs(String indexNullAs) {
field.put( "indexNullAs", indexNullAs );
return this;
}
public FieldMapping field() {
return new FieldMapping( property, entity, mapping );
}
public NumericFieldMapping numericField() {
String forField = (String) field.get( "name" );
if ( forField == null ) {
forField = property.getName();
}
return new NumericFieldMapping( forField, property, entity, mapping );
}
public SortableFieldMapping sortableField() {
String forField = (String) field.get( "name" );
if ( forField == null ) {
forField = property.getName();
}
return new SortableFieldMapping( forField, property, entity, mapping );
}
public FacetMapping facet() {
String forField = (String) field.get( "name" );
if ( forField == null ) {
forField = property.getName();
}
return new FacetMapping( forField, property, entity, mapping );
}
public PropertyMapping property(String name, ElementType type) {
return new PropertyMapping( name, type, entity, mapping );
}
public DateBridgeMapping dateBridge(Resolution resolution) {
return new DateBridgeMapping( mapping, entity, property, resolution );
}
/**
* @deprecated See {@link org.hibernate.search.annotations.AnalyzerDef}
*/
@Deprecated
public AnalyzerDefMapping analyzerDef(String name, Class<? extends TokenizerFactory> tokenizerFactory) {
return analyzerDef( name, "", tokenizerFactory );
}
/**
* @deprecated See {@link org.hibernate.search.annotations.AnalyzerDef}
*/
@Deprecated
public AnalyzerDefMapping analyzerDef(String name, String tokenizerName, Class<? extends TokenizerFactory> tokenizerFactory) {
return new AnalyzerDefMapping( name, tokenizerName, tokenizerFactory, mapping );
}
/**
* @deprecated See {@link org.hibernate.search.annotations.AnalyzerDef}
*/
@Deprecated
public NormalizerDefMapping normalizerDef(String name) {
return new NormalizerDefMapping( name, mapping );
}
public EntityMapping entity(Class<?> entityType) {
return new EntityMapping( entityType, mapping );
}
public CalendarBridgeMapping calendarBridge(Resolution resolution) {
return new CalendarBridgeMapping( mapping, entity, property, resolution );
}
}