-
Notifications
You must be signed in to change notification settings - Fork 242
/
SearchQueryBaseIT.java
245 lines (204 loc) · 8.7 KB
/
SearchQueryBaseIT.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
238
239
240
241
242
243
244
245
/*
* 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.integrationtest.backend.tck.search.query;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMapperUtils.referenceProvider;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.work.execution.spi.IndexIndexer;
import org.hibernate.search.engine.backend.types.Sortable;
import org.hibernate.search.engine.backend.session.spi.BackendSessionContext;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.search.query.dsl.SearchQueryDslExtension;
import org.hibernate.search.engine.search.query.dsl.SearchQueryWhereStep;
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
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.query.SearchQuery;
import org.hibernate.search.engine.search.query.SearchQueryExtension;
import org.hibernate.search.engine.search.query.SearchResult;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubLoadingContext;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingIndexManager;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.assertj.core.api.Assertions;
public class SearchQueryBaseIT {
private static final String INDEX_NAME = "IndexName";
@Rule
public SearchSetupHelper setupHelper = new SearchSetupHelper();
private StubMappingIndexManager indexManager;
private IndexMapping indexMapping;
@Before
public void setup() {
setupHelper.start()
.withIndex(
INDEX_NAME,
ctx -> this.indexMapping = new IndexMapping( ctx.getSchemaElement() ),
indexManager -> this.indexManager = indexManager
)
.setup();
}
@Test
public void getQueryString() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query()
.where( f -> f.match().field( "string" ).matching( "platypus" ) )
.toQuery();
assertThat( query.getQueryString() ).contains( "platypus" );
}
@Test
public void tookAndTimedOut() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query()
.where( f -> f.matchAll() )
.toQuery();
SearchResult<DocumentReference> result = query.fetchAll();
assertNotNull( result.getTook() );
assertNotNull( result.isTimedOut() );
assertFalse( result.isTimedOut() );
}
@Test
public void extension() {
initData( 2 );
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query()
.where( f -> f.matchAll() )
.toQuery();
// Mandatory extension, supported
QueryWrapper<DocumentReference> extendedQuery = query.extension( new SupportedQueryExtension<>() );
SearchResultAssert.assertThat( extendedQuery.extendedFetch() ).fromQuery( query )
.hasDocRefHitsAnyOrder( INDEX_NAME, "0", "1" );
// Mandatory extension, unsupported
Assertions.assertThatThrownBy(
() -> query.extension( new UnSupportedQueryExtension<>() )
)
.isInstanceOf( SearchException.class );
}
@Test
public void context_extension() {
initData( 5 );
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query;
// Mandatory extension, supported
query = scope.query()
.extension( new SupportedQueryDslExtension<>() )
.extendedFeature( "string", "value1", "value2" );
SearchResultAssert.assertThat( query )
.hasDocRefHitsExactOrder( INDEX_NAME, "1","2" );
// Mandatory extension, unsupported
Assertions.assertThatThrownBy(
() -> scope.query()
.extension( new UnSupportedQueryDslExtension<>() )
)
.isInstanceOf( SearchException.class );
}
private void initData(int documentCount) {
IndexIndexer executor =
indexManager.createIndexer();
List<CompletableFuture<?>> futures = new ArrayList<>();
for ( int i = 0; i < documentCount; i++ ) {
int intValue = i;
futures.add( executor.add( referenceProvider( String.valueOf( intValue ) ), document -> {
document.addValue( indexMapping.string, "value" + intValue );
} ) );
}
CompletableFuture.allOf( futures.toArray( new CompletableFuture<?>[0] ) ).join();
indexManager.createWorkspace().refresh().join();
// Check that all documents are searchable
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query()
.where( f -> f.matchAll() )
.toQuery();
SearchResultAssert.assertThat( query ).hasTotalHitCount( documentCount );
}
private static class IndexMapping {
final IndexFieldReference<String> string;
IndexMapping(IndexSchemaElement root) {
string = root.field( "string", f -> f.asString().sortable( Sortable.YES ) )
.toReference();
}
}
private static class QueryWrapper<H> {
private final SearchQuery<H> query;
private QueryWrapper(SearchQuery<H> query) {
this.query = query;
}
public SearchResult<H> extendedFetch() {
return query.fetchAll();
}
}
private static class SupportedQueryExtension<H> implements SearchQueryExtension<QueryWrapper<H>, H> {
@Override
public Optional<QueryWrapper<H>> extendOptional(SearchQuery<H> original,
LoadingContext<?, ?> loadingContext) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( loadingContext ).isNotNull().isInstanceOf( StubLoadingContext.class );
return Optional.of( new QueryWrapper<>( original ) );
}
}
private static class UnSupportedQueryExtension<H> implements SearchQueryExtension<QueryWrapper<H>, H> {
@Override
public Optional<QueryWrapper<H>> extendOptional(SearchQuery<H> original,
LoadingContext<?, ?> loadingContext) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( loadingContext ).isNotNull().isInstanceOf( StubLoadingContext.class );
return Optional.empty();
}
}
private static class SupportedQueryDslExtension<R, E, LOS> implements
SearchQueryDslExtension<MyExtendedDslContext<R>, R, E, LOS> {
@Override
public Optional<MyExtendedDslContext<R>> extendOptional(SearchQuerySelectStep<?, R, E, LOS, ?, ?> original,
IndexScope<?> indexScope, BackendSessionContext sessionContext,
LoadingContextBuilder<R, E, LOS> loadingContextBuilder) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( indexScope ).isNotNull();
Assertions.assertThat( sessionContext ).isNotNull();
Assertions.assertThat( loadingContextBuilder ).isNotNull();
return Optional.of( new MyExtendedDslContext<R>( original.selectEntityReference() ) );
}
}
private static class UnSupportedQueryDslExtension<R, E, LOS> implements
SearchQueryDslExtension<MyExtendedDslContext<R>, R, E, LOS> {
@Override
public Optional<MyExtendedDslContext<R>> extendOptional(SearchQuerySelectStep<?, R, E, LOS, ?, ?> original,
IndexScope<?> indexScope, BackendSessionContext sessionContext,
LoadingContextBuilder<R, E, LOS> loadingContextBuilder) {
Assertions.assertThat( original ).isNotNull();
Assertions.assertThat( indexScope ).isNotNull();
Assertions.assertThat( sessionContext ).isNotNull();
Assertions.assertThat( loadingContextBuilder ).isNotNull();
return Optional.empty();
}
}
private static class MyExtendedDslContext<T> {
private final SearchQueryWhereStep<?, T, ?> delegate;
MyExtendedDslContext(SearchQueryWhereStep<?, T, ?> delegate) {
this.delegate = delegate;
}
public SearchQuery<T> extendedFeature(String fieldName, String value1, String value2) {
return delegate.where( f -> f.bool()
.should( f.match().field( fieldName ).matching( value1 ) )
.should( f.match().field( fieldName ).matching( value2 ) )
)
.sort( f -> f.field( fieldName ) )
.toQuery();
}
}
}