-
Notifications
You must be signed in to change notification settings - Fork 242
/
ElasticsearchExtension.java
231 lines (217 loc) · 11 KB
/
ElasticsearchExtension.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
/*
* 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;
import java.lang.invoke.MethodHandles;
import java.util.Optional;
import org.hibernate.search.backend.elasticsearch.search.aggregation.impl.ElasticsearchSearchAggregationBuilderFactory;
import org.hibernate.search.backend.elasticsearch.search.aggregation.dsl.ElasticsearchSearchAggregationFactory;
import org.hibernate.search.backend.elasticsearch.search.aggregation.dsl.impl.ElasticsearchSearchAggregationFactoryImpl;
import org.hibernate.search.backend.elasticsearch.search.projection.dsl.ElasticsearchSearchProjectionFactory;
import org.hibernate.search.backend.elasticsearch.search.projection.dsl.impl.ElasticsearchSearchProjectionFactoryImpl;
import org.hibernate.search.backend.elasticsearch.search.query.dsl.ElasticsearchSearchQuerySelectStep;
import org.hibernate.search.backend.elasticsearch.search.predicate.dsl.ElasticsearchSearchPredicateFactory;
import org.hibernate.search.backend.elasticsearch.search.query.dsl.impl.ElasticsearchSearchQuerySelectStepImpl;
import org.hibernate.search.backend.elasticsearch.search.projection.impl.ElasticsearchSearchProjectionBuilderFactory;
import org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchQuery;
import org.hibernate.search.backend.elasticsearch.scope.impl.ElasticsearchIndexScope;
import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactory;
import org.hibernate.search.engine.backend.types.dsl.IndexFieldTypeFactoryExtension;
import org.hibernate.search.backend.elasticsearch.types.dsl.ElasticsearchIndexFieldTypeFactory;
import org.hibernate.search.backend.elasticsearch.logging.impl.Log;
import org.hibernate.search.backend.elasticsearch.search.predicate.dsl.impl.ElasticsearchSearchPredicateFactoryImpl;
import org.hibernate.search.backend.elasticsearch.search.sort.dsl.ElasticsearchSearchSortFactory;
import org.hibernate.search.backend.elasticsearch.search.sort.dsl.impl.ElasticsearchSearchSortFactoryImpl;
import org.hibernate.search.backend.elasticsearch.search.predicate.impl.ElasticsearchSearchPredicateBuilderFactory;
import org.hibernate.search.backend.elasticsearch.search.sort.impl.ElasticsearchSearchSortBuilder;
import org.hibernate.search.backend.elasticsearch.search.sort.impl.ElasticsearchSearchSortBuilderFactory;
import org.hibernate.search.engine.backend.session.spi.BackendSessionContext;
import org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactory;
import org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactoryExtension;
import org.hibernate.search.engine.search.aggregation.dsl.spi.SearchAggregationDslContext;
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory;
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactoryExtension;
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory;
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactoryExtension;
import org.hibernate.search.engine.search.query.dsl.SearchQueryDslExtension;
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
import org.hibernate.search.engine.search.sort.dsl.SearchSortFactory;
import org.hibernate.search.engine.search.sort.dsl.SearchSortFactoryExtension;
import org.hibernate.search.engine.search.sort.dsl.spi.SearchSortDslContext;
import org.hibernate.search.engine.backend.scope.spi.IndexScope;
import org.hibernate.search.engine.search.loading.context.spi.LoadingContext;
import org.hibernate.search.engine.search.loading.context.spi.LoadingContextBuilder;
import org.hibernate.search.engine.search.predicate.spi.SearchPredicateBuilderFactory;
import org.hibernate.search.engine.search.projection.spi.SearchProjectionBuilderFactory;
import org.hibernate.search.engine.search.query.SearchQuery;
import org.hibernate.search.engine.search.query.SearchQueryExtension;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
/**
* An extension for the Elasticsearch backend, giving access to Lucene-specific features.
* <p>
* <strong>WARNING:</strong> while this type is API, because instances should be manipulated by users,
* all of its methods are considered SPIs and therefore should never be called directly by users.
* In short, users are only expected to get instances of this type from an API and pass it to another API.
*
* @param <H> The type of query hits.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <R> The entity reference type for projections.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <E> The entity type for projections.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <LOS> The type of the initial step of the loading options definition DSL.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
*
* @see #get()
*/
public final class ElasticsearchExtension<H, R, E, LOS>
implements SearchQueryDslExtension<ElasticsearchSearchQuerySelectStep<R, E, LOS>, R, E, LOS>,
SearchQueryExtension<ElasticsearchSearchQuery<H>, H>,
SearchPredicateFactoryExtension<ElasticsearchSearchPredicateFactory>,
SearchSortFactoryExtension<ElasticsearchSearchSortFactory>,
SearchProjectionFactoryExtension<ElasticsearchSearchProjectionFactory<R, E>, R, E>,
SearchAggregationFactoryExtension<ElasticsearchSearchAggregationFactory>,
IndexFieldTypeFactoryExtension<ElasticsearchIndexFieldTypeFactory> {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private static final ElasticsearchExtension<Object, Object, Object, Object> INSTANCE = new ElasticsearchExtension<>();
/**
* Get the extension with generic parameters automatically set as appropriate for the context in which it's used.
*
* @param <H> The type of query hits.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <R> The entity reference type for projections.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <E> The entity type for projections.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @param <LOS> The type of the initial step of the loading options definition DSL.
* Users should not have to care about this, as the parameter will automatically take the appropriate value when calling
* {@code .extension( ElasticsearchExtension.get() }.
* @return The extension.
*/
@SuppressWarnings("unchecked") // The instance works for any H, R, E and LOS
public static <H, R, E, LOS> ElasticsearchExtension<H, R, E, LOS> get() {
return (ElasticsearchExtension<H, R, E, LOS>) INSTANCE;
}
private ElasticsearchExtension() {
// Private constructor, use get() instead.
}
/**
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchQuerySelectStep<R, E, LOS>> extendOptional(
SearchQuerySelectStep<?, R, E, LOS, ?, ?> original,
IndexScope<?> indexScope,
BackendSessionContext sessionContext,
LoadingContextBuilder<R, E, LOS> loadingContextBuilder) {
if ( indexScope instanceof ElasticsearchIndexScope ) {
return Optional.of( new ElasticsearchSearchQuerySelectStepImpl<>(
(ElasticsearchIndexScope) indexScope, sessionContext, loadingContextBuilder
) );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchQuery<H>> extendOptional(SearchQuery<H> original,
LoadingContext<?, ?> loadingContext) {
if ( original instanceof ElasticsearchSearchQuery ) {
return Optional.of( (ElasticsearchSearchQuery<H>) original );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
public <C, B> Optional<ElasticsearchSearchPredicateFactory> extendOptional(
SearchPredicateFactory original, SearchPredicateBuilderFactory<C, B> factory) {
if ( factory instanceof ElasticsearchSearchPredicateBuilderFactory ) {
return Optional.of( new ElasticsearchSearchPredicateFactoryImpl(
original, (ElasticsearchSearchPredicateBuilderFactory) factory
) );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked") // If the factory is an instance of ElasticsearchSearchSortBuilderFactory, the cast is safe
public Optional<ElasticsearchSearchSortFactory> extendOptional(
SearchSortFactory original, SearchSortDslContext<?, ?, ?> dslContext) {
if ( dslContext.getBuilderFactory() instanceof ElasticsearchSearchSortBuilderFactory ) {
return Optional.of( new ElasticsearchSearchSortFactoryImpl(
original,
((SearchSortDslContext<ElasticsearchSearchSortBuilderFactory, ElasticsearchSearchSortBuilder, ?>) dslContext)
.withExtendedPredicateFactory( this )
) );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
public Optional<ElasticsearchSearchProjectionFactory<R, E>> extendOptional(
SearchProjectionFactory<R, E> original, SearchProjectionBuilderFactory factory) {
if ( factory instanceof ElasticsearchSearchProjectionBuilderFactory ) {
return Optional.of( new ElasticsearchSearchProjectionFactoryImpl<>(
original, (ElasticsearchSearchProjectionBuilderFactory) factory
) );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings("unchecked") // If the factory is an instance of ElasticsearchSearchAggregationBuilderFactory, the cast is safe
public Optional<ElasticsearchSearchAggregationFactory> extendOptional(
SearchAggregationFactory original, SearchAggregationDslContext<?, ?> dslContext) {
if ( dslContext.getBuilderFactory() instanceof ElasticsearchSearchAggregationBuilderFactory ) {
return Optional.of( new ElasticsearchSearchAggregationFactoryImpl(
original,
((SearchAggregationDslContext<ElasticsearchSearchAggregationBuilderFactory, ?>) dslContext)
.withExtendedPredicateFactory( this )
) );
}
else {
return Optional.empty();
}
}
/**
* {@inheritDoc}
*/
@Override
public ElasticsearchIndexFieldTypeFactory extendOrFail(IndexFieldTypeFactory original) {
if ( original instanceof ElasticsearchIndexFieldTypeFactory ) {
return (ElasticsearchIndexFieldTypeFactory) original;
}
else {
throw log.elasticsearchExtensionOnUnknownType( original );
}
}
}