-
Notifications
You must be signed in to change notification settings - Fork 242
/
ElasticsearchStringIndexFieldTypeOptionsStep.java
237 lines (197 loc) · 8.7 KB
/
ElasticsearchStringIndexFieldTypeOptionsStep.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.backend.elasticsearch.types.dsl.impl;
import java.lang.invoke.MethodHandles;
import java.util.Locale;
import org.hibernate.search.backend.elasticsearch.logging.impl.Log;
import org.hibernate.search.backend.elasticsearch.lowlevel.index.mapping.impl.DataTypes;
import org.hibernate.search.backend.elasticsearch.lowlevel.index.mapping.impl.PropertyMapping;
import org.hibernate.search.backend.elasticsearch.search.aggregation.impl.AggregationTypeKeys;
import org.hibernate.search.backend.elasticsearch.search.aggregation.impl.ElasticsearchTermsAggregation;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchExistsPredicate;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchRangePredicate;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.PredicateTypeKeys;
import org.hibernate.search.backend.elasticsearch.search.projection.impl.ElasticsearchFieldProjection;
import org.hibernate.search.backend.elasticsearch.search.projection.impl.ProjectionTypeKeys;
import org.hibernate.search.backend.elasticsearch.search.sort.impl.SortTypeKeys;
import org.hibernate.search.backend.elasticsearch.types.codec.impl.ElasticsearchStringFieldCodec;
import org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchSimpleQueryStringPredicateBuilderFieldState;
import org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchTextMatchPredicate;
import org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchTextPhrasePredicate;
import org.hibernate.search.backend.elasticsearch.types.predicate.impl.ElasticsearchTextWildcardPredicate;
import org.hibernate.search.backend.elasticsearch.types.sort.impl.ElasticsearchStandardFieldSort;
import org.hibernate.search.engine.backend.types.Aggregable;
import org.hibernate.search.engine.backend.types.IndexFieldType;
import org.hibernate.search.engine.backend.types.Norms;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.backend.types.Searchable;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.backend.types.TermVector;
import org.hibernate.search.engine.backend.types.dsl.StringIndexFieldTypeOptionsStep;
import org.hibernate.search.util.common.AssertionFailure;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
import com.google.gson.JsonPrimitive;
class ElasticsearchStringIndexFieldTypeOptionsStep
extends AbstractElasticsearchStandardIndexFieldTypeOptionsStep<ElasticsearchStringIndexFieldTypeOptionsStep, String>
implements StringIndexFieldTypeOptionsStep<ElasticsearchStringIndexFieldTypeOptionsStep> {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private String analyzerName;
private String searchAnalyzerName;
private String normalizerName;
private Projectable projectable = Projectable.DEFAULT;
private Searchable searchable = Searchable.DEFAULT;
private Norms norms = Norms.DEFAULT;
private Sortable sortable = Sortable.DEFAULT;
private Aggregable aggregable = Aggregable.DEFAULT;
private String indexNullAs;
private TermVector termVector = TermVector.DEFAULT;
ElasticsearchStringIndexFieldTypeOptionsStep(ElasticsearchIndexFieldTypeBuildContext buildContext) {
super( buildContext, String.class );
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep analyzer(String analyzerName) {
this.analyzerName = analyzerName;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep searchAnalyzer(String searchAnalyzerName) {
this.searchAnalyzerName = searchAnalyzerName;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep normalizer(String normalizerName) {
this.normalizerName = normalizerName;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep projectable(Projectable projectable) {
this.projectable = projectable;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep norms(Norms norms) {
this.norms = norms;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep termVector(TermVector termVector) {
this.termVector = termVector;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep sortable(Sortable sortable) {
this.sortable = sortable;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep indexNullAs(String indexNullAs) {
this.indexNullAs = indexNullAs;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep searchable(Searchable searchable) {
this.searchable = searchable;
return this;
}
@Override
public ElasticsearchStringIndexFieldTypeOptionsStep aggregable(Aggregable aggregable) {
this.aggregable = aggregable;
return this;
}
@Override
public IndexFieldType<String> toIndexFieldType() {
PropertyMapping mapping = builder.mapping();
boolean resolvedSortable = resolveDefault( sortable );
boolean resolvedProjectable = resolveDefault( projectable );
boolean resolvedSearchable = resolveDefault( searchable );
boolean resolvedAggregable = resolveDefault( aggregable );
mapping.setIndex( resolvedSearchable );
if ( analyzerName != null ) {
mapping.setType( DataTypes.TEXT );
mapping.setAnalyzer( analyzerName );
mapping.setSearchAnalyzer( searchAnalyzerName );
mapping.setTermVector( resolveTermVector() );
builder.analyzerName( analyzerName );
builder.searchAnalyzerName( searchAnalyzerName );
if ( normalizerName != null ) {
throw log.cannotApplyAnalyzerAndNormalizer( analyzerName, normalizerName, buildContext.getEventContext() );
}
if ( resolvedSortable ) {
throw log.cannotUseAnalyzerOnSortableField( analyzerName, buildContext.getEventContext() );
}
if ( indexNullAs != null ) {
throw log.cannotUseIndexNullAsAndAnalyzer( analyzerName, indexNullAs, buildContext.getEventContext() );
}
if ( resolvedAggregable ) {
throw log.cannotUseAnalyzerOnAggregableField( analyzerName, buildContext.getEventContext() );
}
}
else {
mapping.setType( DataTypes.KEYWORD );
mapping.setNormalizer( normalizerName );
mapping.setDocValues( resolvedSortable || resolvedAggregable );
builder.normalizerName( normalizerName );
if ( searchAnalyzerName != null ) {
throw log.searchAnalyzerWithoutAnalyzer( searchAnalyzerName, buildContext.getEventContext() );
}
}
mapping.setNorms( resolveNorms() );
if ( indexNullAs != null ) {
mapping.setNullValue( new JsonPrimitive( indexNullAs ) );
}
ElasticsearchStringFieldCodec codec = ElasticsearchStringFieldCodec.INSTANCE;
builder.codec( codec );
if ( resolvedSearchable ) {
builder.searchable( true );
builder.queryElementFactory( PredicateTypeKeys.MATCH, new ElasticsearchTextMatchPredicate.Factory( codec ) );
builder.queryElementFactory( PredicateTypeKeys.RANGE, new ElasticsearchRangePredicate.Factory<>( codec ) );
builder.queryElementFactory( PredicateTypeKeys.EXISTS, new ElasticsearchExistsPredicate.Factory<>() );
builder.queryElementFactory( PredicateTypeKeys.PHRASE, new ElasticsearchTextPhrasePredicate.Factory() );
builder.queryElementFactory( PredicateTypeKeys.WILDCARD, new ElasticsearchTextWildcardPredicate.Factory() );
builder.queryElementFactory( PredicateTypeKeys.SIMPLE_QUERY_STRING,
new ElasticsearchSimpleQueryStringPredicateBuilderFieldState.Factory() );
}
if ( resolvedSortable ) {
builder.sortable( true );
builder.queryElementFactory( SortTypeKeys.FIELD, new ElasticsearchStandardFieldSort.TextFieldFactory( codec ) );
}
if ( resolvedProjectable ) {
builder.projectable( true );
builder.queryElementFactory( ProjectionTypeKeys.FIELD, new ElasticsearchFieldProjection.Factory<>( codec ) );
}
if ( resolvedAggregable ) {
builder.aggregable( true );
builder.queryElementFactory( AggregationTypeKeys.TERMS, new ElasticsearchTermsAggregation.Factory<>( codec ) );
}
return builder.build();
}
@Override
protected ElasticsearchStringIndexFieldTypeOptionsStep thisAsS() {
return this;
}
private boolean resolveNorms() {
switch ( norms ) {
case YES:
return true;
case NO:
return false;
case DEFAULT:
return ( analyzerName != null );
default:
throw new AssertionFailure( "Unexpected value for Norms: " + norms );
}
}
private String resolveTermVector() {
switch ( termVector ) {
case NO:
case DEFAULT:
return "no";
default:
return termVector.name().toLowerCase( Locale.ROOT );
}
}
}