/
ElasticsearchSearchQueryRequestTransformerIT.java
188 lines (160 loc) · 6.5 KB
/
ElasticsearchSearchQueryRequestTransformerIT.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
/*
* 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.elasticsearch.search.query;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.hibernate.search.util.impl.integrationtest.backend.elasticsearch.ElasticsearchIndexMetadataTestUtils.defaultReadAlias;
import org.hibernate.search.backend.elasticsearch.ElasticsearchExtension;
import org.hibernate.search.backend.elasticsearch.cfg.spi.ElasticsearchBackendSpiSettings;
import org.hibernate.search.backend.elasticsearch.client.impl.Paths;
import org.hibernate.search.backend.elasticsearch.client.spi.ElasticsearchRequest;
import org.hibernate.search.backend.elasticsearch.search.query.ElasticsearchSearchRequestTransformer;
import org.hibernate.search.backend.elasticsearch.util.spi.URLEncodedString;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.engine.search.query.SearchQuery;
import org.hibernate.search.integrationtest.backend.elasticsearch.testsupport.util.ElasticsearchClientSpy;
import org.hibernate.search.integrationtest.backend.elasticsearch.testsupport.util.ElasticsearchRequestAssertionMode;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
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.junit.rules.ExpectedException;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
/**
* Test the content of generated Elasticsearch search queries when the
* {@link ElasticsearchSearchRequestTransformer}
* is used.
*/
public class ElasticsearchSearchQueryRequestTransformerIT {
private static final String INDEX_NAME = "indexname";
private static final String SECOND_INDEX_NAME = "secondindexname";
@Rule
public SearchSetupHelper setupHelper = new SearchSetupHelper();
@Rule
public ElasticsearchClientSpy clientSpy = new ElasticsearchClientSpy();
@Rule
public ExpectedException thrown = ExpectedException.none();
private StubMappingIndexManager indexManager;
private StubMappingIndexManager secondIndexManager;
@Before
public void setup() {
setupHelper.start()
.withBackendProperty(
ElasticsearchBackendSpiSettings.CLIENT_FACTORY, clientSpy.getFactory()
)
.withIndex(
INDEX_NAME,
ctx -> new IndexMapping( ctx.getSchemaElement() ),
indexManager -> this.indexManager = indexManager
)
.withIndex(
SECOND_INDEX_NAME,
ctx -> new IndexMapping( ctx.getSchemaElement() ),
indexManager -> this.secondIndexManager = indexManager
)
.setup();
}
@Test
public void path() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query().extension( ElasticsearchExtension.get() )
.where( f -> f.matchAll() )
.requestTransformer( context -> {
assertThat( context.getPath() ).isEqualTo( "/" + defaultReadAlias( INDEX_NAME ).original + "/_search" );
String newPath = "/" + defaultReadAlias( SECOND_INDEX_NAME ).original + "/_search";
context.setPath( newPath );
// Changes should be visible immediately
assertThat( context.getPath() ).isEqualTo( newPath );
} )
.toQuery();
clientSpy.expectNext(
ElasticsearchRequest.post()
.pathComponent( defaultReadAlias( SECOND_INDEX_NAME ) )
.pathComponent( URLEncodedString.fromString( "_search" ) )
.body( new Gson().fromJson( "{}", JsonObject.class ) )
.build(),
ElasticsearchRequestAssertionMode.EXTENSIBLE
);
query.fetchAll();
}
@Test
public void queryParameters() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query().extension( ElasticsearchExtension.get() )
.where( f -> f.matchAll() )
.requestTransformer( context -> {
assertThat( context.getParametersMap() )
.doesNotContainKeys( "search_type" );
context.getParametersMap().put( "search_type", "dfs_query_then_fetch" );
// Changes should be visible immediately
assertThat( context.getParametersMap() )
.contains( entry( "search_type", "dfs_query_then_fetch" ) );
} )
.toQuery();
clientSpy.expectNext(
ElasticsearchRequest.post()
.pathComponent( defaultReadAlias( INDEX_NAME ) )
.pathComponent( Paths._SEARCH )
.body( new Gson().fromJson( "{}", JsonObject.class ) )
.param( "search_type", "dfs_query_then_fetch" )
.build(),
ElasticsearchRequestAssertionMode.EXTENSIBLE
);
query.fetchAll();
}
@Test
public void body() {
StubMappingScope scope = indexManager.createScope();
SearchQuery<DocumentReference> query = scope.query().extension( ElasticsearchExtension.get() )
.where( f -> f.matchAll() )
.requestTransformer( context -> {
assertThat( context.getBody() )
.isNotNull()
.extracting( body -> body.get( "min_score" ) ).isNull();
context.getBody().addProperty( "min_score", 0.5f );
// Changes should be visible immediately
assertThat( context.getBody() )
.isNotNull()
.extracting( body -> body.get( "min_score" ) )
.extracting( JsonElement::getAsFloat ).isEqualTo( 0.5f );
} )
.toQuery();
clientSpy.expectNext(
ElasticsearchRequest.post()
.pathComponent( defaultReadAlias( INDEX_NAME ) )
.pathComponent( Paths._SEARCH )
.body( new Gson().fromJson( "{'min_score':0.5}", JsonObject.class ) )
.build(),
ElasticsearchRequestAssertionMode.EXTENSIBLE
);
query.fetchAll();
}
@SuppressWarnings("unused")
private static class IndexMapping {
final IndexFieldReference<Integer> integer;
final IndexFieldReference<String> string;
IndexMapping(IndexSchemaElement root) {
integer = root.field(
"integer",
f -> f.asInteger().projectable( Projectable.YES )
)
.toReference();
string = root.field(
"string",
f -> f.asString().projectable( Projectable.YES )
)
.toReference();
}
}
}