Skip to content

Commit

Permalink
Indexed Templates: Add CRUD API, explicit template types
Browse files Browse the repository at this point in the history
This change allows the creation,update and deletion of indexed templates via the _search/template/{id} endpoint.
Storing a template :
curl -XPUT 'http://localhost:9200/_search/template/1a' -d '
{
  "template": {
    "query": { "match_all": {}},
    "size": "{{my_size}}"
  }
}'
Deleting a template:
curl -XDELETE 'http://localhost:9200/_search/template/1a'
Getting a template:
curl -XGET 'http://localhost:9200/_search/template/1a'
Using an indexed template:
curl -XGET 'http://localhost:9200/_search/template' -d '
{
  "template":
  {
    "id" : "1a"
  },
  "params": {
    "my_size": 100
  }
}'
Or more simply:
curl -XGET 'http://localhost:9200/_search/template' -d '
{
  "id" : "1a",
  "params": {
    "my_size": 100
  }
}'
To access on disk templates in template queries the file parameter must now be used. This does break backwards compatiblity with previous versions
since the template "query" was previously aliased to dynamic inline templates or on disk templates.

See elastic#5637
  • Loading branch information
GaelTadh committed Jul 1, 2014
1 parent 40bba77 commit fb20079
Show file tree
Hide file tree
Showing 24 changed files with 1,015 additions and 113 deletions.
21 changes: 21 additions & 0 deletions rest-api-spec/api/indexed_template.create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"indexed_template.create": {
"documentation": "http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/indexed-scripts.html",
"methods": ["PUT", "POST"],
"url": {
"path": "/_search/template/{id}",
"paths": [ "/_search/template/{id}" ],
"parts": {
"id": {
"type" : "string",
"description" : "Document ID",
"required" : true
}
}
},
"body": {
"description" : "The document",
"required" : true
}
}
}
17 changes: 17 additions & 0 deletions rest-api-spec/api/indexed_template.delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"indexed_template.delete": {
"documentation": "http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/indexed-scripts.html",
"methods": ["DELETE"],
"url": {
"path": "/_search/template/{id}",
"paths": [ "/_search/template/{id}" ],
"parts": {
"id": {
"type" : "string",
"description" : "Document ID"
}
}
},
"body": null
}
}
20 changes: 20 additions & 0 deletions rest-api-spec/api/indexed_template.get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"indexed_template.get": {
"documentation": "http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/indexed-scripts.html",
"methods": ["GET"],
"url": {
"path": "/_search/template/{id}",
"paths": [ "/_search/template/{id}" ],
"parts": {
"id": {
"type" : "string",
"description" : "Document ID"
}
}
},
"body": {
"description" : "The document",
"required" : false
}
}
}
21 changes: 21 additions & 0 deletions rest-api-spec/test/template/10_basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"Indexed template":

- do:
indexed_template.create:
id: "1"
body: { "template": { "query": { "match_all": {}}, "size": "{{my_size}}" } }
- match: { _id: "1" }

- do:
indexed_template.get:
id: 1
- match: { template: "{\"query\":{\"match_all\":{}},\"size\":\"{{my_size}}\"}"}

- do:
indexed_template.delete:
id: 1
- match: { found: true }
- match: { _index: ".scripts" }
- match: { _id: "1" }

3 changes: 3 additions & 0 deletions src/main/java/org/elasticsearch/action/ActionModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@
import org.elasticsearch.action.suggest.SuggestAction;
import org.elasticsearch.action.suggest.TransportSuggestAction;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.action.template.get.GetSearchTemplatesAction;
import org.elasticsearch.action.template.get.TransportGetSearchTemplatesAction;
import org.elasticsearch.action.termvector.*;
import org.elasticsearch.action.update.TransportUpdateAction;
import org.elasticsearch.action.update.UpdateAction;
Expand Down Expand Up @@ -285,6 +287,7 @@ protected void configure() {
registerAction(BenchmarkAction.INSTANCE, TransportBenchmarkAction.class);
registerAction(AbortBenchmarkAction.INSTANCE, TransportAbortBenchmarkAction.class);
registerAction(BenchmarkStatusAction.INSTANCE, TransportBenchmarkStatusAction.class);
registerAction(GetSearchTemplatesAction.INSTANCE, TransportGetSearchTemplatesAction.class);

// register Name -> GenericAction Map that can be injected to instances.
MapBinder<String, GenericAction> actionsBinder
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/elasticsearch/action/index/IndexRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public static OpType fromId(byte id) {
throw new ElasticsearchIllegalArgumentException("No type match for [" + id + "]");
}
}

public static OpType fromString(String sOpType) throws ElasticsearchIllegalArgumentException {
if (Strings.hasLength(sOpType)){
if ("index".equals(sOpType)) {
return INDEX;
} else if ("create".equals(sOpType)) {
return CREATE;
}
}
throw new ElasticsearchIllegalArgumentException("opType [" + sOpType + "] not allowed, either [index] or [create] are allowed");
}

}

private String type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.elasticsearch.action.template;
/*
* 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.
*/



import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.GenericAction;
import org.elasticsearch.client.Client;

/**
* Indices action (used with {@link org.elasticsearch.client.Client} API.
*/
public abstract class TemplateAction<Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>>
extends GenericAction<Request, Response> {

protected TemplateAction(String name) {
super(name);
}

public abstract RequestBuilder newRequestBuilder(Client client);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.template.get;

import org.elasticsearch.action.template.TemplateAction;
import org.elasticsearch.client.Client;

/**
*
*/
public class GetSearchTemplatesAction extends TemplateAction<GetSearchTemplatesRequest, GetSearchTemplatesResponse, GetSearchTemplatesRequestBuilder> {

public static final GetSearchTemplatesAction INSTANCE = new GetSearchTemplatesAction();
public static final String NAME = "template/get";

protected GetSearchTemplatesAction() {
super(NAME);
}

@Override
public GetSearchTemplatesResponse newResponse() {
return new GetSearchTemplatesResponse();
}

@Override
public GetSearchTemplatesRequestBuilder newRequestBuilder(Client client) {
return new GetSearchTemplatesRequestBuilder(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.template.get;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

import java.io.IOException;

import static org.elasticsearch.action.ValidateActions.addValidationError;

/**
* Request that allows to retrieve index templates
*/
public class GetSearchTemplatesRequest extends MasterNodeReadOperationRequest<GetSearchTemplatesRequest> {

private String[] ids;

public GetSearchTemplatesRequest() {
}

public GetSearchTemplatesRequest(String... ids) {
this.ids = ids;
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
if (ids == null) {
validationException = addValidationError("ids is null or empty", validationException);
} else {
for (String name : ids) {
if (name == null || !Strings.hasText(name)) {
validationException = addValidationError("name is missing", validationException);
}
}
}
return validationException;
}

/**
* Sets the ids of the search templates.
*/
public GetSearchTemplatesRequest names(String... ids) {
this.ids = ids;
return this;
}

/**
* The ids of the search templates.
*/
public String[] ids() {
return this.ids;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
ids = in.readStringArray();
readLocal(in, Version.V_1_0_0_RC2);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeStringArray(ids);
writeLocal(out, Version.V_1_0_0_RC2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.template.get;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.internal.InternalGenericClient;
import org.elasticsearch.client.internal.InternalIndicesAdminClient;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.script.ScriptService;

/**
*
*/
public class GetSearchTemplatesRequestBuilder extends MasterNodeReadOperationRequestBuilder<GetSearchTemplatesRequest, GetSearchTemplatesResponse, GetSearchTemplatesRequestBuilder> {

Client searchClient;
ESLogger logger = Loggers.getLogger(GetSearchTemplatesRequestBuilder.class);
public GetSearchTemplatesRequestBuilder(Client client) {
super((InternalGenericClient) client, new GetSearchTemplatesRequest());
this.searchClient = client;
}

public GetSearchTemplatesRequestBuilder(Client client, String... ids) {
super((InternalGenericClient) client, new GetSearchTemplatesRequest(ids));
this.searchClient = client;
}

@Override
protected void doExecute(ActionListener<GetSearchTemplatesResponse> listener) {
GetSearchTemplatesResponse response = null;
try {
String script = ScriptService.getScriptFromIndex(searchClient, ScriptService.SCRIPT_INDEX, "mustache", request.ids()[0]);
response = new GetSearchTemplatesResponse(script);
} catch( ElasticsearchException ee ){
logger.error("Failed to find " + ee.toString());
}
listener.onResponse(response);
}
}

0 comments on commit fb20079

Please sign in to comment.