弹性搜索:字段上的预期数字类型

时间:2017-06-19 08:56:45

标签: elasticsearch

我需要计算SUM的所有字段都是INT但是当我发出请求时我得到了这个错误:

{
    "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
}

请求:

{
  "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"
      }
    }
  }
}

此处没有任何聚合的索引:

{
                "_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"
                }
            },

这里是我的索引的映射:

{
    "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"
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

正如您的例外所说,"reason": "Expected numeric type on field [mlf16_txservnum], but got [string]",它清楚地表明mlf16_txservnum是字符串但数字是预期的,因为聚合sum只能应用于数字,而不是字符串,并且在您的映射中,已将mlf16_txservnum的数据类型指定为字符串。

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

将地图中的类型更改为此字段的数字(整数,双数等)以及执行求和的其他字段

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

答案 1 :(得分:1)

我认为@RA KA解释了这个问题并解决了问题。但还有另一种解决方法。如果您的数据很大并且无法重新索引,则可以在运行时更改值的数据类型。

"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)'
  }
 }
}

希望这有助于......!

相关问题