-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add get field mappings to High Level REST API Client #31423
Changes from 6 commits
1730875
b350c33
4cb66ae
207a8d5
f095c57
ce482f1
2205664
88192e8
e38c145
a7403ed
b6265f7
a12c32e
3424790
03b0396
c991dbc
fa77332
7d61d23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,8 @@ | |
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; | ||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse; | ||
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse; | ||
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
|
@@ -86,6 +88,7 @@ | |
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -699,6 +702,117 @@ public void onFailure(Exception e) { | |
} | ||
} | ||
|
||
public void testGetFieldMapping() throws IOException, InterruptedException { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
{ | ||
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); | ||
assertTrue(createIndexResponse.isAcknowledged()); | ||
PutMappingRequest request = new PutMappingRequest("twitter"); | ||
request.type("tweet"); | ||
request.source( | ||
"{\n" + | ||
" \"properties\": {\n" + | ||
" \"message\": {\n" + | ||
" \"type\": \"text\"\n" + | ||
" },\n" + | ||
" \"timestamp\": {\n" + | ||
" \"type\": \"date\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}", // <1> | ||
XContentType.JSON); | ||
PutMappingResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); | ||
assertTrue(putMappingResponse.isAcknowledged()); | ||
} | ||
|
||
// tag::get-field-mapping-request | ||
GetFieldMappingsRequest request = new GetFieldMappingsRequest(); // <1> | ||
request.indices("twitter"); // <2> | ||
request.types("tweet"); // <3> | ||
request.fields("message", "timestamp"); // <4> | ||
// end::get-field-mapping-request | ||
|
||
// tag::get-field-mapping-request-indicesOptions | ||
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1> | ||
// end::get-field-mapping-request-indicesOptions | ||
|
||
// tag::get-field-mapping-request-local | ||
request.local(true); // <1> | ||
// end::get-field-mapping-request-local | ||
|
||
{ | ||
|
||
// tag::get-field-mapping-execute | ||
GetFieldMappingsResponse response = | ||
client.indices().getFieldMapping(request, RequestOptions.DEFAULT); | ||
// end::get-field-mapping-execute | ||
|
||
// tag::get-field-mapping-response | ||
final Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings = | ||
response.mappings();// <1> | ||
final Map<String, GetFieldMappingsResponse.FieldMappingMetaData> typeMappings = | ||
mappings.get("twitter").get("tweet"); // <2> | ||
final GetFieldMappingsResponse.FieldMappingMetaData metaData = | ||
typeMappings.get("message");// <3> | ||
|
||
final String fullName = metaData.fullName();// <4> | ||
final Map<String, Object> source = metaData.sourceAsMap(); // <5> | ||
|
||
// end::get-field-mapping-response | ||
|
||
assertThat(fullName, equalTo("message")); | ||
assertThat(source, equalTo(Collections.singletonMap("message", Collections.singletonMap("type", "text")))); | ||
} | ||
|
||
{ | ||
// tag::get-field-mapping-execute-listener | ||
ActionListener<GetFieldMappingsResponse> listener = | ||
new ActionListener<GetFieldMappingsResponse>() { | ||
@Override | ||
public void onResponse(GetFieldMappingsResponse putMappingResponse) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::get-field-mapping-execute-listener | ||
|
||
// Replace the empty listener by a blocking listener in test | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
final ActionListener<GetFieldMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch); | ||
listener = ActionListener.wrap(r -> { | ||
final Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings = | ||
r.mappings(); | ||
final Map<String, GetFieldMappingsResponse.FieldMappingMetaData> typeMappings = | ||
mappings.get("twitter").get("tweet"); | ||
final GetFieldMappingsResponse.FieldMappingMetaData metaData1 = typeMappings.get("message"); | ||
|
||
final String fullName = metaData1.fullName(); | ||
final Map<String, Object> source = metaData1.sourceAsMap(); | ||
|
||
assertThat(fullName, equalTo("message")); | ||
assertThat(source, equalTo(Collections.singletonMap("message", Collections.singletonMap("type", "text")))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need these assertions here? given that this test is added to test the docs snippets, I would avoid testing the functionality itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is still here? |
||
latchListener.onResponse(r); | ||
}, e -> { | ||
latchListener.onFailure(e); | ||
fail("should not fail"); | ||
}); | ||
|
||
// tag::get-field-mapping-execute-async | ||
client.indices().getFieldMappingAsync(request, RequestOptions.DEFAULT, listener); // <1> | ||
// end::get-field-mapping-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
public void testOpenIndex() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use setRandomLocal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, you can't -
setRandomLocal
requiresMasterNodeReadRequest
whileGetFieldMappingsRequest
does not extend itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right we could make the comment accept a consumer instead then, we did it in other places, up to you on whether it's worthwhile or not.