I have a use case where I have to fire aggregation on a field having string as its data type. But that field contains numeric as well as alpha-numeric values.
{
"_index": "website",
"_type": "blog1",
"_id": "2",
"_score": 1,
"_source": {
"title": "Title",
"text": "Just trying this out...",
**"linenum": "20"**
}
},
{
"_index": "website",
"_type": "blog1",
"_id": "4",
"_score": 1,
"_source": {
"title": "Title",
"text": "Just trying this out...",
**"linenum": "10892AC"**
}
},
{
"_index": "website",
"_type": "blog1",
"_id": "1",
"_score": 1,
"_source": {
"title": "Title",
"text": "Just trying this out...",
**"linenum": "10"**
}
}
Is there any way where I can get sum of linenum field ignoring the alphanumeric value.
I have tried using script in the query as well.
{
"query": {
"match_all": {}
},
"fields": "*",
"aggs" : {
"linenum_Sum" : {
"sum" : {
"script" : "Float.parseFloat(doc['linenum'].value)" }
}
}
}
But even this query will give me only 10 in the result including script exception.
{
"shard": 2,
"index": "website",
"node": "pHlqkKB5QMi8vy4TUY-Cog",
"reason": {
"type": "script_exception",
"reason": "failed to run inline script [Float.parseFloat(doc['linenum'].value)] using lang [groovy]",
"caused_by": {
"type": "number_format_exception",
"reason": "For input string: \"ac\""
}
}
}