Search in sources :

Example 16 with OpenSearchClient

use of org.opensearch.client.opensearch.OpenSearchClient in project opensearch-java by opensearch-project.

the class ApiConventionsTest method builders.

@Test(expected = TransportException.class)
public void builders() throws Exception {
    OpenSearchClient client = new OpenSearchClient(transport);
    // tag::builders
    CreateIndexResponse createResponse = client.indices().create(new CreateIndexRequest.Builder().index("my-index").aliases("foo", new Alias.Builder().isWriteIndex(true).build()).build());
// end::builders
}
Also used : Alias(org.opensearch.client.opensearch.indices.Alias) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Test(org.junit.Test)

Example 17 with OpenSearchClient

use of org.opensearch.client.opensearch.OpenSearchClient in project opensearch-java by opensearch-project.

the class RequestOptionsTest method testClientHeader.

@Test
public void testClientHeader() throws IOException {
    final RestClientTransport trsp = new RestClientTransport(restClient, new JsonbJsonpMapper());
    final OpenSearchClient client = new OpenSearchClient(trsp).withTransportOptions(trsp.options().with(b -> b.addHeader("X-Foo", "Bar").addHeader("uSer-agEnt", "MegaClient/1.2.3")));
    Properties props = getProps(client);
    assertEquals("Bar", props.getProperty("header-x-foo"));
    assertEquals("MegaClient/1.2.3", props.getProperty("header-user-agent"));
}
Also used : HttpServer(com.sun.net.httpserver.HttpServer) Properties(java.util.Properties) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) InetAddress(java.net.InetAddress) List(java.util.List) ResponseException(org.opensearch.client.ResponseException) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) Locale(java.util.Locale) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) OutputStreamWriter(java.io.OutputStreamWriter) NameValuePair(org.apache.http.NameValuePair) Assert(org.junit.Assert) HttpHost(org.apache.http.HttpHost) Before(org.junit.Before) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Properties(java.util.Properties) Test(org.junit.Test)

Example 18 with OpenSearchClient

use of org.opensearch.client.opensearch.OpenSearchClient in project opensearch-java by opensearch-project.

the class ConnectingTest method createClient.

// we don't have a running ES
@Ignore
@Test
public void createClient() throws Exception {
    // tag::create-client
    // Create the low-level client
    RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
    // Create the transport with a Jackson mapper
    OpenSearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
    // And create the API client
    OpenSearchClient client = new OpenSearchClient(transport);
    // end::create-client
    // tag::first-request
    SearchResponse<Product> search = client.search(s -> s.index("products").query(q -> q.term(t -> t.field("name").value(v -> v.stringValue("bicycle")))), Product.class);
    for (Hit<Product> hit : search.hits().hits()) {
        processProduct(hit.source());
    }
// end::first-request
}
Also used : JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) Ignore(org.junit.Ignore) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) RestClient(org.opensearch.client.RestClient) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) HttpHost(org.apache.http.HttpHost) Hit(org.opensearch.client.opensearch.core.search.Hit) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) HttpHost(org.apache.http.HttpHost) RestClient(org.opensearch.client.RestClient) JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with OpenSearchClient

use of org.opensearch.client.opensearch.OpenSearchClient in project opensearch-java by opensearch-project.

the class MigrateHlrcTest method migrate.

@Test
public void migrate() {
    // tag::migrate
    // Create the low-level client
    RestClientBuilder httpClientBuilder = RestClient.builder(new HttpHost("localhost", 9200));
    // Create the HLRC
    RestHighLevelClient hlrc = new RestHighLevelClient(httpClientBuilder);
    // Create the new Java Client with the same low level client
    OpenSearchTransport transport = new RestClientTransport(hlrc.getLowLevelClient(), new JacksonJsonpMapper());
    OpenSearchClient esClient = new OpenSearchClient(transport);
// hlrc and esClient share the same httpClient
// end::migrate
}
Also used : RestClientTransport(org.opensearch.client.transport.rest_client.RestClientTransport) HttpHost(org.apache.http.HttpHost) JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) RestClientBuilder(org.opensearch.client.RestClientBuilder) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) Test(org.junit.Test)

Aggregations

OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)19 HashMap (java.util.HashMap)11 Map (java.util.Map)9 Test (org.junit.Test)8 OpenSearchTransport (org.opensearch.client.transport.OpenSearchTransport)7 List (java.util.List)5 Assert (org.junit.Assert)5 JsonData (org.opensearch.client.json.JsonData)5 PutClusterSettingsRequest (org.opensearch.client.opensearch.cluster.PutClusterSettingsRequest)5 SearchResponse (org.opensearch.client.opensearch.core.SearchResponse)5 Alias (org.opensearch.client.opensearch.indices.Alias)5 CreateIndexResponse (org.opensearch.client.opensearch.indices.CreateIndexResponse)5 Arrays (java.util.Arrays)4 LogManager (java.util.logging.LogManager)4 Logger (java.util.logging.Logger)4 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)4 NodeStatistics (org.opensearch.client.opensearch._types.NodeStatistics)4 SortOrder (org.opensearch.client.opensearch._types.SortOrder)4 Aggregation (org.opensearch.client.opensearch._types.aggregations.Aggregation)4 Query (org.opensearch.client.opensearch._types.query_dsl.Query)4