-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Fix put mappings java API documentation #31955
Changes from 1 commit
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 |
---|---|---|
|
@@ -19,10 +19,16 @@ | |
|
||
package org.elasticsearch.client.documentation; | ||
|
||
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.metadata.MappingMetaData; | ||
import org.elasticsearch.common.collect.ImmutableOpenMap; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
/** | ||
* This class is used to generate the Java indices administration documentation. | ||
* You need to wrap your code between two tags like: | ||
|
@@ -48,16 +54,14 @@ public void testPutMappingDocumentation() throws Exception { | |
Client client = client(); | ||
|
||
// tag::index-with-mapping | ||
client.admin().indices().prepareCreate("twitter") // <1> | ||
.addMapping("\"tweet\": {\n" + // <2> | ||
" \"properties\": {\n" + | ||
" \"message\": {\n" + | ||
" \"type\": \"text\"\n" + | ||
" }\n" + | ||
" }\n" + | ||
"}") | ||
client.admin().indices().prepareCreate("twitter") // <1> | ||
.addMapping("tweet", "message", "type=text") // <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. I wasn't sure which of the three "addMapping" variants to use in the client API docs, but went with this one since I think its the simplest. Both alternatives |
||
.get(); | ||
// end::index-with-mapping | ||
GetMappingsResponse getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get(); | ||
assertEquals(1, getMappingsResponse.getMappings().size()); | ||
ImmutableOpenMap<String, MappingMetaData> indexMapping = getMappingsResponse.getMappings().get("twitter"); | ||
assertThat(indexMapping.get("tweet"), instanceOf(MappingMetaData.class)); | ||
|
||
// we need to delete in order to create a fresh new index with another type | ||
client.admin().indices().prepareDelete("twitter").get(); | ||
|
@@ -89,6 +93,11 @@ public void testPutMappingDocumentation() throws Exception { | |
"}", XContentType.JSON) | ||
.get(); | ||
// end::putMapping-request-source | ||
getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get(); | ||
assertEquals(1, getMappingsResponse.getMappings().size()); | ||
indexMapping = getMappingsResponse.getMappings().get("twitter"); | ||
assertThat(indexMapping.get("user"), instanceOf(MappingMetaData.class)); | ||
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 really need this assertion, given the one immediately following? this feels like it could be an implementation detail. 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. sure |
||
assertThat(indexMapping.get("user").getSourceAsMap().toString(), equalTo("{properties={name={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. small thing, but 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. Yes, I was thinking about this but wanted to avoid few extra lines of code here, since I found they would also make the test harder to read. I generally agree with "toString" being brittle. In this case, since most maps use AbstractMap#toString(), it should be quite stable though. So maybe okay to trade for succinctness and readability here, wdyt? 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'm good with your reasoning. 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. Actually, I found a way that avoids "toString()" and is quite short as well, please take a look at the next commit to see if you like this better. |
||
|
||
// tag::putMapping-request-source-append | ||
client.admin().indices().preparePutMapping("twitter") // <1> | ||
|
@@ -102,6 +111,11 @@ public void testPutMappingDocumentation() throws Exception { | |
"}", XContentType.JSON) | ||
.get(); | ||
// end::putMapping-request-source-append | ||
getMappingsResponse = client.admin().indices().prepareGetMappings("twitter").get(); | ||
assertEquals(1, getMappingsResponse.getMappings().size()); | ||
indexMapping = getMappingsResponse.getMappings().get("twitter"); | ||
assertThat(indexMapping.get("user"), instanceOf(MappingMetaData.class)); | ||
assertThat(indexMapping.get("user").getSourceAsMap().toString(), equalTo("{properties={name={type=text}, user_name={type=text}}}")); | ||
} | ||
|
||
} |
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.
the word "already" reads strangely here. what about "You can add mappings for a new type at index creation time".