mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-06 04:58:50 +00:00
Fix SearchRequest.templateParams so that it is a Map<String, Object> so that it can take more data-types than just strings, to support Arrays.
This commit is contained in:
parent
08bbfac7eb
commit
fda1576d55
@ -74,7 +74,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
|
|||||||
private boolean templateSourceUnsafe;
|
private boolean templateSourceUnsafe;
|
||||||
private String templateName;
|
private String templateName;
|
||||||
private ScriptService.ScriptType templateType;
|
private ScriptService.ScriptType templateType;
|
||||||
private Map<String, String> templateParams = Collections.emptyMap();
|
private Map<String, Object> templateParams = Collections.emptyMap();
|
||||||
|
|
||||||
private BytesReference source;
|
private BytesReference source;
|
||||||
private boolean sourceUnsafe;
|
private boolean sourceUnsafe;
|
||||||
@ -452,7 +452,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
|
|||||||
/**
|
/**
|
||||||
* Template parameters used for rendering
|
* Template parameters used for rendering
|
||||||
*/
|
*/
|
||||||
public void templateParams(Map<String, String> params) {
|
public void templateParams(Map<String, Object> params) {
|
||||||
this.templateParams = params;
|
this.templateParams = params;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -473,7 +473,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
|
|||||||
/**
|
/**
|
||||||
* Template parameters used for rendering
|
* Template parameters used for rendering
|
||||||
*/
|
*/
|
||||||
public Map<String, String> templateParams() {
|
public Map<String, Object> templateParams() {
|
||||||
return templateParams;
|
return templateParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -579,7 +579,7 @@ public class SearchRequest extends ActionRequest<SearchRequest> implements Indic
|
|||||||
templateType = ScriptService.ScriptType.readFrom(in);
|
templateType = ScriptService.ScriptType.readFrom(in);
|
||||||
}
|
}
|
||||||
if (in.readBoolean()) {
|
if (in.readBoolean()) {
|
||||||
templateParams = (Map<String, String>) in.readGenericValue();
|
templateParams = (Map<String, Object>) in.readGenericValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -958,7 +958,7 @@ public class SearchRequestBuilder extends ActionRequestBuilder<SearchRequest, Se
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchRequestBuilder setTemplateParams(Map<String,String> templateParams) {
|
public SearchRequestBuilder setTemplateParams(Map<String,Object> templateParams) {
|
||||||
request.templateParams(templateParams);
|
request.templateParams(templateParams);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
|
|||||||
private BytesReference templateSource;
|
private BytesReference templateSource;
|
||||||
private String templateName;
|
private String templateName;
|
||||||
private ScriptService.ScriptType templateType;
|
private ScriptService.ScriptType templateType;
|
||||||
private Map<String, String> templateParams;
|
private Map<String, Object> templateParams;
|
||||||
private Boolean queryCache;
|
private Boolean queryCache;
|
||||||
|
|
||||||
private long nowInMillis;
|
private long nowInMillis;
|
||||||
@ -188,7 +188,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> templateParams() {
|
public Map<String, Object> templateParams() {
|
||||||
return templateParams;
|
return templateParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,7 +236,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
|
|||||||
templateType = ScriptService.ScriptType.readFrom(in);
|
templateType = ScriptService.ScriptType.readFrom(in);
|
||||||
}
|
}
|
||||||
if (in.readBoolean()) {
|
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)) {
|
if (in.getVersion().onOrAfter(ParsedScrollId.SCROLL_SEARCH_AFTER_MINIMUM_VERSION)) {
|
||||||
@ -302,4 +302,3 @@ public class ShardSearchLocalRequest implements ShardSearchRequest {
|
|||||||
return out.bytes().copyBytesArray();
|
return out.bytes().copyBytesArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ public interface ShardSearchRequest {
|
|||||||
|
|
||||||
ScriptService.ScriptType templateType();
|
ScriptService.ScriptType templateType();
|
||||||
|
|
||||||
Map<String, String> templateParams();
|
Map<String, Object> templateParams();
|
||||||
|
|
||||||
BytesReference templateSource();
|
BytesReference templateSource();
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ public class ShardSearchTransportRequest extends TransportRequest implements Sha
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> templateParams() {
|
public Map<String, Object> templateParams() {
|
||||||
return shardSearchLocalRequest.templateParams();
|
return shardSearchLocalRequest.templateParams();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
|
index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject());
|
||||||
refresh();
|
refresh();
|
||||||
|
|
||||||
Map<String, String> templateParams = Maps.newHashMap();
|
Map<String, Object> templateParams = Maps.newHashMap();
|
||||||
templateParams.put("mySize", "2");
|
templateParams.put("mySize", "2");
|
||||||
templateParams.put("myField", "theField");
|
templateParams.put("myField", "theField");
|
||||||
templateParams.put("myValue", "foo");
|
templateParams.put("myValue", "foo");
|
||||||
@ -260,7 +260,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
|
|
||||||
indexRandom(true,builders);
|
indexRandom(true,builders);
|
||||||
|
|
||||||
Map<String, String> templateParams = Maps.newHashMap();
|
Map<String, Object> templateParams = Maps.newHashMap();
|
||||||
templateParams.put("fieldParam", "foo");
|
templateParams.put("fieldParam", "foo");
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
|
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
|
||||||
@ -306,7 +306,6 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
" }" +
|
" }" +
|
||||||
"}"));
|
"}"));
|
||||||
|
|
||||||
|
|
||||||
indexRandom(true, builders);
|
indexRandom(true, builders);
|
||||||
|
|
||||||
builders.clear();
|
builders.clear();
|
||||||
@ -319,7 +318,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
|
|
||||||
indexRandom(true,builders);
|
indexRandom(true,builders);
|
||||||
|
|
||||||
Map<String, String> templateParams = Maps.newHashMap();
|
Map<String, Object> templateParams = Maps.newHashMap();
|
||||||
templateParams.put("fieldParam", "foo");
|
templateParams.put("fieldParam", "foo");
|
||||||
|
|
||||||
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
|
SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").
|
||||||
@ -368,4 +367,36 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest {
|
|||||||
sr = client().prepareSearch().setQuery(query).get();
|
sr = client().prepareSearch().setQuery(query).get();
|
||||||
assertHitCount(sr, 4);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user