-
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
Added Put Mapping API to high-level Rest client (#27205) #27869
Changes from 7 commits
232b219
c0385c1
658eb7a
7cd96c6
6a00310
b7a110e
574d2fa
9a7c924
ca458e5
b9ec26b
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; | ||
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
import org.elasticsearch.action.bulk.BulkRequest; | ||
import org.elasticsearch.action.delete.DeleteRequest; | ||
|
@@ -179,6 +180,21 @@ static Request createIndex(CreateIndexRequest createIndexRequest) throws IOExcep | |
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity); | ||
} | ||
|
||
static Request putMapping(PutMappingRequest putMappingRequest) throws IOException { | ||
// The concreteIndex is an internal concept, not applicable to requests made over the REST API. | ||
assert (putMappingRequest.getConcreteIndex() == null); | ||
|
||
String endpoint = endpoint(putMappingRequest.indices(), "_mapping", putMappingRequest.type()); | ||
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 want to support 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. no that's an internal concept leaked to our external API. Maybe check in |
||
|
||
Params parameters = Params.builder(); | ||
parameters.withTimeout(putMappingRequest.timeout()); | ||
parameters.withMasterTimeout(putMappingRequest.masterNodeTimeout()); | ||
parameters.withUpdateAllTypes(putMappingRequest.updateAllTypes()); | ||
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 parameter has been removed from master, I think that you may see that once you merge master in. I need to remember to add it back though when backporting to 6.x. |
||
|
||
HttpEntity entity = createEntity(putMappingRequest, REQUEST_BODY_CONTENT_TYPE); | ||
return new Request(HttpPut.METHOD_NAME, endpoint, parameters.getParams(), entity); | ||
} | ||
|
||
static Request info() { | ||
return new Request(HttpGet.METHOD_NAME, "/", Collections.emptyMap(), null); | ||
} | ||
|
@@ -455,6 +471,10 @@ static String endpoint(String[] indices, String[] types, String endpoint) { | |
return endpoint(String.join(",", indices), String.join(",", types), endpoint); | ||
} | ||
|
||
static String endpoint(String[] indices, String endpoint, String type) { | ||
return endpoint(String.join(",", indices), endpoint, type); | ||
} | ||
|
||
/** | ||
* Utility method to build request's endpoint. | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
|
@@ -108,6 +110,38 @@ public void testCreateIndex() throws IOException { | |
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void testPutMapping() throws IOException { | ||
{ | ||
// Add mappings to index | ||
String indexName = "mapping_index"; | ||
createIndex(indexName); | ||
|
||
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName); | ||
|
||
putMappingRequest.type("type_name"); | ||
|
||
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. nit: can you remove this empty line and the one above please? |
||
XContentBuilder mappingBuilder = JsonXContent.contentBuilder(); | ||
mappingBuilder.startObject().startObject("properties").startObject("field"); | ||
mappingBuilder.field("type", "text"); | ||
mappingBuilder.endObject().endObject().endObject(); | ||
putMappingRequest.source(mappingBuilder); | ||
|
||
PutMappingResponse putMappingResponse = | ||
execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync); | ||
assertTrue(putMappingResponse.isAcknowledged()); | ||
|
||
Map<String, Object> indexMetaData = getIndexMetadata(indexName); | ||
|
||
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. nit: can you remove this empty line? |
||
Map<String, Object> mappingsData = (Map) indexMetaData.get("mappings"); | ||
Map<String, Object> typeData = (Map) mappingsData.get("type_name"); | ||
Map<String, Object> properties = (Map) typeData.get("properties"); | ||
Map<String, Object> field = (Map) properties.get("field"); | ||
|
||
assertEquals("text", field.get("type")); | ||
} | ||
} | ||
|
||
public void testDeleteIndex() throws IOException { | ||
{ | ||
// Delete index if exists | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; | ||
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; | ||
import org.elasticsearch.action.support.ActiveShardCount; | ||
|
@@ -157,15 +159,15 @@ public void testCreateIndex() throws IOException { | |
|
||
// tag::create-index-request-mappings | ||
request.mapping("tweet", // <1> | ||
" {\n" + | ||
" \"tweet\": {\n" + | ||
" \"properties\": {\n" + | ||
" \"message\": {\n" + | ||
" \"type\": \"text\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }", // <2> | ||
"{\n" + | ||
" \"tweet\": {\n" + | ||
" \"properties\": {\n" + | ||
" \"message\": {\n" + | ||
" \"type\": \"text\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}", // <2> | ||
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. was this required? Maybe you can revert this small part? 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. The IDE indented the lines; will revert. Would you like me to add the two extra spaces per line within the string back in? They seemed superfluous. |
||
XContentType.JSON); | ||
// end::create-index-request-mappings | ||
|
||
|
@@ -228,6 +230,86 @@ public void onFailure(Exception e) { | |
} | ||
} | ||
|
||
public void testPutMapping() throws IOException { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
{ | ||
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter")); | ||
assertTrue(createIndexResponse.isAcknowledged()); | ||
} | ||
|
||
{ | ||
// tag::put-mapping-request | ||
PutMappingRequest request = new PutMappingRequest("twitter"); // <1> | ||
request.type("tweet"); // <2> | ||
// end::put-mapping-request | ||
|
||
// tag::put-mapping-request-source | ||
request.source( | ||
"{\n" + | ||
" \"tweet\": {\n" + | ||
" \"properties\": {\n" + | ||
" \"message\": {\n" + | ||
" \"type\": \"text\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}", // <1> | ||
XContentType.JSON); | ||
// end::put-mapping-request-source | ||
|
||
// tag::put-mapping-request-timeout | ||
request.timeout(TimeValue.timeValueMinutes(2)); // <1> | ||
request.timeout("2m"); // <2> | ||
// end::put-mapping-request-timeout | ||
// tag::put-mapping-request-masterTimeout | ||
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> | ||
request.masterNodeTimeout("1m"); // <2> | ||
// end::put-mapping-request-masterTimeout | ||
|
||
// tag::put-mapping-execute | ||
PutMappingResponse putMappingResponse = client.indices().putMapping(request); | ||
// end::put-mapping-execute | ||
|
||
// tag::put-mapping-response | ||
boolean acknowledged = putMappingResponse.isAcknowledged(); // <1> | ||
// end::put-mapping-response | ||
assertTrue(acknowledged); | ||
} | ||
} | ||
|
||
public void testPutMappingAsync() throws Exception { | ||
final RestHighLevelClient client = highLevelClient(); | ||
|
||
{ | ||
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter")); | ||
assertTrue(createIndexResponse.isAcknowledged()); | ||
} | ||
|
||
{ | ||
PutMappingRequest request = new PutMappingRequest("twitter").type("tweet"); | ||
// tag::put-mapping-execute-async | ||
client.indices().putMappingAsync(request, new ActionListener<PutMappingResponse>() { | ||
@Override | ||
public void onResponse(PutMappingResponse putMappingResponse) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}); | ||
// end::put-mapping-execute-async | ||
|
||
assertBusy(() -> { | ||
// TODO Use Indices Exist API instead once it exists | ||
Response response = client.getLowLevelClient().performRequest("HEAD", "twitter"); | ||
assertTrue(RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode()); | ||
}); | ||
} | ||
} | ||
|
||
public void testOpenIndex() throws IOException { | ||
RestHighLevelClient client = highLevelClient(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
[[java-rest-high-put-mapping]] | ||
=== Put Mapping API | ||
|
||
[[java-rest-high-put-mapping-request]] | ||
==== Put Mapping Request | ||
|
||
A `PutMappingRequest` requires an `index` argument, and a type: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-request] | ||
-------------------------------------------------- | ||
<1> The index to add the mapping to | ||
<2> The type to create (or update) | ||
|
||
==== Mapping source | ||
A description of the fields to create on the mapping; if not defined, the mapping will default to empty. | ||
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. should we list this under the optional arguments? After all it is optional? 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. Hmm. Technically it's optional; but a PutMapping request without a source just adds a new type; which, in the ES 6.x+ world, is basically meaningless. |
||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-request-source] | ||
-------------------------------------------------- | ||
<1> The mapping source | ||
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. should we cover the different way to provide the source like we did in other docs pages (e.g. search API)? I see that we didn't do it for the create index API, but maybe we should have. Shall we have a common page on how to provide a source and link to it? What do you think? Maybe we could also do it as a followup so this PR doesn't get too big. 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. That does make sense; but it does rather seem like there could be some more user-friendly mapping-building tools, abstracting out the boilerplate. E.g. the ones in Elastic4s:
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. But anyway; as things are now, I think a separate doc makes sense, so as not to clutter the Create Index and Put Mapping pages too much? 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 agree that we should do better, but for now we are only porting what the transport client supports to the new client. I think that having a separate page would be good so we don't repeat the same content in different pages, I assume that the content would be the same, it can probably already be extracted from the search docs page. 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. Cool; I am tempted to suggest doing it as a follow-up PR, then, since I'm not sure I've got the time to do it just now. If you think it's a blocker for this one, though, then that's fair enough. 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, fine as a follow-up PR 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. @catalin-ursachi let me know if you want to take this, otherwise I can take care of it. 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. By all means feel free to take it; otherwise I'll pick it up when I next set aside some time for OS work, but I can't promise when that is. 🙂 |
||
|
||
==== Optional arguments | ||
The following arguments can optionally be provided: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-request-timeout] | ||
-------------------------------------------------- | ||
<1> Timeout to wait for the all the nodes to acknowledge the index creation as a `TimeValue` | ||
<2> Timeout to wait for the all the nodes to acknowledge the index creation as a `String` | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-request-masterTimeout] | ||
-------------------------------------------------- | ||
<1> Timeout to connect to the master node as a `TimeValue` | ||
<2> Timeout to connect to the master node as a `String` | ||
|
||
[[java-rest-high-put-mapping-sync]] | ||
==== Synchronous Execution | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-execute] | ||
-------------------------------------------------- | ||
|
||
[[java-rest-high-put-mapping-async]] | ||
==== Asynchronous Execution | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-execute-async] | ||
-------------------------------------------------- | ||
<1> Called when the execution is successfully completed. The response is | ||
provided as an argument | ||
<2> Called in case of failure. The raised exception is provided as an argument | ||
|
||
[[java-rest-high-put-mapping-response]] | ||
==== Put Mapping Response | ||
|
||
The returned `PutMappingResponse` allows to retrieve information about the executed | ||
operation as follows: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-mapping-response] | ||
-------------------------------------------------- | ||
<1> Indicates whether all of the nodes have acknowledged the request |
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.
given that a request is provided by users, this should rather be an exception that gets thrown. Also this assertion may make the user's application crash. Throw
IllegalArgumentException
instead?