YasBES
YasBES

Reputation: 2325

Elastic Search : Expected numeric type on field

All the fields that i need to calculate the SUM are INT but when i make the request i got this error :

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Expected numeric type on field [mlf16_txservnum], but got [string]"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "bpa_stag_v3",
                "node": "zzsbvdwmQ0-d0Ca_b3w0uQ",
                "reason": {
                    "type": "illegal_argument_exception",
                    "reason": "Expected numeric type on field [mlf16_txservnum], but got [string]"
                }
            }
        ]
    },
    "status": 400
}

Here the request :

{
  "size" : 0,
  "query" : {
    "bool" : {
      "filter" : [ {
        "term" : {
          "mlf16_cptfou_six" : "095436"
        }
      }, {
        "term" : {
          "mlf16_codadres" : "00"
        }
      }, {
        "term" : {
          "mlf16_semind" : "24"
        }
      }, {
        "term" : {
          "mlf16_annee" : "2017"
        }
      } ]
    }
  },
  "aggregations" : {
    "Sum_Service_Rate_Numerator" : {
      "sum" : {
        "field" : "mlf16_txservnum"
      }
    },
    "Sum_Service_Rate_Denominator" : {
      "sum" : {
        "field" : "mlf16_txservden"
      }
    }
  }
}

Here the index without any aggregation :

{
                "_index": "bpa_stag_v3",
                "_type": "indic_semaine_yassine",
                "_id": "2017-06-11,2017,23,23,0000900156,05,0000241235,00,00,5I,3,0,19,0,2,0,0,3,2,2,0,12,3,241235,2017",
                "_score": 1,
                "_source": {
                    "dt_extract": "2017-06-11",
                    "mlf16_annee": "2017",
                    "mlf16_semind": "23",
                    "mlf16_semcal": "23",
                    "mlf16_cptmag": "0000900156",
                    "mlf16_codraft": "05",
                    "mlf16_cptfou": "0000241235",
                    "mlf16_codadrdl": "00",
                    "mlf16_codadres": "00",
                    "mlf16_grpges": "5I",
                    "mlf16_clasges": "3",
                    "mlf16_txservnum": 0,
                    "mlf16_txservden": 19,
                    "mlf16_txdocnum": 0,
                    "mlf16_txdocden": 2,
                    "mlf16_txfiabnum": 0,
                    "mlf16_txfiabden": 0,
                    "mlf16_refret2j": 3,
                    "mlf16_refret7j": 2,
                    "mlf16_refret21j": 2,
                    "mlf16_natcouv": "0",
                    "mlf16_nbligndiff": 12,
                    "mlf16_nbrefdiff": 3,
                    "mlf16_cptfou_six": "241235",
                    "dt_year": 2017,
                    "es_id": "2017-06-11,2017,23,23,0000900156,05,0000241235,00,00,5I,3,0,19,0,2,0,0,3,2,2,0,12,3,241235,2017"
                }
            },

Here the mapping of my index :

{
    "bpa_stag_v3": {
        "mappings": {
            "indic_semaine_yassine": {
                "dynamic_templates": [
                    {
                        "string_fields": {
                            "mapping": {
                                "index": "not_analyzed",
                                "omit_norms": true,
                                "type": "string"
                            },
                            "match": "*",
                            "match_mapping_type": "string"
                        }
                    }
                ],
                "properties": {
                    "dt_extract": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "dt_year": {
                        "type": "long"
                    },
                    "es_id": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_annee": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_clasges": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_codadrdl": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_codadres": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_codraft": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_cptfou": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_cptfou_six": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_cptmag": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_grpges": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_natcouv": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_nbligndiff": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_nbrefdiff": {
                        "type": "date",
                        "format": "strict_date_optional_time||epoch_millis"
                    },
                    "mlf16_refret21j": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_refret2j": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_refret7j": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_semcal": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_semind": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txdocden": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txdocnum": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txfiabden": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txfiabnum": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txservden": {
                        "type": "string",
                        "index": "not_analyzed"
                    },
                    "mlf16_txservnum": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

Upvotes: 3

Views: 10674

Answers (2)

BlackPOP
BlackPOP

Reputation: 5747

I think @RA KA explained the issue and fix.But there is another way to fix as well. If your data is huge and can't reindex, You can change the data type of value on run time.

"aggregations" : {
 "Sum_Service_Rate_Numerator" : {
  "sum" : {
    "field" : 'Integer.parseInt(doc["mlf16_txservnum"].value)'
  }
},
"Sum_Service_Rate_Denominator" : {
  "sum" : {
    "field" : 'Integer.parseInt(doc["mlf16_txservden"].value)'
  }
 }
}

Hope this Helps..!

Upvotes: 3

Ra Ka
Ra Ka

Reputation: 3055

As your exception says, "reason": "Expected numeric type on field [mlf16_txservnum], but got [string]", it clearly says that mlf16_txservnum is string but numeric expected because aggregation sum can be only applied to numbers, not string and in your mapping, you have specified datatype of mlf16_txservnum to string.

"mlf16_txservnum": {
    "type": "string",
    "index": "not_analyzed"
}

Change the type in your mapping to number (integer, double etc.) for this field and other fields that you performing sum:

"mlf16_txservnum": {
    "type": "integer"
}

Upvotes: 6

Related Questions