如果小数点后的数字为零,则它不会返回为double值?

时间:2016-11-01 05:42:58

标签: elasticsearch elastic-stack elasticsearch-5

我在elasticsearch 5.0创建了一个索引,其中包含来自MySQL db的数据。我的表中有一个string字段,我需要它作为ES中的double

所以当我使用PUT创建适当字段的索引时,我所做的就是添加了映射:

{
  "mappings": {
    "my_type": {
      "properties": {      
        "chargeamount": {
          "type": "double"
        }
      }
    }
  }
}

在我这样做之后,包含十进制数后的数字的值(即:23.23)将值正确地返回为double,但是在小数点后面有零的数字(即:23.00)将其作为字符串本身返回(即:2300)。

修改: 这些是我做的步骤:

  1. 我最初通过PUT请求创建了索引 (http://hostmachine:9402/indexname)将上述映射作为 身体。

  2. 然后我将数据(从我的MySQL表)推送到索引     logstash。如果需要,我可以提供logstash conf。

  3. 将数据上传到索引后,我尝试查询,以检查结果是否显示为double值。 POST请求(http://hostmachine:9402/indexname/_search?和正文如下:

    {  
            "size" : 0,
           "query":{  
              "query_string":{  
                 "query":"myquery"
              }
           },
           "aggs":{  
              "total":{  
                 "terms":{  
                    "field":"userid"
                 },
                 "aggs":{  
                    "total":{  
                       "sum":{  
                           "script":{
                            "lang": "painless",
                            "inline" : "doc['chargeamount'].value"
                           }
                       }
                    }
                 }
              }
           }
        }
    
  4. 结果如下面的快照所示,它应该是 267472.00

    enter image description here

    我哪里错了?任何帮助都可以得到赞赏。

1 个答案:

答案 0 :(得分:1)

您需要确保索引创建查询中的映射类型与您在logstash配置中的public static String replaceChar(String word, char c, int i) { String ret=""; if(i<0 || word.length()<i) { System.out.println(word); return word; } ret= word.substring(0, i) + c; if(word.length() > i+1){ ret=ret+word.substring(i+1); } return ret; } 完全相同,即document_type

message_logs
相关问题