-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
HLRest: Add get index templates API #31161
Changes from 2 commits
b2e11f7
9587596
a6119c3
a2dbe0a
7aa8107
caea845
16d9e27
7117a3d
c4a2bb9
b821c39
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 |
---|---|---|
|
@@ -54,6 +54,8 @@ | |
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse; | ||
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; | ||
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse; | ||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; | ||
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; | ||
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; | ||
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; | ||
|
||
|
@@ -1059,4 +1061,28 @@ public void putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequest, Re | |
restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, RequestConverters::putTemplate, options, | ||
PutIndexTemplateResponse::fromXContent, listener, emptySet()); | ||
} | ||
|
||
/** | ||
* Gets index templates using the Index Templates API | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API | ||
* on elastic.co</a> | ||
*/ | ||
public GetIndexTemplatesResponse getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest, | ||
RequestOptions options) throws IOException { | ||
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest, RequestConverters::getTemplates, | ||
options, GetIndexTemplatesResponse::fromXContent, emptySet()); | ||
} | ||
|
||
/** | ||
* Asynchronously gets index templates using the Index Templates API | ||
* <p> | ||
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API | ||
* on elastic.co</a> | ||
*/ | ||
public void getTemplatesAsync(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options, | ||
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. can you improve the javadocs like we have it in the other methods? 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 pushed 7117a3d |
||
ActionListener<GetIndexTemplatesResponse> listener) { | ||
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest, RequestConverters::getTemplates, | ||
options, GetIndexTemplatesResponse::fromXContent, listener, emptySet()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
[[java-rest-high-get-templates]] | ||
=== Get Templates API | ||
|
||
The Get Templates API allows to retrieve a list of index templates by name. | ||
|
||
[[java-rest-high-get-templates-request]] | ||
==== Get Index Templates Request | ||
|
||
A `GetIndexTemplatesRequest` specifies one or several names of the getting index templates. | ||
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. maybe better "of the index templates to get" |
||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-request] | ||
-------------------------------------------------- | ||
<1> A single index template name | ||
<2> Multiple index templates' names | ||
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 would also just use singular "template" here |
||
<3> An index template's name using wildcard | ||
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. same here |
||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-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-get-templates-sync]] | ||
==== Synchronous Execution | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-execute] | ||
-------------------------------------------------- | ||
|
||
[[java-rest-high-get-templates-async]] | ||
==== Asynchronous Execution | ||
|
||
The asynchronous execution of a get index templates request requires a `GetTemplatesRequest` | ||
instance and an `ActionListener` instance to be passed to the asynchronous | ||
method: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-execute-async] | ||
-------------------------------------------------- | ||
<1> The `GetTemplatesRequest` to execute and the `ActionListener` to use when | ||
the execution completes | ||
|
||
The asynchronous method does not block and returns immediately. Once it is | ||
completed the `ActionListener` is called back using the `onResponse` method | ||
if the execution successfully completed or using the `onFailure` method if | ||
it failed. | ||
|
||
A typical listener for `GetTemplatesResponse` looks like: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-execute-listener] | ||
-------------------------------------------------- | ||
<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-get-templates-response]] | ||
==== Get Templates Response | ||
|
||
The returned `GetTemplatesResponse` consists a list of matching index templates. | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[get-templates-response] | ||
-------------------------------------------------- | ||
<1> A list of matching index templates | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,11 @@ | |
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.singletonMap; | ||
|
@@ -37,6 +39,7 @@ public class GetIndexTemplatesResponse extends ActionResponse implements ToXCont | |
private List<IndexTemplateMetaData> indexTemplates; | ||
|
||
GetIndexTemplatesResponse() { | ||
indexTemplates = new ArrayList<>(); | ||
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. Does this enable us to make the indexTemplates list final? |
||
} | ||
|
||
GetIndexTemplatesResponse(List<IndexTemplateMetaData> indexTemplates) { | ||
|
@@ -76,4 +79,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public static GetIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException { | ||
final List<IndexTemplateMetaData> templates = new ArrayList<>(); | ||
for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
final IndexTemplateMetaData templateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()); | ||
templates.add(templateMetaData); | ||
} | ||
} | ||
templates.sort(Comparator.comparing(IndexTemplateMetaData::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. Why is sorting necessary here? From the way the templates are rendered in toXContent it looks to me we could just read them back the same way? |
||
return new GetIndexTemplatesResponse(templates); | ||
} | ||
} |
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.
nit: can we call it getTemplate? That's what our SPEC have, without the final 's'
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 pushed 7117a3d