mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-22 21:05:23 +00:00
Indexed scripts: make sure headers are handed over to internal requests and streamline versioning support
The get, put and delete indexed script apis map to get, index and delete api and internally create those corresponding requests. We need to make sure that the original headers are handed over to the new request by passing the original request in the constructor when creating the new one. Also streamlined the support for version and version_type in the REST layer since the parameters were not consistently parsed and set to the internal java API requests. Modified the REST delete template and delete script actions to make use of a client instead of using the `ScriptService` directly. Closes #7569
This commit is contained in:
parent
221eafab59
commit
a857798e1c
@ -16,6 +16,17 @@
|
|||||||
"description" : "Script language",
|
"description" : "Script language",
|
||||||
"required" : true
|
"required" : true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"params" : {
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"body": null
|
"body": null
|
||||||
|
@ -12,6 +12,17 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"params" : {
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
|
},
|
||||||
"body": null
|
"body": null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,17 @@
|
|||||||
"description" : "Script language",
|
"description" : "Script language",
|
||||||
"required" : true
|
"required" : true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"params" : {
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"body": null
|
"body": null
|
||||||
|
@ -11,11 +11,19 @@
|
|||||||
"description" : "Template ID",
|
"description" : "Template ID",
|
||||||
"required" : true
|
"required" : true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"params" : {
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"body": {
|
"body": null
|
||||||
"description" : "The document",
|
|
||||||
"required" : false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,6 +16,23 @@
|
|||||||
"description" : "Script language",
|
"description" : "Script language",
|
||||||
"required" : true
|
"required" : true
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"params" : {
|
||||||
|
"op_type": {
|
||||||
|
"type" : "enum",
|
||||||
|
"options" : ["index", "create"],
|
||||||
|
"default" : "index",
|
||||||
|
"description" : "Explicit operation type"
|
||||||
|
},
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"body": {
|
"body": {
|
||||||
|
@ -13,6 +13,23 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"params" : {
|
||||||
|
"op_type": {
|
||||||
|
"type" : "enum",
|
||||||
|
"options" : ["index", "create"],
|
||||||
|
"default" : "index",
|
||||||
|
"description" : "Explicit operation type"
|
||||||
|
},
|
||||||
|
"version": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Explicit version number for concurrency control"
|
||||||
|
},
|
||||||
|
"version_type": {
|
||||||
|
"type": "enum",
|
||||||
|
"options": ["internal", "external", "external_gte", "force"],
|
||||||
|
"description": "Specific version type"
|
||||||
|
}
|
||||||
|
},
|
||||||
"body": {
|
"body": {
|
||||||
"description" : "The document",
|
"description" : "The document",
|
||||||
"required" : true
|
"required" : true
|
||||||
|
112
rest-api-spec/test/script/20_versions.yaml
Normal file
112
rest-api-spec/test/script/20_versions.yaml
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
---
|
||||||
|
"External version":
|
||||||
|
|
||||||
|
- do:
|
||||||
|
put_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
body: { "script": "_score * doc[\"myParent.weight\"].value" }
|
||||||
|
version_type: external
|
||||||
|
version: 0
|
||||||
|
|
||||||
|
- match: { _version: 0 }
|
||||||
|
|
||||||
|
- do:
|
||||||
|
put_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
body: { "script": "_score * doc[\"myParent.weight\"].value" }
|
||||||
|
version_type: external
|
||||||
|
version: 5
|
||||||
|
|
||||||
|
- match: { _version: 5 }
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 3
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
delete_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 3
|
||||||
|
|
||||||
|
- do:
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 5
|
||||||
|
version_type: external
|
||||||
|
- is_true: script
|
||||||
|
|
||||||
|
- do:
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 5
|
||||||
|
version_type: external_gte
|
||||||
|
- is_true: script
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 10
|
||||||
|
version_type: external_gte
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
delete_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 3
|
||||||
|
version_type: external
|
||||||
|
|
||||||
|
- do:
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 5
|
||||||
|
version_type: force
|
||||||
|
- is_true: script
|
||||||
|
|
||||||
|
- do:
|
||||||
|
get_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
version: 10
|
||||||
|
version_type: force
|
||||||
|
- is_true: script
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
put_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
body: { "script": "_score * doc[\"myParent.weight\"].value" }
|
||||||
|
version_type: external
|
||||||
|
version: 5
|
||||||
|
|
||||||
|
- do:
|
||||||
|
catch: conflict
|
||||||
|
put_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
body: { "script": "_score * doc[\"myParent.weight\"].value" }
|
||||||
|
version_type: external
|
||||||
|
version: 0
|
||||||
|
|
||||||
|
- do:
|
||||||
|
put_script:
|
||||||
|
lang: groovy
|
||||||
|
id: 1
|
||||||
|
body: { "script": "_score * doc[\"myParent.weight\"].value" }
|
||||||
|
version_type: external
|
||||||
|
version: 6
|
||||||
|
|
||||||
|
- match: { _version: 6}
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.action.delete;
|
package org.elasticsearch.action.delete;
|
||||||
|
|
||||||
|
import org.elasticsearch.action.ActionRequest;
|
||||||
import org.elasticsearch.action.ActionRequestValidationException;
|
import org.elasticsearch.action.ActionRequestValidationException;
|
||||||
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
|
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
@ -86,6 +87,14 @@ public class DeleteRequest extends ShardReplicationOperationRequest<DeleteReques
|
|||||||
public DeleteRequest() {
|
public DeleteRequest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a delete request caused by some other request, which is provided as an
|
||||||
|
* argument so that its headers and context can be copied to the new request
|
||||||
|
*/
|
||||||
|
public DeleteRequest(ActionRequest request) {
|
||||||
|
super(request);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionRequestValidationException validate() {
|
public ActionRequestValidationException validate() {
|
||||||
ActionRequestValidationException validationException = super.validate();
|
ActionRequestValidationException validationException = super.validate();
|
||||||
|
@ -21,6 +21,7 @@ package org.elasticsearch.action.index;
|
|||||||
|
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import org.elasticsearch.*;
|
import org.elasticsearch.*;
|
||||||
|
import org.elasticsearch.action.ActionRequest;
|
||||||
import org.elasticsearch.action.ActionRequestValidationException;
|
import org.elasticsearch.action.ActionRequestValidationException;
|
||||||
import org.elasticsearch.action.RoutingMissingException;
|
import org.elasticsearch.action.RoutingMissingException;
|
||||||
import org.elasticsearch.action.TimestampParsingException;
|
import org.elasticsearch.action.TimestampParsingException;
|
||||||
@ -151,6 +152,14 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
|
|||||||
public IndexRequest() {
|
public IndexRequest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an index request caused by some other request, which is provided as an
|
||||||
|
* argument so that its headers and context can be copied to the new request
|
||||||
|
*/
|
||||||
|
public IndexRequest(ActionRequest request) {
|
||||||
|
super(request);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new index request against the specific index. The {@link #type(String)}
|
* Constructs a new index request against the specific index. The {@link #type(String)}
|
||||||
* {@link #source(byte[])} must be set.
|
* {@link #source(byte[])} must be set.
|
||||||
|
@ -20,11 +20,10 @@
|
|||||||
package org.elasticsearch.action.indexedscripts.delete;
|
package org.elasticsearch.action.indexedscripts.delete;
|
||||||
|
|
||||||
import org.elasticsearch.action.ActionListener;
|
import org.elasticsearch.action.ActionListener;
|
||||||
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
import org.elasticsearch.action.support.ActionFilters;
|
import org.elasticsearch.action.support.ActionFilters;
|
||||||
import org.elasticsearch.action.support.DelegatingActionListener;
|
import org.elasticsearch.action.support.DelegatingActionListener;
|
||||||
import org.elasticsearch.action.delete.DeleteResponse;
|
|
||||||
import org.elasticsearch.action.support.HandledTransportAction;
|
import org.elasticsearch.action.support.HandledTransportAction;
|
||||||
import org.elasticsearch.client.Client;
|
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
@ -37,14 +36,12 @@ import org.elasticsearch.transport.TransportService;
|
|||||||
public class TransportDeleteIndexedScriptAction extends HandledTransportAction<DeleteIndexedScriptRequest, DeleteIndexedScriptResponse> {
|
public class TransportDeleteIndexedScriptAction extends HandledTransportAction<DeleteIndexedScriptRequest, DeleteIndexedScriptResponse> {
|
||||||
|
|
||||||
private final ScriptService scriptService;
|
private final ScriptService scriptService;
|
||||||
private final Client client;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TransportDeleteIndexedScriptAction(Settings settings, ThreadPool threadPool, ScriptService scriptService,
|
public TransportDeleteIndexedScriptAction(Settings settings, ThreadPool threadPool, ScriptService scriptService,
|
||||||
Client client, TransportService transportService, ActionFilters actionFilters) {
|
TransportService transportService, ActionFilters actionFilters) {
|
||||||
super(settings, DeleteIndexedScriptAction.NAME, threadPool, transportService, actionFilters);
|
super(settings, DeleteIndexedScriptAction.NAME, threadPool, transportService, actionFilters);
|
||||||
this.scriptService = scriptService;
|
this.scriptService = scriptService;
|
||||||
this.client = client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,10 +51,10 @@ public class TransportDeleteIndexedScriptAction extends HandledTransportAction<D
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doExecute(final DeleteIndexedScriptRequest request, final ActionListener<DeleteIndexedScriptResponse> listener) {
|
protected void doExecute(final DeleteIndexedScriptRequest request, final ActionListener<DeleteIndexedScriptResponse> listener) {
|
||||||
scriptService.deleteScriptFromIndex(client, request.scriptLang(), request.id(), request.version(), new DelegatingActionListener<DeleteResponse, DeleteIndexedScriptResponse>(listener) {
|
scriptService.deleteScriptFromIndex(request, new DelegatingActionListener<DeleteResponse, DeleteIndexedScriptResponse>(listener) {
|
||||||
@Override
|
@Override
|
||||||
public DeleteIndexedScriptResponse getDelegatedFromInstigator(DeleteResponse deleteResponse){
|
public DeleteIndexedScriptResponse getDelegatedFromInstigator(DeleteResponse deleteResponse){
|
||||||
return new DeleteIndexedScriptResponse(deleteResponse.getIndex(),deleteResponse.getType(),deleteResponse.getType(),deleteResponse.getVersion(), deleteResponse.isFound());
|
return new DeleteIndexedScriptResponse(deleteResponse.getIndex(), deleteResponse.getType(), deleteResponse.getId(), deleteResponse.getVersion(), deleteResponse.isFound());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,6 @@ import org.elasticsearch.action.ActionListener;
|
|||||||
import org.elasticsearch.action.get.GetResponse;
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.support.ActionFilters;
|
import org.elasticsearch.action.support.ActionFilters;
|
||||||
import org.elasticsearch.action.support.HandledTransportAction;
|
import org.elasticsearch.action.support.HandledTransportAction;
|
||||||
import org.elasticsearch.client.Client;
|
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
@ -36,14 +35,12 @@ import org.elasticsearch.transport.TransportService;
|
|||||||
public class TransportGetIndexedScriptAction extends HandledTransportAction<GetIndexedScriptRequest, GetIndexedScriptResponse> {
|
public class TransportGetIndexedScriptAction extends HandledTransportAction<GetIndexedScriptRequest, GetIndexedScriptResponse> {
|
||||||
|
|
||||||
private final ScriptService scriptService;
|
private final ScriptService scriptService;
|
||||||
private final Client client;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TransportGetIndexedScriptAction(Settings settings, ThreadPool threadPool, ScriptService scriptService,
|
public TransportGetIndexedScriptAction(Settings settings, ThreadPool threadPool, ScriptService scriptService,
|
||||||
TransportService transportService, Client client, ActionFilters actionFilters) {
|
TransportService transportService, ActionFilters actionFilters) {
|
||||||
super(settings, GetIndexedScriptAction.NAME, threadPool,transportService, actionFilters);
|
super(settings, GetIndexedScriptAction.NAME, threadPool,transportService, actionFilters);
|
||||||
this.scriptService = scriptService;
|
this.scriptService = scriptService;
|
||||||
this.client = client;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -53,7 +50,7 @@ public class TransportGetIndexedScriptAction extends HandledTransportAction<GetI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doExecute(GetIndexedScriptRequest request, ActionListener<GetIndexedScriptResponse> listener){
|
public void doExecute(GetIndexedScriptRequest request, ActionListener<GetIndexedScriptResponse> listener){
|
||||||
GetResponse scriptResponse = scriptService.queryScriptIndex(client, request.scriptLang(), request.id(), request.version(), request.versionType());
|
GetResponse scriptResponse = scriptService.queryScriptIndex(request);
|
||||||
listener.onResponse(new GetIndexedScriptResponse(scriptResponse));
|
listener.onResponse(new GetIndexedScriptResponse(scriptResponse));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import org.elasticsearch.action.index.IndexResponse;
|
|||||||
import org.elasticsearch.action.support.ActionFilters;
|
import org.elasticsearch.action.support.ActionFilters;
|
||||||
import org.elasticsearch.action.support.DelegatingActionListener;
|
import org.elasticsearch.action.support.DelegatingActionListener;
|
||||||
import org.elasticsearch.action.support.HandledTransportAction;
|
import org.elasticsearch.action.support.HandledTransportAction;
|
||||||
import org.elasticsearch.client.Client;
|
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
@ -37,14 +36,11 @@ import org.elasticsearch.transport.TransportService;
|
|||||||
public class TransportPutIndexedScriptAction extends HandledTransportAction<PutIndexedScriptRequest, PutIndexedScriptResponse> {
|
public class TransportPutIndexedScriptAction extends HandledTransportAction<PutIndexedScriptRequest, PutIndexedScriptResponse> {
|
||||||
|
|
||||||
private final ScriptService scriptService;
|
private final ScriptService scriptService;
|
||||||
private final Client client;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TransportPutIndexedScriptAction(Settings settings, ThreadPool threadPool,
|
public TransportPutIndexedScriptAction(Settings settings, ThreadPool threadPool,
|
||||||
ScriptService scriptService, Client client,
|
ScriptService scriptService, TransportService transportService, ActionFilters actionFilters) {
|
||||||
TransportService transportService, ActionFilters actionFilters) {
|
|
||||||
super(settings, PutIndexedScriptAction.NAME, threadPool, transportService, actionFilters);
|
super(settings, PutIndexedScriptAction.NAME, threadPool, transportService, actionFilters);
|
||||||
this.client = client;
|
|
||||||
this.scriptService = scriptService;
|
this.scriptService = scriptService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,12 +51,11 @@ public class TransportPutIndexedScriptAction extends HandledTransportAction<PutI
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doExecute(final PutIndexedScriptRequest request, final ActionListener<PutIndexedScriptResponse> listener) {
|
protected void doExecute(final PutIndexedScriptRequest request, final ActionListener<PutIndexedScriptResponse> listener) {
|
||||||
scriptService.putScriptToIndex(client, request.safeSource(), request.scriptLang(), request.id(), null, request.opType().toString(), request.version(), request.versionType(), new DelegatingActionListener<IndexResponse,PutIndexedScriptResponse>(listener) {
|
scriptService.putScriptToIndex(request, new DelegatingActionListener<IndexResponse,PutIndexedScriptResponse>(listener) {
|
||||||
@Override
|
@Override
|
||||||
public PutIndexedScriptResponse getDelegatedFromInstigator(IndexResponse indexResponse){
|
public PutIndexedScriptResponse getDelegatedFromInstigator(IndexResponse indexResponse){
|
||||||
return new PutIndexedScriptResponse(indexResponse.getType(),indexResponse.getId(),indexResponse.getVersion(),indexResponse.isCreated());
|
return new PutIndexedScriptResponse(indexResponse.getType(),indexResponse.getId(),indexResponse.getVersion(),indexResponse.isCreated());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,17 +18,16 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.rest.action.script;
|
package org.elasticsearch.rest.action.script;
|
||||||
|
|
||||||
import org.elasticsearch.action.delete.DeleteRequest;
|
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptRequest;
|
||||||
import org.elasticsearch.action.delete.DeleteResponse;
|
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptResponse;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.lucene.uid.Versions;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
||||||
|
import org.elasticsearch.index.VersionType;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
||||||
import org.elasticsearch.script.ScriptService;
|
|
||||||
|
|
||||||
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
|
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
|
||||||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
||||||
@ -36,24 +35,28 @@ import static org.elasticsearch.rest.RestStatus.OK;
|
|||||||
|
|
||||||
public class RestDeleteIndexedScriptAction extends BaseRestHandler {
|
public class RestDeleteIndexedScriptAction extends BaseRestHandler {
|
||||||
|
|
||||||
private ScriptService scriptService;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RestDeleteIndexedScriptAction(Settings settings, Client client,
|
public RestDeleteIndexedScriptAction(Settings settings, Client client, RestController controller) {
|
||||||
ScriptService scriptService, RestController controller) {
|
|
||||||
super(settings, client);
|
super(settings, client);
|
||||||
controller.registerHandler(DELETE, "/_scripts/{lang}/{id}", this);
|
controller.registerHandler(DELETE, "/_scripts/{lang}/{id}", this);
|
||||||
this.scriptService = scriptService;
|
}
|
||||||
|
|
||||||
|
protected RestDeleteIndexedScriptAction(Settings settings, Client client) {
|
||||||
|
super(settings, client);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getScriptLang(RestRequest request) {
|
||||||
|
return request.param("lang");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
||||||
|
DeleteIndexedScriptRequest deleteIndexedScriptRequest = new DeleteIndexedScriptRequest(getScriptLang(request), request.param("id"));
|
||||||
final String id = request.param("id");
|
deleteIndexedScriptRequest.version(request.paramAsLong("version", deleteIndexedScriptRequest.version()));
|
||||||
final long version = request.paramAsLong("version", Versions.MATCH_ANY);
|
deleteIndexedScriptRequest.versionType(VersionType.fromString(request.param("version_type"), deleteIndexedScriptRequest.versionType()));
|
||||||
scriptService.deleteScriptFromIndex(client,request.param("lang"), id, version, new RestBuilderListener<DeleteResponse>(channel) {
|
client.deleteIndexedScript(deleteIndexedScriptRequest, new RestBuilderListener<DeleteIndexedScriptResponse>(channel) {
|
||||||
@Override
|
@Override
|
||||||
public RestResponse buildResponse(DeleteResponse result, XContentBuilder builder) throws Exception {
|
public RestResponse buildResponse(DeleteIndexedScriptResponse result, XContentBuilder builder) throws Exception {
|
||||||
builder.startObject()
|
builder.startObject()
|
||||||
.field(Fields.FOUND, result.isFound())
|
.field(Fields.FOUND, result.isFound())
|
||||||
.field(Fields._INDEX, result.getIndex())
|
.field(Fields._INDEX, result.getIndex())
|
||||||
@ -77,6 +80,4 @@ public class RestDeleteIndexedScriptAction extends BaseRestHandler {
|
|||||||
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
||||||
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
|
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -27,6 +27,7 @@ import org.elasticsearch.common.settings.Settings;
|
|||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
import org.elasticsearch.index.VersionType;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
import org.elasticsearch.rest.action.support.RestResponseListener;
|
import org.elasticsearch.rest.action.support.RestResponseListener;
|
||||||
|
|
||||||
@ -47,11 +48,24 @@ public class RestGetIndexedScriptAction extends BaseRestHandler {
|
|||||||
controller.registerHandler(GET, "/_scripts/{lang}/{id}", this);
|
controller.registerHandler(GET, "/_scripts/{lang}/{id}", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected RestGetIndexedScriptAction(Settings settings, Client client) {
|
||||||
|
super(settings, client);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getScriptLang(RestRequest request) {
|
||||||
|
return request.param("lang");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getScriptFieldName() {
|
||||||
|
return "script";
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
||||||
|
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(getScriptLang(request), request.param("id"));
|
||||||
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest(request.param("lang"), request.param("id"));
|
getRequest.version(request.paramAsLong("version", getRequest.version()));
|
||||||
RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) {
|
getRequest.versionType(VersionType.fromString(request.param("version_type"), getRequest.versionType()));
|
||||||
|
client.getIndexedScript(getRequest, new RestResponseListener<GetIndexedScriptResponse>(channel) {
|
||||||
@Override
|
@Override
|
||||||
public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception {
|
public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception {
|
||||||
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
|
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
|
||||||
@ -61,7 +75,7 @@ public class RestGetIndexedScriptAction extends BaseRestHandler {
|
|||||||
try{
|
try{
|
||||||
String script = response.getScript();
|
String script = response.getScript();
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field("script",script);
|
builder.field(getScriptFieldName(), script);
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
return new BytesRestResponse(OK, builder);
|
return new BytesRestResponse(OK, builder);
|
||||||
} catch( IOException|ClassCastException e ){
|
} catch( IOException|ClassCastException e ){
|
||||||
@ -69,7 +83,6 @@ public class RestGetIndexedScriptAction extends BaseRestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
client.getIndexedScript(getRequest, responseListener);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ import org.elasticsearch.common.inject.Inject;
|
|||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
||||||
|
import org.elasticsearch.index.VersionType;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
||||||
|
|
||||||
@ -52,6 +53,10 @@ public class RestPutIndexedScriptAction extends BaseRestHandler {
|
|||||||
controller.registerHandler(POST, "/_scripts/{lang}/{id}/_create", new CreateHandler(settings, client));
|
controller.registerHandler(POST, "/_scripts/{lang}/{id}/_create", new CreateHandler(settings, client));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected RestPutIndexedScriptAction(Settings settings, Client client) {
|
||||||
|
super(settings, client);
|
||||||
|
}
|
||||||
|
|
||||||
final class CreateHandler extends BaseRestHandler {
|
final class CreateHandler extends BaseRestHandler {
|
||||||
protected CreateHandler(Settings settings, final Client client) {
|
protected CreateHandler(Settings settings, final Client client) {
|
||||||
super(settings, client);
|
super(settings, client);
|
||||||
@ -64,12 +69,15 @@ public class RestPutIndexedScriptAction extends BaseRestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getScriptLang(RestRequest request) {
|
||||||
|
return request.param("lang");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
||||||
|
PutIndexedScriptRequest putRequest = new PutIndexedScriptRequest(getScriptLang(request), request.param("id")).listenerThreaded(false);
|
||||||
PutIndexedScriptRequest putRequest = new PutIndexedScriptRequest(request.param("lang"), request.param("id"));
|
putRequest.version(request.paramAsLong("version", putRequest.version()));
|
||||||
putRequest.listenerThreaded(false);
|
putRequest.versionType(VersionType.fromString(request.param("version_type"), putRequest.versionType()));
|
||||||
|
|
||||||
putRequest.source(request.content(), request.contentUnsafe());
|
putRequest.source(request.content(), request.contentUnsafe());
|
||||||
String sOpType = request.param("op_type");
|
String sOpType = request.param("op_type");
|
||||||
if (sOpType != null) {
|
if (sOpType != null) {
|
||||||
@ -109,5 +117,4 @@ public class RestPutIndexedScriptAction extends BaseRestHandler {
|
|||||||
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
||||||
static final XContentBuilderString CREATED = new XContentBuilderString("created");
|
static final XContentBuilderString CREATED = new XContentBuilderString("created");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,63 +18,25 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.rest.action.template;
|
package org.elasticsearch.rest.action.template;
|
||||||
|
|
||||||
import org.elasticsearch.action.delete.DeleteRequest;
|
|
||||||
import org.elasticsearch.action.delete.DeleteResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.lucene.uid.Versions;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.rest.RestController;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
import org.elasticsearch.rest.RestRequest;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.action.script.RestDeleteIndexedScriptAction;
|
||||||
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
|
||||||
import org.elasticsearch.script.ScriptService;
|
|
||||||
|
|
||||||
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
|
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
|
||||||
import static org.elasticsearch.rest.RestStatus.OK;
|
|
||||||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
|
||||||
|
|
||||||
public class RestDeleteSearchTemplateAction extends BaseRestHandler {
|
public class RestDeleteSearchTemplateAction extends RestDeleteIndexedScriptAction {
|
||||||
|
|
||||||
private ScriptService scriptService;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RestDeleteSearchTemplateAction(Settings settings, Client client, RestController controller, ScriptService scriptService) {
|
public RestDeleteSearchTemplateAction(Settings settings, Client client, RestController controller) {
|
||||||
super(settings, client);
|
super(settings, client);
|
||||||
controller.registerHandler(DELETE, "/_search/template/{id}", this);
|
controller.registerHandler(DELETE, "/_search/template/{id}", this);
|
||||||
this.scriptService = scriptService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
protected String getScriptLang(RestRequest request) {
|
||||||
final String id = request.param("id");
|
return "mustache";
|
||||||
final long version = request.paramAsLong("version", Versions.MATCH_ANY);
|
|
||||||
scriptService.deleteScriptFromIndex(client,"mustache", id, version, new RestBuilderListener<DeleteResponse>(channel) {
|
|
||||||
@Override
|
|
||||||
public RestResponse buildResponse(DeleteResponse result, XContentBuilder builder) throws Exception {
|
|
||||||
builder.startObject()
|
|
||||||
.field(Fields.FOUND, result.isFound())
|
|
||||||
.field(Fields._INDEX, result.getIndex())
|
|
||||||
.field(Fields._TYPE, result.getType())
|
|
||||||
.field(Fields._ID, result.getId())
|
|
||||||
.field(Fields._VERSION, result.getVersion())
|
|
||||||
.endObject();
|
|
||||||
RestStatus status = OK;
|
|
||||||
if (!result.isFound()) {
|
|
||||||
status = NOT_FOUND;
|
|
||||||
}
|
|
||||||
return new BytesRestResponse(status, builder);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class Fields {
|
|
||||||
static final XContentBuilderString FOUND = new XContentBuilderString("found");
|
|
||||||
static final XContentBuilderString _INDEX = new XContentBuilderString("_index");
|
|
||||||
static final XContentBuilderString _TYPE = new XContentBuilderString("_type");
|
|
||||||
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
|
||||||
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -18,28 +18,19 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.rest.action.template;
|
package org.elasticsearch.rest.action.template;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchIllegalStateException;
|
|
||||||
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest;
|
|
||||||
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.rest.RestController;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.rest.RestRequest;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.rest.action.script.RestGetIndexedScriptAction;
|
||||||
import org.elasticsearch.rest.*;
|
|
||||||
import org.elasticsearch.rest.action.support.RestResponseListener;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.elasticsearch.rest.RestRequest.Method.GET;
|
import static org.elasticsearch.rest.RestRequest.Method.GET;
|
||||||
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
|
|
||||||
import static org.elasticsearch.rest.RestStatus.OK;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class RestGetSearchTemplateAction extends BaseRestHandler {
|
public class RestGetSearchTemplateAction extends RestGetIndexedScriptAction {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RestGetSearchTemplateAction(Settings settings, Client client, RestController controller) {
|
public RestGetSearchTemplateAction(Settings settings, Client client, RestController controller) {
|
||||||
@ -48,27 +39,12 @@ public class RestGetSearchTemplateAction extends BaseRestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
protected String getScriptLang(RestRequest request) {
|
||||||
final GetIndexedScriptRequest getRequest = new GetIndexedScriptRequest("mustache", request.param("id"));
|
return "mustache";
|
||||||
RestResponseListener<GetIndexedScriptResponse> responseListener = new RestResponseListener<GetIndexedScriptResponse>(channel) {
|
}
|
||||||
@Override
|
|
||||||
public RestResponse buildResponse(GetIndexedScriptResponse response) throws Exception {
|
@Override
|
||||||
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
|
protected String getScriptFieldName() {
|
||||||
if (!response.isExists()) {
|
return "template";
|
||||||
return new BytesRestResponse(NOT_FOUND, builder);
|
|
||||||
} else {
|
|
||||||
try{
|
|
||||||
String templateString = response.getScript();
|
|
||||||
builder.startObject();
|
|
||||||
builder.field("template",templateString);
|
|
||||||
builder.endObject();
|
|
||||||
return new BytesRestResponse(OK, builder);
|
|
||||||
} catch( IOException|ClassCastException e ){
|
|
||||||
throw new ElasticsearchIllegalStateException("Unable to parse " + response.getScript() + " as json",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
client.getIndexedScript(getRequest, responseListener);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,30 +18,22 @@
|
|||||||
*/
|
*/
|
||||||
package org.elasticsearch.rest.action.template;
|
package org.elasticsearch.rest.action.template;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
|
||||||
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest;
|
|
||||||
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.rest.BaseRestHandler;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilderString;
|
import org.elasticsearch.rest.RestChannel;
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.RestController;
|
||||||
import org.elasticsearch.rest.action.support.RestBuilderListener;
|
import org.elasticsearch.rest.RestRequest;
|
||||||
|
import org.elasticsearch.rest.action.script.RestPutIndexedScriptAction;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import static org.elasticsearch.rest.RestRequest.Method.POST;
|
import static org.elasticsearch.rest.RestRequest.Method.POST;
|
||||||
import static org.elasticsearch.rest.RestRequest.Method.PUT;
|
import static org.elasticsearch.rest.RestRequest.Method.PUT;
|
||||||
import static org.elasticsearch.rest.RestStatus.CREATED;
|
|
||||||
import static org.elasticsearch.rest.RestStatus.OK;
|
|
||||||
import static org.elasticsearch.rest.RestStatus.BAD_REQUEST;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class RestPutSearchTemplateAction extends BaseRestHandler {
|
public class RestPutSearchTemplateAction extends RestPutIndexedScriptAction {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RestPutSearchTemplateAction(Settings settings, Client client, RestController controller) {
|
public RestPutSearchTemplateAction(Settings settings, Client client, RestController controller) {
|
||||||
@ -67,51 +59,8 @@ public class RestPutSearchTemplateAction extends BaseRestHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) {
|
protected String getScriptLang(RestRequest request) {
|
||||||
PutIndexedScriptRequest putRequest = new PutIndexedScriptRequest("mustache", request.param("id"));
|
return "mustache";
|
||||||
putRequest.listenerThreaded(false);
|
|
||||||
|
|
||||||
putRequest.source(request.content(), request.contentUnsafe());
|
|
||||||
String sOpType = request.param("op_type");
|
|
||||||
if (sOpType != null) {
|
|
||||||
try {
|
|
||||||
putRequest.opType(IndexRequest.OpType.fromString(sOpType));
|
|
||||||
} catch (ElasticsearchIllegalArgumentException eia){
|
|
||||||
try {
|
|
||||||
XContentBuilder builder = channel.newBuilder();
|
|
||||||
channel.sendResponse(new BytesRestResponse(BAD_REQUEST, builder.startObject().field("error", eia.getMessage()).endObject()));
|
|
||||||
return;
|
|
||||||
} catch (IOException e1) {
|
|
||||||
logger.warn("Failed to send response", e1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
client.putIndexedScript(putRequest, new RestBuilderListener<PutIndexedScriptResponse>(channel) {
|
|
||||||
@Override
|
|
||||||
public RestResponse buildResponse(PutIndexedScriptResponse response, XContentBuilder builder) throws Exception {
|
|
||||||
builder.startObject()
|
|
||||||
.field(Fields._ID, response.getId())
|
|
||||||
.field(Fields._VERSION, response.getVersion())
|
|
||||||
.field(Fields.CREATED, response.isCreated());
|
|
||||||
builder.endObject();
|
|
||||||
RestStatus status = OK;
|
|
||||||
if (response.isCreated()) {
|
|
||||||
status = CREATED;
|
|
||||||
}
|
|
||||||
return new BytesRestResponse(status, builder);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class Fields {
|
|
||||||
static final XContentBuilderString _VERSION = new XContentBuilderString("_version");
|
|
||||||
static final XContentBuilderString _ID = new XContentBuilderString("_id");
|
|
||||||
static final XContentBuilderString CREATED = new XContentBuilderString("created");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,13 +26,15 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||||
import org.elasticsearch.ElasticsearchIllegalStateException;
|
import org.elasticsearch.ElasticsearchIllegalStateException;
|
||||||
import org.elasticsearch.action.ActionListener;
|
import org.elasticsearch.action.ActionListener;
|
||||||
import org.elasticsearch.action.ListenableActionFuture;
|
|
||||||
import org.elasticsearch.action.delete.DeleteRequest;
|
import org.elasticsearch.action.delete.DeleteRequest;
|
||||||
import org.elasticsearch.action.delete.DeleteResponse;
|
import org.elasticsearch.action.delete.DeleteResponse;
|
||||||
import org.elasticsearch.action.get.GetRequest;
|
import org.elasticsearch.action.get.GetRequest;
|
||||||
import org.elasticsearch.action.get.GetResponse;
|
import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.index.IndexResponse;
|
import org.elasticsearch.action.index.IndexResponse;
|
||||||
|
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptRequest;
|
||||||
|
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest;
|
||||||
|
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
import org.elasticsearch.common.ParseField;
|
import org.elasticsearch.common.ParseField;
|
||||||
@ -44,7 +46,6 @@ import org.elasticsearch.common.inject.Inject;
|
|||||||
import org.elasticsearch.common.io.Streams;
|
import org.elasticsearch.common.io.Streams;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||||
import org.elasticsearch.common.lucene.uid.Versions;
|
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
|
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
|
||||||
@ -53,7 +54,6 @@ import org.elasticsearch.common.xcontent.XContentFactory;
|
|||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
import org.elasticsearch.env.Environment;
|
import org.elasticsearch.env.Environment;
|
||||||
import org.elasticsearch.index.VersionType;
|
|
||||||
import org.elasticsearch.index.query.TemplateQueryParser;
|
import org.elasticsearch.index.query.TemplateQueryParser;
|
||||||
import org.elasticsearch.search.lookup.SearchLookup;
|
import org.elasticsearch.search.lookup.SearchLookup;
|
||||||
import org.elasticsearch.watcher.FileChangesListener;
|
import org.elasticsearch.watcher.FileChangesListener;
|
||||||
@ -81,10 +81,9 @@ public class ScriptService extends AbstractComponent {
|
|||||||
public static final String SCRIPT_CACHE_EXPIRE_SETTING = "script.cache.expire";
|
public static final String SCRIPT_CACHE_EXPIRE_SETTING = "script.cache.expire";
|
||||||
public static final String DISABLE_DYNAMIC_SCRIPTING_DEFAULT = "sandbox";
|
public static final String DISABLE_DYNAMIC_SCRIPTING_DEFAULT = "sandbox";
|
||||||
public static final String SCRIPT_INDEX = ".scripts";
|
public static final String SCRIPT_INDEX = ".scripts";
|
||||||
|
public static final String DEFAULT_LANG = "groovy";
|
||||||
|
|
||||||
//Make static so that it has visibility in IndexedScript
|
private final String defaultLang;
|
||||||
//Looked up from settings in ctor
|
|
||||||
private static String defaultLang = "groovy";
|
|
||||||
|
|
||||||
private final ImmutableMap<String, ScriptEngineService> scriptEngines;
|
private final ImmutableMap<String, ScriptEngineService> scriptEngines;
|
||||||
|
|
||||||
@ -108,7 +107,7 @@ public class ScriptService extends AbstractComponent {
|
|||||||
ONLY_DISK_ALLOWED,
|
ONLY_DISK_ALLOWED,
|
||||||
SANDBOXED_ONLY;
|
SANDBOXED_ONLY;
|
||||||
|
|
||||||
public static final DynamicScriptDisabling parse(String s) {
|
static DynamicScriptDisabling parse(String s) {
|
||||||
switch (s.toLowerCase(Locale.ROOT)) {
|
switch (s.toLowerCase(Locale.ROOT)) {
|
||||||
// true for "disable_dynamic" means only on-disk scripts are enabled
|
// true for "disable_dynamic" means only on-disk scripts are enabled
|
||||||
case "true":
|
case "true":
|
||||||
@ -129,15 +128,6 @@ public class ScriptService extends AbstractComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final ParseField SCRIPT_LANG = new ParseField("lang","script_lang");
|
public static final ParseField SCRIPT_LANG = new ParseField("lang","script_lang");
|
||||||
|
|
||||||
public static final ParseField VALUE_SCRIPT_FILE = new ParseField("value_script_file");
|
|
||||||
public static final ParseField VALUE_SCRIPT_ID = new ParseField("value_script_id");
|
|
||||||
public static final ParseField VALUE_SCRIPT_INLINE = new ParseField("value_script");
|
|
||||||
|
|
||||||
public static final ParseField KEY_SCRIPT_FILE = new ParseField("key_script_file");
|
|
||||||
public static final ParseField KEY_SCRIPT_ID = new ParseField("key_script_id");
|
|
||||||
public static final ParseField KEY_SCRIPT_INLINE = new ParseField("key_script");
|
|
||||||
|
|
||||||
public static final ParseField SCRIPT_FILE = new ParseField("script_file","file");
|
public static final ParseField SCRIPT_FILE = new ParseField("script_file","file");
|
||||||
public static final ParseField SCRIPT_ID = new ParseField("script_id", "id");
|
public static final ParseField SCRIPT_ID = new ParseField("script_id", "id");
|
||||||
public static final ParseField SCRIPT_INLINE = new ParseField("script","scriptField");
|
public static final ParseField SCRIPT_INLINE = new ParseField("script","scriptField");
|
||||||
@ -165,8 +155,7 @@ public class ScriptService extends AbstractComponent {
|
|||||||
throw new ElasticsearchIllegalArgumentException("Unexpected value read for ScriptType got [" + scriptTypeVal +
|
throw new ElasticsearchIllegalArgumentException("Unexpected value read for ScriptType got [" + scriptTypeVal +
|
||||||
"] expected one of ["+INLINE_VAL +"," + INDEXED_VAL + "," + FILE_VAL+"]");
|
"] expected one of ["+INLINE_VAL +"," + INDEXED_VAL + "," + FILE_VAL+"]");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void writeTo(ScriptType scriptType, StreamOutput out) throws IOException{
|
public static void writeTo(ScriptType scriptType, StreamOutput out) throws IOException{
|
||||||
if (scriptType != null) {
|
if (scriptType != null) {
|
||||||
@ -190,17 +179,14 @@ public class ScriptService extends AbstractComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static class IndexedScript {
|
static class IndexedScript {
|
||||||
String lang;
|
private final String lang;
|
||||||
String id;
|
private final String id;
|
||||||
|
|
||||||
IndexedScript(String lang, String script) {
|
IndexedScript(String lang, String script) {
|
||||||
this.lang = lang;
|
this.lang = lang;
|
||||||
final String[] parts = script.split("/");
|
final String[] parts = script.split("/");
|
||||||
if (parts.length == 1) {
|
if (parts.length == 1) {
|
||||||
this.id = script;
|
this.id = script;
|
||||||
if (this.lang == null){
|
|
||||||
this.lang = defaultLang;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (parts.length != 3) {
|
if (parts.length != 3) {
|
||||||
throw new ElasticsearchIllegalArgumentException("Illegal index script format [" + script + "]" +
|
throw new ElasticsearchIllegalArgumentException("Illegal index script format [" + script + "]" +
|
||||||
@ -224,7 +210,7 @@ public class ScriptService extends AbstractComponent {
|
|||||||
TimeValue cacheExpire = settings.getAsTime(SCRIPT_CACHE_EXPIRE_SETTING, null);
|
TimeValue cacheExpire = settings.getAsTime(SCRIPT_CACHE_EXPIRE_SETTING, null);
|
||||||
logger.debug("using script cache with max_size [{}], expire [{}]", cacheMaxSize, cacheExpire);
|
logger.debug("using script cache with max_size [{}], expire [{}]", cacheMaxSize, cacheExpire);
|
||||||
|
|
||||||
this.defaultLang = settings.get(DEFAULT_SCRIPTING_LANGUAGE_SETTING, "groovy");
|
this.defaultLang = settings.get(DEFAULT_SCRIPTING_LANGUAGE_SETTING, DEFAULT_LANG);
|
||||||
this.dynamicScriptingDisabled = DynamicScriptDisabling.parse(settings.get(DISABLE_DYNAMIC_SCRIPTING_SETTING, DISABLE_DYNAMIC_SCRIPTING_DEFAULT));
|
this.dynamicScriptingDisabled = DynamicScriptDisabling.parse(settings.get(DISABLE_DYNAMIC_SCRIPTING_SETTING, DISABLE_DYNAMIC_SCRIPTING_DEFAULT));
|
||||||
|
|
||||||
CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
|
CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
|
||||||
@ -292,17 +278,16 @@ public class ScriptService extends AbstractComponent {
|
|||||||
CacheKey cacheKey;
|
CacheKey cacheKey;
|
||||||
CompiledScript compiled;
|
CompiledScript compiled;
|
||||||
|
|
||||||
|
if (lang == null) {
|
||||||
|
lang = defaultLang;
|
||||||
|
}
|
||||||
|
|
||||||
if(scriptType == ScriptType.INDEXED) {
|
if(scriptType == ScriptType.INDEXED) {
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
throw new ElasticsearchIllegalArgumentException("Got an indexed script with no Client registered.");
|
throw new ElasticsearchIllegalArgumentException("Got an indexed script with no Client registered.");
|
||||||
}
|
}
|
||||||
|
|
||||||
final IndexedScript indexedScript = new IndexedScript(lang,script);
|
final IndexedScript indexedScript = new IndexedScript(lang, script);
|
||||||
|
|
||||||
if (lang != null && !lang.equals(indexedScript.lang)) {
|
|
||||||
logger.trace("Overriding lang to " + indexedScript.lang);
|
|
||||||
lang = indexedScript.lang;
|
|
||||||
}
|
|
||||||
|
|
||||||
verifyDynamicScripting(indexedScript.lang); //Since anyone can index a script, disable indexed scripting
|
verifyDynamicScripting(indexedScript.lang); //Since anyone can index a script, disable indexed scripting
|
||||||
// if dynamic scripting is disabled, perhaps its own setting ?
|
// if dynamic scripting is disabled, perhaps its own setting ?
|
||||||
@ -328,10 +313,6 @@ public class ScriptService extends AbstractComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lang == null) {
|
|
||||||
lang = defaultLang;
|
|
||||||
}
|
|
||||||
|
|
||||||
//This is an inline script check to see if we have it in the cache
|
//This is an inline script check to see if we have it in the cache
|
||||||
verifyDynamicScripting(lang);
|
verifyDynamicScripting(lang);
|
||||||
|
|
||||||
@ -373,32 +354,27 @@ public class ScriptService extends AbstractComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GetResponse queryScriptIndex(Client client, String scriptLang, String id){
|
public GetResponse queryScriptIndex(GetIndexedScriptRequest request) {
|
||||||
return queryScriptIndex(client, scriptLang, id, Versions.MATCH_ANY, VersionType.INTERNAL);
|
String scriptLang = validateScriptLanguage(request.scriptLang());
|
||||||
}
|
GetRequest getRequest = new GetRequest(request, SCRIPT_INDEX).type(scriptLang).id(request.id())
|
||||||
|
.version(request.version()).versionType(request.versionType())
|
||||||
public GetResponse queryScriptIndex(Client client, String scriptLang, String id,
|
.operationThreaded(false).preference("_local"); //Set preference for no forking
|
||||||
long version, VersionType versionType) {
|
return client.get(getRequest).actionGet();
|
||||||
scriptLang = validateScriptLanguage(scriptLang);
|
|
||||||
return client.prepareGet(SCRIPT_INDEX, scriptLang, id)
|
|
||||||
.setVersion(version)
|
|
||||||
.setVersionType(versionType)
|
|
||||||
.setPreference("_local") //Set preference for no forking
|
|
||||||
.setOperationThreaded(false)
|
|
||||||
.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String validateScriptLanguage(String scriptLang) {
|
private String validateScriptLanguage(String scriptLang) {
|
||||||
if (scriptLang == null){
|
if (scriptLang == null) {
|
||||||
scriptLang = defaultLang;
|
scriptLang = defaultLang;
|
||||||
} else if (!scriptEngines.containsKey(scriptLang)){
|
} else if (!scriptEngines.containsKey(scriptLang)) {
|
||||||
throw new ElasticsearchIllegalArgumentException("script_lang not supported ["+scriptLang+"]");
|
throw new ElasticsearchIllegalArgumentException("script_lang not supported ["+scriptLang+"]");
|
||||||
}
|
}
|
||||||
return scriptLang;
|
return scriptLang;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getScriptFromIndex(Client client, String scriptLang, String id) {
|
private String getScriptFromIndex(Client client, String scriptLang, String id) {
|
||||||
GetResponse responseFields = queryScriptIndex(client,scriptLang,id);
|
scriptLang = validateScriptLanguage(scriptLang);
|
||||||
|
GetRequest getRequest = new GetRequest(SCRIPT_INDEX, scriptLang, id);
|
||||||
|
GetResponse responseFields = client.get(getRequest).actionGet();
|
||||||
if (responseFields.isExists()) {
|
if (responseFields.isExists()) {
|
||||||
return getScriptFromResponse(responseFields);
|
return getScriptFromResponse(responseFields);
|
||||||
}
|
}
|
||||||
@ -406,63 +382,47 @@ public class ScriptService extends AbstractComponent {
|
|||||||
+ scriptLang + "/" + id + "]");
|
+ scriptLang + "/" + id + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validate(BytesReference scriptBytes, String scriptLang) throws IOException{
|
private void validate(BytesReference scriptBytes, String scriptLang) {
|
||||||
XContentParser parser = XContentFactory.xContent(scriptBytes).createParser(scriptBytes);
|
|
||||||
TemplateQueryParser.TemplateContext context = TemplateQueryParser.parse(parser, "params", "script", "template");
|
|
||||||
if (Strings.hasLength(context.template()) == true){
|
|
||||||
//Just try and compile it
|
|
||||||
//This will have the benefit of also adding the script to the cache if it compiles
|
|
||||||
try {
|
|
||||||
CompiledScript compiledScript = compile(scriptLang, context.template(), ScriptService.ScriptType.INLINE);
|
|
||||||
if (compiledScript == null) {
|
|
||||||
throw new ElasticsearchIllegalArgumentException("Unable to parse [" + context.template() +
|
|
||||||
"] lang [" + scriptLang + "] (ScriptService.compile returned null)");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ElasticsearchIllegalArgumentException("Unable to parse [" + context.template() +
|
|
||||||
"] lang [" + scriptLang + "]", e);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new ElasticsearchIllegalArgumentException("Unable to find script in : " + scriptBytes.toUtf8());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void putScriptToIndex(Client client, BytesReference scriptBytes, @Nullable String scriptLang, String id,
|
|
||||||
@Nullable TimeValue timeout, @Nullable String sOpType, long version,
|
|
||||||
VersionType versionType, ActionListener<IndexResponse> listener) {
|
|
||||||
try {
|
try {
|
||||||
scriptLang = validateScriptLanguage(scriptLang);
|
XContentParser parser = XContentFactory.xContent(scriptBytes).createParser(scriptBytes);
|
||||||
|
TemplateQueryParser.TemplateContext context = TemplateQueryParser.parse(parser, "params", "script", "template");
|
||||||
//verify that the script compiles
|
if (Strings.hasLength(context.template())){
|
||||||
validate(scriptBytes, scriptLang);
|
//Just try and compile it
|
||||||
|
//This will have the benefit of also adding the script to the cache if it compiles
|
||||||
IndexRequest indexRequest = new IndexRequest(SCRIPT_INDEX, scriptLang, id);
|
try {
|
||||||
indexRequest.listenerThreaded(false);
|
CompiledScript compiledScript = compile(scriptLang, context.template(), ScriptService.ScriptType.INLINE);
|
||||||
indexRequest.operationThreaded(false);
|
if (compiledScript == null) {
|
||||||
indexRequest.version(version);
|
throw new ElasticsearchIllegalArgumentException("Unable to parse [" + context.template() +
|
||||||
indexRequest.versionType(versionType);
|
"] lang [" + scriptLang + "] (ScriptService.compile returned null)");
|
||||||
indexRequest.refresh(true); //Always refresh after indexing a template
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
indexRequest.source(scriptBytes, true);
|
throw new ElasticsearchIllegalArgumentException("Unable to parse [" + context.template() +
|
||||||
if (timeout != null) {
|
"] lang [" + scriptLang + "]", e);
|
||||||
indexRequest.timeout(timeout);
|
}
|
||||||
|
} else {
|
||||||
|
throw new ElasticsearchIllegalArgumentException("Unable to find script in : " + scriptBytes.toUtf8());
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
if (sOpType != null) {
|
throw new ElasticsearchIllegalArgumentException("failed to parse template script", e);
|
||||||
indexRequest.opType(IndexRequest.OpType.fromString(sOpType));
|
|
||||||
}
|
|
||||||
|
|
||||||
client.index(indexRequest, listener);
|
|
||||||
} catch (Throwable t){
|
|
||||||
listener.onFailure(t);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteScriptFromIndex(Client client, @Nullable String scriptLang, String id,
|
public void putScriptToIndex(PutIndexedScriptRequest request, ActionListener<IndexResponse> listener) {
|
||||||
long version, ActionListener<DeleteResponse> listener) {
|
String scriptLang = validateScriptLanguage(request.scriptLang());
|
||||||
scriptLang = validateScriptLanguage(scriptLang);
|
//verify that the script compiles
|
||||||
client.delete((new DeleteRequest(SCRIPT_INDEX,scriptLang,id)).refresh(true).version(version), listener);
|
validate(request.safeSource(), scriptLang);
|
||||||
|
|
||||||
|
IndexRequest indexRequest = new IndexRequest(request).index(SCRIPT_INDEX).type(scriptLang).id(request.id())
|
||||||
|
.listenerThreaded(false).operationThreaded(false).version(request.version()).versionType(request.versionType())
|
||||||
|
.source(request.safeSource(), true).opType(request.opType()).refresh(true); //Always refresh after indexing a template
|
||||||
|
client.index(indexRequest, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteScriptFromIndex(DeleteIndexedScriptRequest request, ActionListener<DeleteResponse> listener) {
|
||||||
|
String scriptLang = validateScriptLanguage(request.scriptLang());
|
||||||
|
DeleteRequest deleteRequest = new DeleteRequest(request).index(SCRIPT_INDEX).type(scriptLang).id(request.id())
|
||||||
|
.refresh(true).version(request.version()).versionType(request.versionType());
|
||||||
|
client.delete(deleteRequest, listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getScriptFromResponse(GetResponse responseFields) {
|
public static String getScriptFromResponse(GetResponse responseFields) {
|
||||||
@ -602,6 +562,9 @@ public class ScriptService extends AbstractComponent {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
|
if (! (o instanceof CacheKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
CacheKey other = (CacheKey) o;
|
CacheKey other = (CacheKey) o;
|
||||||
return lang.equals(other.lang) && script.equals(other.script);
|
return lang.equals(other.lang) && script.equals(other.script);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user