/
ElasticsearchGetClientIT.java
59 lines (47 loc) · 1.96 KB
/
ElasticsearchGetClientIT.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
/*
* 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.documentation.backend.elasticsearch.client;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.search.backend.elasticsearch.ElasticsearchBackend;
import org.hibernate.search.documentation.testsupport.BackendConfigurations;
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper;
import org.hibernate.search.engine.backend.Backend;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.mapping.SearchMapping;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
public class ElasticsearchGetClientIT {
@Rule
public DocumentationSetupHelper setupHelper =
DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() );
private EntityManagerFactory entityManagerFactory;
@Before
public void setup() {
entityManagerFactory = setupHelper.start().setup( Book.class );
}
@Test
public void client() throws IOException {
//tag::client[]
SearchMapping mapping = /* ... */ // <1>
//end::client[]
Search.mapping( entityManagerFactory );
//tag::client[]
Backend backend = mapping.backend(); // <2>
ElasticsearchBackend elasticsearchBackend = backend.unwrap( ElasticsearchBackend.class ); // <3>
RestClient client = elasticsearchBackend.client( RestClient.class ); // <4>
//end::client[]
Response response = client.performRequest( new Request( "GET", "/" ) );
assertThat( response ).isNotNull();
assertThat( response.getStatusLine().getStatusCode() ).isEqualTo( 200 );
}
}