Scripting: Deprecate stored search template apis (#25437)

This commit deprecates the PUT, GET and DELETE search template apis.
Instead, the stored script api should be used.

closes #24596
This commit is contained in:
Ryan Ernst 2017-07-12 16:07:28 -07:00 committed by GitHub
parent e2bfb35f4a
commit 70b2897bdf
10 changed files with 119 additions and 70 deletions

View File

@ -116,12 +116,15 @@ You can also create search templates:
[source,js] [source,js]
------------------------------------------ ------------------------------------------
POST /_search/template/my_template_1 POST /_scripts/my_template_1
{ {
"template": { "script": {
"query": { "lang": "mustache",
"match": { "source": {
"message": "{{query_string}}" "query": {
"match": {
"message": "{{query_string}}"
}
} }
} }
} }
@ -132,12 +135,15 @@ POST /_search/template/my_template_1
[source,js] [source,js]
------------------------------------------ ------------------------------------------
POST /_search/template/my_template_2 POST /_scripts/my_template_2
{ {
"template": { "script": {
"query": { "lang": "mustache",
"term": { "source": {
"{{field}}": "{{value}}" "query": {
"term": {
"{{field}}": "{{value}}"
}
} }
} }
} }

View File

@ -402,17 +402,19 @@ The previous query will be rendered as:
[[pre-registered-templates]] [[pre-registered-templates]]
===== Pre-registered template ===== Pre-registered template
You can register search templates by storing them in the cluster state. You can register search templates by using the stored scripts api.
There are REST APIs to manage these stored templates.
[source,js] [source,js]
------------------------------------------ ------------------------------------------
POST _search/template/<templatename> POST _scripts/<templatename>
{ {
"template": { "script": {
"query": { "lang": "mustache",
"match": { "source": {
"title": "{{query_string}}" "query": {
"match": {
"title": "{{query_string}}"
}
} }
} }
} }
@ -440,7 +442,7 @@ This template can be retrieved by
[source,js] [source,js]
------------------------------------------ ------------------------------------------
GET _search/template/<templatename> GET _scripts/<templatename>
------------------------------------------ ------------------------------------------
// CONSOLE // CONSOLE
// TEST[continued] // TEST[continued]
@ -450,10 +452,15 @@ which is rendered as:
[source,js] [source,js]
------------------------------------------ ------------------------------------------
{ {
"_id" : "<templatename>", "script" : {
"lang" : "mustache", "lang" : "mustache",
"found" : true, "source" : "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}",
"template" : "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}" "options": {
"content_type" : "application/json; charset=UTF-8"
}
},
"_id": "<templatename>",
"found": true
} }
------------------------------------------ ------------------------------------------
// TESTRESPONSE // TESTRESPONSE
@ -462,7 +469,7 @@ This template can be deleted by
[source,js] [source,js]
------------------------------------------ ------------------------------------------
DELETE _search/template/<templatename> DELETE _scripts/<templatename>
------------------------------------------ ------------------------------------------
// CONSOLE // CONSOLE
// TEST[continued] // TEST[continued]

View File

@ -20,6 +20,8 @@ package org.elasticsearch.script.mustache;
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest; import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestController;
@ -32,11 +34,14 @@ import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE; import static org.elasticsearch.rest.RestRequest.Method.DELETE;
public class RestDeleteSearchTemplateAction extends BaseRestHandler { public class RestDeleteSearchTemplateAction extends BaseRestHandler {
private static final DeprecationLogger DEPRECATION_LOGGER =
new DeprecationLogger(Loggers.getLogger(RestDeleteSearchTemplateAction.class));
public RestDeleteSearchTemplateAction(Settings settings, RestController controller) { public RestDeleteSearchTemplateAction(Settings settings, RestController controller) {
super(settings); super(settings);
controller.registerHandler(DELETE, "/_search/template/{id}", this); controller.registerAsDeprecatedHandler(DELETE, "/_search/template/{id}", this,
"The stored search template API is deprecated. Use stored scripts instead.", DEPRECATION_LOGGER);
} }
@Override @Override

View File

@ -22,6 +22,8 @@ import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptReque
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
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.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
@ -39,6 +41,8 @@ import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.GET;
public class RestGetSearchTemplateAction extends BaseRestHandler { public class RestGetSearchTemplateAction extends BaseRestHandler {
private static final DeprecationLogger DEPRECATION_LOGGER =
new DeprecationLogger(Loggers.getLogger(RestGetSearchTemplateAction.class));
public static final ParseField _ID_PARSE_FIELD = new ParseField("_id"); public static final ParseField _ID_PARSE_FIELD = new ParseField("_id");
@ -47,7 +51,8 @@ public class RestGetSearchTemplateAction extends BaseRestHandler {
public RestGetSearchTemplateAction(Settings settings, RestController controller) { public RestGetSearchTemplateAction(Settings settings, RestController controller) {
super(settings); super(settings);
controller.registerHandler(GET, "/_search/template/{id}", this); controller.registerAsDeprecatedHandler(GET, "/_search/template/{id}", this,
"The stored search template API is deprecated. Use stored scripts instead.", DEPRECATION_LOGGER);
} }
@Override @Override

View File

@ -23,6 +23,8 @@ import java.io.IOException;
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest; import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestController;
@ -36,11 +38,15 @@ import static org.elasticsearch.rest.RestRequest.Method.PUT;
public class RestPutSearchTemplateAction extends BaseRestHandler { public class RestPutSearchTemplateAction extends BaseRestHandler {
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(RestPutSearchTemplateAction.class));
public RestPutSearchTemplateAction(Settings settings, RestController controller) { public RestPutSearchTemplateAction(Settings settings, RestController controller) {
super(settings); super(settings);
controller.registerHandler(POST, "/_search/template/{id}", this); controller.registerAsDeprecatedHandler(POST, "/_search/template/{id}", this,
controller.registerHandler(PUT, "/_search/template/{id}", this); "The stored search template API is deprecated. Use stored scripts instead.", DEPRECATION_LOGGER);
controller.registerAsDeprecatedHandler(PUT, "/_search/template/{id}", this,
"The stored search template API is deprecated. Use stored scripts instead.", DEPRECATION_LOGGER);
} }
@Override @Override

View File

@ -13,15 +13,21 @@
- match: { nodes.$master.modules.0.name: lang-mustache } - match: { nodes.$master.modules.0.name: lang-mustache }
--- ---
"Indexed template": "Stored template":
- skip:
features: "warnings"
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
put_template: put_template:
id: "1" id: "1"
body: { "template": { "query": { "match_all": {}}, "size": "{{my_size}}" } } body: { "template": { "query": { "match_all": {}}, "size": "{{my_size}}" } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
get_template: get_template:
id: 1 id: 1
- match: { found: true } - match: { found: true }
@ -30,6 +36,8 @@
- match: { template: /.*query\S\S\S\Smatch_all.*/ } - match: { template: /.*query\S\S\S\Smatch_all.*/ }
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
catch: missing catch: missing
get_template: get_template:
id: 2 id: 2
@ -38,22 +46,30 @@
- match: { _id: "2" } - match: { _id: "2" }
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
delete_template: delete_template:
id: "1" id: "1"
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
catch: missing catch: missing
delete_template: delete_template:
id: "non_existing" id: "non_existing"
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
catch: request catch: request
put_template: put_template:
id: "1" id: "1"
body: { "template": { "query": { "match{{}}_all": {}}, "size": "{{my_size}}" } } body: { "template": { "query": { "match{{}}_all": {}}, "size": "{{my_size}}" } }
- do: - do:
warnings:
- "The stored search template API is deprecated. Use stored scripts instead."
catch: /failed\sto\sparse.*/ catch: /failed\sto\sparse.*/
put_template: put_template:
id: "1" id: "1"

View File

@ -2,9 +2,9 @@
"Stored Template validate tests": "Stored Template validate tests":
- do: - do:
put_template: put_script:
id: "1" id: "1"
body: { "template": { "query": { "match": { "text": "{{my_value}}" } }, "aggs": { "my_terms": { "terms": { "field": "{{my_field}}" } } } } } body: { "script": { "lang": "mustache", "source": { "query": { "match": { "text": "{{my_value}}" } }, "aggs": { "my_terms": { "terms": { "field": "{{my_field}}" } } } } } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:
@ -54,9 +54,9 @@
"Escaped Stored Template validate tests": "Escaped Stored Template validate tests":
- do: - do:
put_template: put_script:
id: "1" id: "1"
body: { "template": "{ \"query\": { \"match\": { \"text\": \"{{my_value}}\" } }, \"size\": {{my_size}} }" } body: { "script": { "lang" : "mustache", "source" : "{ \"query\": { \"match\": { \"text\": \"{{my_value}}\" } }, \"size\": {{my_size}} }" } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:
@ -122,9 +122,9 @@
indices.refresh: {} indices.refresh: {}
- do: - do:
put_template: put_script:
id: "1" id: "1"
body: { "template": { "query": { "match" : { "text": "{{my_value}}" } }, "size": "{{my_size}}" } } body: { "script": { "lang": "mustache", "source": { "query": { "match" : { "text": "{{my_value}}" } }, "size": "{{my_size}}" } } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:

View File

@ -23,9 +23,9 @@
- match: { hits.total: 1 } - match: { hits.total: 1 }
- do: - do:
put_template: put_script:
id: "1" id: "1"
body: { "template": { "query": { "term": { "text": "{{template}}" } } } } body: { "script": { "lang": "mustache", "source": { "query": { "term": { "text": "{{template}}" } } } } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:
@ -88,9 +88,9 @@
indices.refresh: {} indices.refresh: {}
- do: - do:
put_template: put_script:
id: "template_1" id: "template_1"
body: { "template": { "query": { "match": { "{{field}}": { "query" : "{{value}}", "operator" : "{{operator}}{{^operator}}or{{/operator}}" } } }, "size": "{{size}}" } } body: { "script": { "lang": "mustache", "source": { "query": { "match": { "{{field}}": { "query" : "{{value}}", "operator" : "{{operator}}{{^operator}}or{{/operator}}" } } }, "size": "{{size}}" } } }
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:

View File

@ -129,9 +129,9 @@ setup:
"Basic multi-search using stored template": "Basic multi-search using stored template":
- do: - do:
put_template: put_script:
id: stored_template_1 id: stored_template_1
body: {"template": {"query": {"match": {"{{field}}": "{{value}}" }}}} body: { "script": { "lang" : "mustache", "source": { "query": {"match": {"{{field}}": "{{value}}" }}}}}
- match: { acknowledged: true } - match: { acknowledged: true }
- do: - do:

View File

@ -37,22 +37,24 @@ setup:
"Search template with typed_keys parameter": "Search template with typed_keys parameter":
- do: - do:
put_template: put_script:
id: template_1 id: template_1
body: body:
template: script:
query: lang: mustache
match: source:
bool: "{{bool_value}}" query:
aggs: match:
test_missing: bool: "{{bool_value}}"
missing: aggs:
field: "{{missing_field}}" test_missing:
suggest: missing:
term_suggest: field: "{{missing_field}}"
text: "{{suggest_text}}" suggest:
term: term_suggest:
field: "{{suggest_field}}" text: "{{suggest_text}}"
term:
field: "{{suggest_field}}"
- match: { acknowledged: true } - match: { acknowledged: true }
@ -76,24 +78,26 @@ setup:
"Multisearch template with typed_keys parameter": "Multisearch template with typed_keys parameter":
- do: - do:
put_template: put_script:
id: registered_template id: registered_template
body: body:
template: script:
query: lang: mustache
range: source:
integer: query:
gte: "{{starting_value}}" range:
aggs: integer:
test_histogram: gte: "{{starting_value}}"
histogram: aggs:
field: "{{histo.field}}" test_histogram:
interval: "{{histo.interval}}" histogram:
suggest: field: "{{histo.field}}"
phrase_suggester: interval: "{{histo.interval}}"
text: "{{keywords}}" suggest:
phrase: phrase_suggester:
field: name text: "{{keywords}}"
phrase:
field: name
- match: { acknowledged: true } - match: { acknowledged: true }