I made a small test which will, maybe, tell you more about the problem
void Main()
{
var uri = new List<Uri> {
new Uri("http://xxx.xxx.xxx.xxx"),
new Uri("http://xxx.xxx.xxx.xxx"),
new Uri("http://xxx.xxx.xxx.xxx"),
new Uri("http://xxx.xxx.xxx.xxx"),
new Uri("http://xxx.xxx.xxx.xxx")
};
var connectionPool = new SniffingConnectionPool(uri);
var settings = new ConnectionSettings(connectionPool, sourceSerializer: JsonNetSerializer.Default)
.PrettyJson()
.DisableDirectStreaming()
.DefaultIndex("test_date_parser");
var elastic = new ElasticClient(settings);
List<Test> toInsert = new List<Test> {
new Test {ToReturn = 25.2M, DateFrom = new DateTime(2018,03,16), DateTo = new DateTime(2018,04,16)},
new Test {ToReturn = 35.2M, DateFrom = new DateTime(2018,02,16), DateTo = new DateTime(2018,04,16)},
new Test {ToReturn = 45.2M, DateFrom = new DateTime(2018,04,16), DateTo = new DateTime(2018,05,16)}
};
toInsert.ForEach(x => elastic.Index(x, i => i.Type("test")));
// this work
var indexResponse = elastic.Search<Test>(s => s
.Query(q => q
.MatchAll()
)
.ScriptFields(sf => sf
.ScriptField("testCalculation", sc => sc
.Source(@"
DateTimeFormatter fmtr = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDate paramsFromParsed = LocalDate.parse(params.from, fmtr);
return paramsFromParsed;
").Lang("painless").Params(p => p
.Add("from", new DateTime(2018, 03, 16)))))
).Dump();
//this doesnt
var indexResponse2 = elastic.SearchTemplate<Test>(st => st
.Id("test_date_parser_template")
.Params(p => p
.Add("from", new DateTime(2018, 03, 16)))).Dump();
}
public class Test
{
public decimal ToReturn {get; set;}
public DateTime DateFrom {get; set;}
public DateTime DateTo {get; set;}
}
Response in case of indexResponse one is ok but as soon as i store this script as a search template and try to use it i will get DateTime exception:
# Response:
{
"error" : {
"root_cause" : [
{
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... ocalDate.parse(2018-03-16T00:00:00, fmtr);return p ...",
" ^---- HERE"
],
"script" : "DateTimeFormatter fmtr = DateTimeFormatter.ISO_LOCAL_DATE_TIME;LocalDate paramsFromParsed = LocalDate.parse(2018-03-16T00:00:00, fmtr);return paramsFromParsed;",
"lang" : "painless"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "test_date_parser",
"node" : "----",
"reason" : {
"type" : "script_exception",
"reason" : "compile error",
"script_stack" : [
"... ocalDate.parse(2018-03-16T00:00:00, fmtr);return p ...",
" ^---- HERE"
],
"script" : "DateTimeFormatter fmtr = DateTimeFormatter.ISO_LOCAL_DATE_TIME;LocalDate paramsFromParsed = LocalDate.parse(2018-03-16T00:00:00, fmtr);return paramsFromParsed;",
"lang" : "painless",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "invalid sequence of tokens near ['T00'].",
"caused_by" : {
"type" : "no_viable_alt_exception",
"reason" : "no_viable_alt_exception: null"
}
}
}
}
]
},
"status" : 500
}
Search Template:
POST _scripts/test_date_parser_template
{
"script": {
"lang": "mustache",
"source":{
"query":{
"match_all": {}
},
"script_fields": {
"testCalculation": {
"script": {
"lang": "painless",
"source": "DateTimeFormatter fmtr = DateTimeFormatter.ISO_LOCAL_DATE_TIME;LocalDate paramsFromParsed = LocalDate.parse({{from}}, fmtr);return paramsFromParsed;"
}
}
}
}
}
}
Am i missing something here ?