Search in sources :

Example 6 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testPutMapping.

public void testPutMapping() throws IOException {
    RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    {
        // tag::put-mapping-request
        // <1>
        PutMappingRequest request = new PutMappingRequest("twitter");
        // end::put-mapping-request
        {
            // tag::put-mapping-request-source
            request.source("{\n" + "  \"properties\": {\n" + "    \"message\": {\n" + "      \"type\": \"text\"\n" + "    }\n" + "  }\n" + // <1>
            "}", XContentType.JSON);
            // end::put-mapping-request-source
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        {
            // tag::put-mapping-map
            Map<String, Object> jsonMap = new HashMap<>();
            Map<String, Object> message = new HashMap<>();
            message.put("type", "text");
            Map<String, Object> properties = new HashMap<>();
            properties.put("message", message);
            jsonMap.put("properties", properties);
            // <1>
            request.source(jsonMap);
            // end::put-mapping-map
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        {
            // tag::put-mapping-xcontent
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.startObject("properties");
                {
                    builder.startObject("message");
                    {
                        builder.field("type", "text");
                    }
                    builder.endObject();
                }
                builder.endObject();
            }
            builder.endObject();
            // <1>
            request.source(builder);
            // end::put-mapping-xcontent
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
            assertTrue(putMappingResponse.isAcknowledged());
        }
        // tag::put-mapping-request-timeout
        // <1>
        request.setTimeout(TimeValue.timeValueMinutes(2));
        // end::put-mapping-request-timeout
        // tag::put-mapping-request-masterTimeout
        // <1>
        request.setMasterTimeout(TimeValue.timeValueMinutes(1));
        // end::put-mapping-request-masterTimeout
        // tag::put-mapping-execute
        AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
        // end::put-mapping-execute
        // tag::put-mapping-response
        // <1>
        boolean acknowledged = putMappingResponse.isAcknowledged();
        // end::put-mapping-response
        assertTrue(acknowledged);
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 7 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testAnalyze.

public void testAnalyze() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::analyze-builtin-request
        AnalyzeRequest request = // <1>
        AnalyzeRequest.withGlobalAnalyzer(// <1>
        "english", "Some text to analyze", // <2>
        "Some more text to analyze");
    // end::analyze-builtin-request
    }
    {
        // tag::analyze-custom-request
        Map<String, Object> stopFilter = new HashMap<>();
        stopFilter.put("type", "stop");
        // <1>
        stopFilter.put("stopwords", new String[] { "to" });
        AnalyzeRequest request = // <2>
        AnalyzeRequest.buildCustomAnalyzer("standard").addCharFilter(// <3>
        "html_strip").addTokenFilter(// <4>
        "lowercase").addTokenFilter(// <5>
        stopFilter).build("<b>Some text to analyze</b>");
    // end::analyze-custom-request
    }
    {
        // tag::analyze-custom-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.buildCustomNormalizer().addTokenFilter("lowercase").build("<b>BaR</b>");
        // end::analyze-custom-normalizer-request
        // tag::analyze-request-explain
        // <1>
        request.explain(true);
        // <2>
        request.attributes("keyword", "type");
        // end::analyze-request-explain
        // tag::analyze-execute
        AnalyzeResponse response = client.indices().analyze(request, RequestOptions.DEFAULT);
        // end::analyze-execute
        // tag::analyze-response-tokens
        // <1>
        List<AnalyzeResponse.AnalyzeToken> tokens = response.getTokens();
        // end::analyze-response-tokens
        // tag::analyze-response-detail
        // <1>
        DetailAnalyzeResponse detail = response.detail();
        // end::analyze-response-detail
        assertNull(tokens);
        assertNotNull(detail.tokenizer());
    }
    CreateIndexRequest req = new CreateIndexRequest("my_index");
    CreateIndexResponse resp = client.indices().create(req, RequestOptions.DEFAULT);
    assertTrue(resp.isAcknowledged());
    PutMappingRequest pmReq = new PutMappingRequest("my_index").source(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("my_field").field("type", "text").field("analyzer", "english").endObject().endObject().endObject());
    AcknowledgedResponse pmResp = client.indices().putMapping(pmReq, RequestOptions.DEFAULT);
    assertTrue(pmResp.isAcknowledged());
    {
        // tag::analyze-index-request
        AnalyzeRequest request = AnalyzeRequest.withIndexAnalyzer(// <1>
        "my_index", // <2>
        "my_analyzer", "some text to analyze");
        // end::analyze-index-request
        // tag::analyze-execute-listener
        ActionListener<AnalyzeResponse> listener = new ActionListener<AnalyzeResponse>() {

            @Override
            public void onResponse(AnalyzeResponse analyzeTokens) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::analyze-execute-listener
        // use a built-in analyzer in the test
        request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
        // Use a blocking listener in the test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::analyze-execute-async
        // <1>
        client.indices().analyzeAsync(request, RequestOptions.DEFAULT, listener);
        // end::analyze-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
    {
        // tag::analyze-index-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.withNormalizer(// <1>
        "my_index", // <2>
        "my_normalizer", "some text to analyze");
    // end::analyze-index-normalizer-request
    }
    {
        // tag::analyze-field-request
        AnalyzeRequest request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
    // end::analyze-field-request
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) AnalyzeResponse(org.opensearch.client.indices.AnalyzeResponse) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) List(java.util.List) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testPutMapping.

public void testPutMapping() throws IOException {
    String indexName = "mapping_index";
    createIndex(indexName, Settings.EMPTY);
    PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
    XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
    mappingBuilder.startObject().startObject("properties").startObject("field");
    mappingBuilder.field("type", "text");
    mappingBuilder.endObject().endObject().endObject();
    putMappingRequest.source(mappingBuilder);
    AcknowledgedResponse putMappingResponse = execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
    assertTrue(putMappingResponse.isAcknowledged());
    Map<String, Object> getIndexResponse = getAsMap(indexName);
    assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 9 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testGetMapping.

public void testGetMapping() throws IOException {
    String indexName = "test";
    createIndex(indexName, Settings.EMPTY);
    PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
    XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
    mappingBuilder.startObject().startObject("properties").startObject("field");
    mappingBuilder.field("type", "text");
    mappingBuilder.endObject().endObject().endObject();
    putMappingRequest.source(mappingBuilder);
    AcknowledgedResponse putMappingResponse = execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync);
    assertTrue(putMappingResponse.isAcknowledged());
    Map<String, Object> getIndexResponse = getAsMap(indexName);
    assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
    GetMappingsRequest request = new GetMappingsRequest().indices(indexName);
    GetMappingsResponse getMappingsResponse = execute(request, highLevelClient().indices()::getMapping, highLevelClient().indices()::getMappingAsync);
    Map<String, Object> mappings = getMappingsResponse.mappings().get(indexName).sourceAsMap();
    Map<String, String> type = new HashMap<>();
    type.put("type", "text");
    Map<String, Object> field = new HashMap<>();
    field.put("field", type);
    Map<String, Object> expected = new HashMap<>();
    expected.put("properties", field);
    assertThat(mappings, equalTo(expected));
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.opensearch.client.indices.GetMappingsResponse)

Example 10 with PutMappingRequest

use of org.opensearch.client.indices.PutMappingRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testPutMapping.

public void testPutMapping() throws IOException {
    String[] indices = RequestConvertersTests.randomIndicesNames(0, 5);
    PutMappingRequest putMappingRequest = new PutMappingRequest(indices);
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomTimeout(putMappingRequest, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    RequestConvertersTests.setRandomMasterTimeout(putMappingRequest, expectedParams);
    RequestConvertersTests.setRandomIndicesOptions(putMappingRequest::indicesOptions, putMappingRequest::indicesOptions, expectedParams);
    Request request = IndicesRequestConverters.putMapping(putMappingRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String index = String.join(",", indices);
    if (Strings.hasLength(index)) {
        endpoint.add(index);
    }
    endpoint.add("_mapping");
    Assert.assertEquals(endpoint.toString(), request.getEndpoint());
    Assert.assertEquals(expectedParams, request.getParameters());
    Assert.assertEquals(HttpPut.METHOD_NAME, request.getMethod());
    RequestConvertersTests.assertToXContentBody(putMappingRequest, request.getEntity());
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) HashMap(java.util.HashMap) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) StringJoiner(java.util.StringJoiner)

Aggregations

PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)10 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)7 HashMap (java.util.HashMap)6 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)6 IOException (java.io.IOException)4 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)4 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)4 GetFieldMappingsRequest (org.opensearch.client.indices.GetFieldMappingsRequest)4 GetMappingsRequest (org.opensearch.client.indices.GetMappingsRequest)4 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)4 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 OpenSearchException (org.opensearch.OpenSearchException)3 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)3 ActionListener (org.opensearch.action.ActionListener)2 LatchedActionListener (org.opensearch.action.LatchedActionListener)2 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)2 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)2