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

Yas*_*BES 1 elasticsearch

我需要计算总和的所有字段都为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
}
Run Code Online (Sandbox Code Playgroud)

这里的要求:

{
  "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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这里的索引没有任何聚合:

{
                "_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"
                }
            },
Run Code Online (Sandbox Code Playgroud)

这是我的索引的映射:

{
    "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"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ra *_* Ka 5

就像您的例外所说,,"reason": "Expected numeric type on field [mlf16_txservnum], but got [string]"它显然说mlf16_txservnum是字符串,但是应该是数字,因为聚合sum只能应用于数字,不能应用于字符串,并且在映射中,您已将数据类型指定mlf16_txservnum为。

"mlf16_txservnum": {
    "type": "string",
    "index": "not_analyzed"
}
Run Code Online (Sandbox Code Playgroud)

将此字段和您执行求和的其他字段的映射类型更改为数字(整数,双精度等):

"mlf16_txservnum": {
    "type": "integer"
}
Run Code Online (Sandbox Code Playgroud)