forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mustache templating to query execution.
Adds support for storing mustache based query templates that can later be filled with query parameter values at execution time. Relates to elastic#4879
- Loading branch information
Isabel Drost-Fromm
committed
Feb 14, 2014
1 parent
d335630
commit 6725fc8
Showing
12 changed files
with
692 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/org/elasticsearch/index/query/TemplateQueryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 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.index.query; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
/** | ||
* Facilitates creating template query requests. | ||
* */ | ||
public class TemplateQueryBuilder extends BaseQueryBuilder { | ||
|
||
/** Parameters to fill the template with. */ | ||
private Map<String, Object> vars; | ||
/** Template to fill.*/ | ||
private String template; | ||
|
||
/** | ||
* @param template the template to use for that query. | ||
* @param vars the parameters to fill the template with. | ||
* */ | ||
public TemplateQueryBuilder(String template, Map<String, Object> vars) { | ||
this.template = template; | ||
this.vars = vars; | ||
} | ||
|
||
@Override | ||
protected void doXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(TemplateQueryParser.NAME); | ||
builder.field(TemplateQueryParser.STRING, template); | ||
builder.field(TemplateQueryParser.VARS, vars); | ||
builder.endObject(); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/main/java/org/elasticsearch/index/query/TemplateQueryParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* 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.index.query; | ||
|
||
import java.io.IOException; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.script.ExecutableScript; | ||
import org.elasticsearch.script.ScriptService; | ||
|
||
/** | ||
* In the simplest case, parse template string and variables from the request, compile the template and | ||
* execute the template against the given variables. | ||
* | ||
* TODO support named templates | ||
* */ | ||
public class TemplateQueryParser implements QueryParser { | ||
|
||
/** Name to reference this type of query. */ | ||
public static final String NAME = "template"; | ||
/** Name of query parameter containing the template string. */ | ||
public static final String STRING = "template_string"; | ||
/** Name of query parameter containing the template parameters. */ | ||
public static final String VARS = "template_vars"; | ||
/** This is what we are registered with for query executions. */ | ||
private final ScriptService scriptService; | ||
|
||
/** | ||
* @param scriptService will automatically be wired by Guice | ||
* */ | ||
@Inject | ||
public TemplateQueryParser(ScriptService scriptService) { | ||
this.scriptService = scriptService; | ||
} | ||
|
||
/** | ||
* @return a list of names this query is registered under. | ||
* */ | ||
@Override | ||
public String[] names() { | ||
return new String[] {NAME}; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public Query parse(QueryParseContext parseContext) throws IOException { | ||
XContentParser parser = parseContext.parser(); | ||
|
||
String template = ""; | ||
Map<String, Object> vars = new HashMap<String, Object>(); | ||
|
||
String currentFieldName = null; | ||
XContentParser.Token token; | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
currentFieldName = parser.currentName(); | ||
} else if (STRING.equals(currentFieldName)) { | ||
template = (String) parser.objectText(); | ||
} else if (VARS.equals(currentFieldName)) { | ||
XContentParser.Token innerToken; | ||
String key = ""; | ||
while ((innerToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
// parsing template parameter map | ||
if (innerToken == XContentParser.Token.FIELD_NAME) { | ||
key = parser.currentName(); | ||
} else { | ||
vars.put(key, parser.objectText()); | ||
key = ""; | ||
} | ||
} | ||
} | ||
} | ||
|
||
ExecutableScript executable = this.scriptService.executable("mustache", template, vars); | ||
String querySource = (String) executable.run(); | ||
|
||
XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource); | ||
try { | ||
final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParser); | ||
context.reset(qSourceParser); | ||
Query result = context.parseInnerQuery(); | ||
parser.nextToken(); | ||
return result; | ||
} finally { | ||
qSourceParser.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/main/java/org/elasticsearch/script/mustache/MustacheScriptEngineService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/** | ||
* 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.script.mustache; | ||
|
||
import com.fasterxml.jackson.core.io.SegmentedStringWriter; | ||
import com.fasterxml.jackson.core.util.BufferRecycler; | ||
import com.github.mustachejava.DefaultMustacheFactory; | ||
import com.github.mustachejava.Mustache; | ||
import com.github.mustachejava.MustacheFactory; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.component.AbstractComponent; | ||
import org.elasticsearch.common.inject.Inject; | ||
import org.elasticsearch.common.io.FastStringReader; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.script.ExecutableScript; | ||
import org.elasticsearch.script.ScriptEngineService; | ||
import org.elasticsearch.script.SearchScript; | ||
import org.elasticsearch.search.lookup.SearchLookup; | ||
|
||
import java.io.StringWriter; | ||
import java.util.Map; | ||
|
||
/** | ||
* Main entry point handling template registration, compilation and | ||
* execution. | ||
* | ||
* Template handling is based on Mustache. Template handling is a two step | ||
* process: First compile the string representing the template, the resulting | ||
* {@link Mustache} object can then be re-used for subsequent executions. | ||
*/ | ||
public class MustacheScriptEngineService extends AbstractComponent implements ScriptEngineService { | ||
|
||
/** Factory to generate Mustache objects from. */ | ||
private static final MustacheFactory MFACTORY = new DefaultMustacheFactory(); | ||
|
||
/** | ||
* @param settings automatically wired by Guice. | ||
* */ | ||
@Inject | ||
public MustacheScriptEngineService(Settings settings) { | ||
super(settings); | ||
} | ||
|
||
/** | ||
* Compile a template string to (in this case) a Mustache object than can | ||
* later be re-used for execution to fill in missing parameter values. | ||
* | ||
* @param template | ||
* a string representing the template to compile. | ||
* @return a compiled template object for later execution. | ||
* */ | ||
public Object compile(String template) { | ||
return MFACTORY.compile(new FastStringReader(template), "query-template"); | ||
} | ||
|
||
/** | ||
* Execute a compiled template object (as retrieved from the compile method) | ||
* and fill potential place holders with the variables given. | ||
* | ||
* @param template | ||
* compiled template object. | ||
* @param vars | ||
* map of variables to use during substitution. | ||
* | ||
* @return the processed string with all given variables substitued. | ||
* */ | ||
public Object execute(Object template, Map<String, Object> vars) { | ||
SegmentedStringWriter result = new SegmentedStringWriter(new BufferRecycler()); | ||
((Mustache) template).execute(result, vars); | ||
return result.getAndClear(); | ||
} | ||
|
||
@Override | ||
public String[] types() { | ||
return new String[] {"mustache"}; | ||
} | ||
|
||
@Override | ||
public String[] extensions() { | ||
return new String[] {"mustache"}; | ||
} | ||
|
||
@Override | ||
public ExecutableScript executable(Object mustache, | ||
@Nullable Map<String, Object> vars) { | ||
return new MustacheExecutableScript((Mustache) mustache, vars); | ||
} | ||
|
||
@Override | ||
public SearchScript search(Object compiledScript, SearchLookup lookup, | ||
@Nullable Map<String, Object> vars) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object unwrap(Object value) { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// Nothing to do here | ||
} | ||
|
||
/** | ||
* Used at query execution time by script service in order to execute a query template. | ||
* */ | ||
private class MustacheExecutableScript implements ExecutableScript { | ||
/** Compiled template object. */ | ||
private Mustache mustache; | ||
/** Parameters to fill above object with. */ | ||
private Map<String, Object> vars; | ||
|
||
/** | ||
* @param mustache the compiled template object | ||
* @param vars the parameters to fill above object with | ||
**/ | ||
public MustacheExecutableScript(Mustache mustache, | ||
Map<String, Object> vars) { | ||
this.mustache = mustache; | ||
this.vars = vars; | ||
} | ||
|
||
@Override | ||
public void setNextVar(String name, Object value) { | ||
this.vars.put(name, value); | ||
} | ||
|
||
@Override | ||
public Object run() { | ||
StringWriter result = new StringWriter(); | ||
((Mustache) mustache).execute(result, vars); | ||
return result.toString(); | ||
} | ||
|
||
@Override | ||
public Object unwrap(Object value) { | ||
return value; | ||
} | ||
} | ||
} |
Oops, something went wrong.