forked from elastic/elasticsearch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search Templates: Adds API endpoint to render search templates as a r…
…esponse Closes elastic#6821
- Loading branch information
Showing
17 changed files
with
795 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
eclipse.preferences.version=1 | ||
encoding//src/main/java=UTF-8 | ||
encoding//src/main/resources=UTF-8 | ||
encoding//src/test/java=UTF-8 | ||
encoding//src/test/resources=UTF-8 | ||
encoding/<project>=UTF-8 | ||
encoding/rest-api-spec=UTF-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../org/elasticsearch/action/admin/indices/validate/template/RenderSearchTemplateAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.validate.template; | ||
|
||
import org.elasticsearch.action.Action; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
|
||
public class RenderSearchTemplateAction extends Action<RenderSearchTemplateRequest, RenderSearchTemplateResponse, RenderSearchTemplateRequestBuilder> { | ||
|
||
public static final RenderSearchTemplateAction INSTANCE = new RenderSearchTemplateAction(); | ||
public static final String NAME = "indices:admin/render/template/search"; | ||
|
||
public RenderSearchTemplateAction() { | ||
super(NAME); | ||
} | ||
|
||
@Override | ||
public RenderSearchTemplateRequestBuilder newRequestBuilder(ElasticsearchClient client) { | ||
return new RenderSearchTemplateRequestBuilder(client, this); | ||
} | ||
|
||
@Override | ||
public RenderSearchTemplateResponse newResponse() { | ||
return new RenderSearchTemplateResponse(); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...org/elasticsearch/action/admin/indices/validate/template/RenderSearchTemplateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.validate.template; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.script.Template; | ||
|
||
import java.io.IOException; | ||
|
||
public class RenderSearchTemplateRequest extends ActionRequest<RenderSearchTemplateRequest> { | ||
|
||
private Template template; | ||
|
||
public void template(Template template) { | ||
this.template = template; | ||
} | ||
|
||
public Template template() { | ||
return template; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException exception = null; | ||
if (template == null) { | ||
exception = new ActionRequestValidationException(); | ||
exception.addValidationError("template must not be null"); | ||
} | ||
return exception; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
boolean hasTemplate = template!= null; | ||
out.writeBoolean(hasTemplate); | ||
if (hasTemplate) { | ||
template.writeTo(out); | ||
} | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
if (in.readBoolean()) { | ||
template = Template.readTemplate(in); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...sticsearch/action/admin/indices/validate/template/RenderSearchTemplateRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.validate.template; | ||
|
||
import org.elasticsearch.action.ActionRequestBuilder; | ||
import org.elasticsearch.client.ElasticsearchClient; | ||
import org.elasticsearch.script.Template; | ||
|
||
public class RenderSearchTemplateRequestBuilder extends ActionRequestBuilder<RenderSearchTemplateRequest, RenderSearchTemplateResponse, RenderSearchTemplateRequestBuilder> { | ||
|
||
public RenderSearchTemplateRequestBuilder(ElasticsearchClient client, | ||
RenderSearchTemplateAction action) { | ||
super(client, action, new RenderSearchTemplateRequest()); | ||
} | ||
|
||
public RenderSearchTemplateRequestBuilder template(Template template) { | ||
request.template(template); | ||
return this; | ||
} | ||
|
||
public Template template() { | ||
return request.template(); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...rg/elasticsearch/action/admin/indices/validate/template/RenderSearchTemplateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.validate.template; | ||
|
||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import java.io.IOException; | ||
|
||
public class RenderSearchTemplateResponse extends ActionResponse implements ToXContent { | ||
|
||
private BytesReference source; | ||
|
||
public BytesReference source() { | ||
return source; | ||
} | ||
|
||
public void source(BytesReference source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
boolean hasSource = source != null; | ||
out.writeBoolean(hasSource); | ||
if (hasSource) { | ||
out.writeBytesReference(source); | ||
} | ||
} | ||
|
||
@Override | ||
public void readFrom(StreamInput in) throws IOException { | ||
super.readFrom(in); | ||
if (in.readBoolean()) { | ||
source = in.readBytesReference(); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.rawField("template_output", source); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...ticsearch/action/admin/indices/validate/template/TransportRenderSearchTemplateAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.action.admin.indices.validate.template; | ||
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.action.support.HandledTransportAction; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.concurrent.AbstractRunnable; | ||
import org.elasticsearch.script.ExecutableScript; | ||
import org.elasticsearch.script.ScriptContext; | ||
import org.elasticsearch.script.ScriptService; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
import org.elasticsearch.transport.TransportService; | ||
|
||
public class TransportRenderSearchTemplateAction extends HandledTransportAction<RenderSearchTemplateRequest, RenderSearchTemplateResponse> { | ||
|
||
private final ScriptService scriptService; | ||
|
||
@Inject | ||
protected TransportRenderSearchTemplateAction(ScriptService scriptService, Settings settings, ThreadPool threadPool, | ||
TransportService transportService, ActionFilters actionFilters) { | ||
super(settings, RenderSearchTemplateAction.NAME, threadPool, transportService, actionFilters, RenderSearchTemplateRequest.class); | ||
this.scriptService = scriptService; | ||
} | ||
|
||
@Override | ||
protected void doExecute(final RenderSearchTemplateRequest request, final ActionListener<RenderSearchTemplateResponse> listener) { | ||
threadPool.generic().execute(new AbstractRunnable() { | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
listener.onFailure(t); | ||
} | ||
|
||
@Override | ||
protected void doRun() throws Exception { | ||
ExecutableScript executable = scriptService.executable(request.template(), ScriptContext.Standard.SEARCH); | ||
BytesReference processedTemplate = (BytesReference) executable.run(); | ||
RenderSearchTemplateResponse response = new RenderSearchTemplateResponse(); | ||
response.source(processedTemplate); | ||
listener.onResponse(response); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.