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);
}
}
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
}
}
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));
}
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));
}
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());
}
Aggregations