-
Notifications
You must be signed in to change notification settings - Fork 25k
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 1 commit
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 |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest; | ||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; | ||
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.GetMappingsRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
|
@@ -225,6 +226,20 @@ static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOExcep | |
return request; | ||
} | ||
|
||
static Request getFieldMappings(GetFieldMappingsRequest getFieldMappingsRequest) throws IOException { | ||
String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices(); | ||
String[] types = getFieldMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.types(); | ||
String[] fields = getFieldMappingsRequest.fields() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.fields(); | ||
|
||
Request request = new Request(HttpGet.METHOD_NAME, endpoint(indices, "_mapping", types, "field", fields)); | ||
|
||
Params parameters = new Params(request); | ||
parameters.withIndicesOptions(getFieldMappingsRequest.indicesOptions()); | ||
parameters.withIncludeDefaults(getFieldMappingsRequest.includeDefaults()); | ||
parameters.withLocal(getFieldMappingsRequest.local()); | ||
return request; | ||
} | ||
|
||
static Request refresh(RefreshRequest refreshRequest) { | ||
String[] indices = refreshRequest.indices() == null ? Strings.EMPTY_ARRAY : refreshRequest.indices(); | ||
Request request = new Request(HttpPost.METHOD_NAME, endpoint(indices, "_refresh")); | ||
|
@@ -908,6 +923,13 @@ static String endpoint(String[] indices, String endpoint, String[] suffixes) { | |
.addCommaSeparatedPathParts(suffixes).build(); | ||
} | ||
|
||
static String endpoint(String[] indices, String endpoint, String[] suffixes, String extraEndpoint, String[] extraSuffixes) { | ||
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. I think I'd make just call the 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. makes sense. thanks |
||
return new EndpointBuilder().addCommaSeparatedPathParts(indices) | ||
.addPathPartAsIs(endpoint).addCommaSeparatedPathParts(suffixes) | ||
.addPathPartAsIs(extraEndpoint).addCommaSeparatedPathParts(extraSuffixes) | ||
.build(); | ||
} | ||
|
||
static String endpoint(String[] indices, String endpoint, String type) { | ||
return new EndpointBuilder().addCommaSeparatedPathParts(indices).addPathPartAsIs(endpoint).addPathPart(type).build(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest; | ||
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; | ||
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.GetMappingsRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
|
@@ -452,6 +453,53 @@ public void testGetMapping() throws IOException { | |
assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod())); | ||
} | ||
|
||
public void testGetFieldMapping() throws IOException { | ||
GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest(); | ||
|
||
String[] indices = Strings.EMPTY_ARRAY; | ||
if (randomBoolean()) { | ||
indices = randomIndicesNames(0, 5); | ||
getFieldMappingsRequest.indices(indices); | ||
} else if (randomBoolean()) { | ||
getFieldMappingsRequest.indices((String[]) null); | ||
} | ||
|
||
String type = null; | ||
if (randomBoolean()) { | ||
type = randomAlphaOfLengthBetween(3, 10); | ||
getFieldMappingsRequest.types(type); | ||
} else if (randomBoolean()) { | ||
getFieldMappingsRequest.types((String[]) null); | ||
} | ||
|
||
String[] fields = new String[randomIntBetween(1, 5)]; | ||
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. You support leaving these 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. agreed. |
||
for(int i = 0; i < fields.length; i++) { | ||
fields[i] = randomAlphaOfLengthBetween(3, 10); | ||
} | ||
getFieldMappingsRequest.fields(fields); | ||
|
||
Map<String, String> expectedParams = new HashMap<>(); | ||
|
||
setRandomIndicesOptions(getFieldMappingsRequest::indicesOptions, getFieldMappingsRequest::indicesOptions, expectedParams); | ||
|
||
Request request = RequestConverters.getFieldMappings(getFieldMappingsRequest); | ||
StringJoiner endpoint = new StringJoiner("/", "/", ""); | ||
String index = String.join(",", indices); | ||
if (Strings.hasLength(index)) { | ||
endpoint.add(index); | ||
} | ||
endpoint.add("_mapping"); | ||
if (type != null) { | ||
endpoint.add(type); | ||
} | ||
endpoint.add("field"); | ||
endpoint.add(String.join(",", fields)); | ||
assertThat(endpoint.toString(), equalTo(request.getEndpoint())); | ||
|
||
assertThat(expectedParams, equalTo(request.getParameters())); | ||
assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod())); | ||
} | ||
|
||
public void testDeleteIndex() { | ||
String[] indices = randomIndicesNames(0, 5); | ||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indices); | ||
|
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,113 @@ 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-execute | ||
GetFieldMappingsResponse response = | ||
client.indices().getFieldMappings(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().getFieldMappingsAsync(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.
it should be singular getFieldMapping. that's what we have in the SPEC
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.
@javanna there is inconsistency with a class name
GetFieldMappingsResponse
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.
there are plenty of these, for the method names we follow the SPEC regardless of class names. If we could we would change the class names but we are trying to make it easy for people to migrate off of the transport client so we don't do that.
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.
i see the difficulties - sounds ok to me