Skip to content

Commit

Permalink
Fix SearchRequest.templateParams so that it is a Map<String, Object> …
Browse files Browse the repository at this point in the history
…so that it can take more data-types than just strings, to support Arrays.
  • Loading branch information
reuben-sutton authored and GaelTadh committed Nov 24, 2014
1 parent 08bbfac commit fda1576
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
private boolean templateSourceUnsafe;
private String templateName;
private ScriptService.ScriptType templateType;
private Map<String, String> templateParams = Collections.emptyMap();
private Map<String, Object> templateParams = Collections.emptyMap();

private BytesReference source;
private boolean sourceUnsafe;
Expand Down Expand Up @@ -452,7 +452,7 @@ public void templateType(ScriptService.ScriptType templateType) {
/**
* Template parameters used for rendering
*/
public void templateParams(Map<String, String> params) {
public void templateParams(Map<String, Object> params) {
this.templateParams = params;
}

Expand All @@ -473,7 +473,7 @@ public ScriptService.ScriptType templateType() {
/**
* Template parameters used for rendering
*/
public Map<String, String> templateParams() {
public Map<String, Object> templateParams() {
return templateParams;
}

Expand Down Expand Up @@ -579,7 +579,7 @@ public void readFrom(StreamInput in) throws IOException {
templateType = ScriptService.ScriptType.readFrom(in);
}
if (in.readBoolean()) {
templateParams = (Map<String, String>) in.readGenericValue();
templateParams = (Map<String, Object>) in.readGenericValue();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ public SearchRequestBuilder setTemplateType(ScriptService.ScriptType templateTyp
return this;
}

public SearchRequestBuilder setTemplateParams(Map<String,String> templateParams) {
public SearchRequestBuilder setTemplateParams(Map<String,Object> templateParams) {
request.templateParams(templateParams);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
private BytesReference templateSource;
private String templateName;
private ScriptService.ScriptType templateType;
private Map<String, String> templateParams;
private Map<String, Object> templateParams;
private Boolean queryCache;

private long nowInMillis;
Expand Down Expand Up @@ -188,7 +188,7 @@ public ScriptService.ScriptType templateType() {
}

@Override
public Map<String, String> templateParams() {
public Map<String, Object> templateParams() {
return templateParams;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ protected void innerReadFrom(StreamInput in) throws IOException {
templateType = ScriptService.ScriptType.readFrom(in);
}
if (in.readBoolean()) {
templateParams = (Map<String, String>) in.readGenericValue();
templateParams = (Map<String, Object>) in.readGenericValue();
}
}
if (in.getVersion().onOrAfter(ParsedScrollId.SCROLL_SEARCH_AFTER_MINIMUM_VERSION)) {
Expand Down Expand Up @@ -302,4 +302,3 @@ public BytesReference cacheKey() throws IOException {
return out.bytes().copyBytesArray();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface ShardSearchRequest {

ScriptService.ScriptType templateType();

Map<String, String> templateParams();
Map<String, Object> templateParams();

BytesReference templateSource();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public ScriptService.ScriptType templateType() {
}

@Override
public Map<String, String> templateParams() {
public Map<String, Object> templateParams() {
return shardSearchLocalRequest.templateParams();
}

Expand Down
39 changes: 35 additions & 4 deletions src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void testThatParametersCanBeSet() throws Exception {
index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
refresh();

Map<String, String> templateParams = Maps.newHashMap();
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("mySize", "2");
templateParams.put("myField", "theField");
templateParams.put("myValue", "foo");
Expand Down Expand Up @@ -260,7 +260,7 @@ public void testIndexedTemplateClient() throws Exception {

indexRandom(true,builders);

Map<String, String> templateParams = Maps.newHashMap();
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("fieldParam", "foo");

SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
Expand Down Expand Up @@ -306,7 +306,6 @@ public void testIndexedTemplate() throws Exception {
" }" +
"}"));


indexRandom(true, builders);

builders.clear();
Expand All @@ -319,7 +318,7 @@ public void testIndexedTemplate() throws Exception {

indexRandom(true,builders);

Map<String, String> templateParams = Maps.newHashMap();
Map<String, Object> templateParams = Maps.newHashMap();
templateParams.put("fieldParam", "foo");

SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
Expand Down Expand Up @@ -368,4 +367,36 @@ public void testIndexedTemplate() throws Exception {
sr = client().prepareSearch().setQuery(query).get();
assertHitCount(sr, 4);
}

@Test
public void testIndexedTemplateWithArray() throws Exception {
createIndex(ScriptService.SCRIPT_INDEX);
ensureGreen(ScriptService.SCRIPT_INDEX);
List<IndexRequestBuilder> builders = new ArrayList<>();

String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";

builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, "mustache", "4").setSource(jsonBuilder().startObject().field("template", multiQuery).endObject()));

indexRandom(true,builders);

builders.clear();

builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}"));
builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}"));
builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}"));
builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}"));
builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}"));

indexRandom(true,builders);

Map<String, Object> arrayTemplateParams = new HashMap<>();
String[] fieldParams = {"foo","bar"};
arrayTemplateParams.put("fieldParam", fieldParams);

SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
setTemplateName("/mustache/4").setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(arrayTemplateParams).get();
assertHitCount(searchResponse, 5);
}

}

0 comments on commit fda1576

Please sign in to comment.