Hi,
why does elastic (mustache) create a empty strings in search templates?
I've got sample data as follows:
Mappings:
PUT /movie_db
{"mappings": {"movie": {"properties": {"title": {"type": "string", "analyzer": "snowball"}, "actors": {"type": "string", "position_offset_gap" : 100, "analyzer": "standard"}, "genre": {"type": "string", "index": "not_analyzed"}, "release_year": {"type": "integer", "index": "not_analyzed"}, "description": {"type": "string", "analyzer": "snowball"}}}}}
Sample data:
POST movie_db/movie
{"title":"Hackers","release_year":1995,"genre":["Action","Crime","Drama"],"actors":["Johnny Lee Miller","Angelina Jolie"],"description":"High-school age computer expert Zero Cool and his hacker friends take on an evil corporation's computer virus with their hacking skills."}
I've added couple of duplicate records, just with different release_year
values.
Now when I create search template as follows:
POST /_search/template/testTemplate
{
"template": {
"query": {
"bool": {
"must": [
{
"terms": {
"release_year": ["{{#release_year}}","{{.}}","{{/release_year}}"]
}
}
]
}
}
}
}
And query it:
GET /movie_db/_search/template
{
"template": {
"id": "testTemplate"
},
"params": {
"release_year": ["1995","1998"]
}
}
I'm getting an error NumberFormatException. Mustache generates empty strings, which cause it.
{ "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[BiAcX5l7SfmwqBthXJ6S5g][movie_db][0]: SearchParseException[[movie_db][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"release_year\":[\"\",\"1\",\"\",\"2\",\"\"]}}]}}}]]]; nested: NumberFormatException[For input string: \"\"]; }{[BiAcX5l7SfmwqBthXJ6S5g][movie_db][1]: SearchParseException[[movie_db][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"release_year\":[\"\",\"1\",\"\",\"2\",\"\"]}}]}}}]]]; nested: NumberFormatException[For input string: \"\"]; }{[BiAcX5l7SfmwqBthXJ6S5g][movie_db][2]: SearchParseException[[movie_db][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"release_year\":[\"\",\"1\",\"\",\"2\",\"\"]}}]}}}]]]; nested: NumberFormatException[For input string: \"\"]; }{[BiAcX5l7SfmwqBthXJ6S5g][movie_db][3]: SearchParseException[[movie_db][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"release_year\":[\"\",\"1\",\"\",\"2\",\"\"]}}]}}}]]]; nested: NumberFormatException[For input string: \"\"]; }{[BiAcX5l7SfmwqBthXJ6S5g][movie_db][4]: SearchParseException[[movie_db][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"release_year\":[\"\",\"1\",\"\",\"2\",\"\"]}}]}}}]]]; nested: NumberFormatException[For input string: \"\"]; }]", "status": 400 }
NumberFormatException[For input string: ""]
{"release_year":["","1","","2",""]}
It looks like this query is rendered and empty string cannot be converted into number:
{
"query": {
"bool": {
"must": [
{
"terms": {
"release_year": ["","1995","1998",""]
}
}
]
}
}
}
How to avoid/fix that? I was following documentation. I've asked this question on SO, but solution is quite tricky/hacky
Please advice.