2017-03-06 25 views
1

請問我怎樣在轉換'字符串'字段時重新索引,例如「field2」:「123.2」(在舊的索引文件中)變成浮點/雙數,例如「field2」:123.2(打算在新索引中)?這篇文章是我能得到的最接近的,但我不知道哪個函數用於將字符串轉換爲數字。我正在使用ElasticSearch版本2.3.3。非常感謝您的任何意見!reindex同時轉換特定字段的字符串值(存在於舊索引中)爲數字字段值(在新索引中)

回答

0

使用Elasticsearch templates指定新索引的映射並將該字段指定爲雙精度型。

構建模板的最簡單方法是使用現有映射。

GET oldindex/_mapping 
POST _template/templatename 
{ 
    "template" : "newindex", // this can be a wildcard pattern to match indexes 
    "mappings": { // this is copied from the response of the previous call 
    "mytype": { 
     "properties": { 
     "field2": { 
      "type": "double" // change the type 
     } 
     } 
    } 
    } 
} 
POST newindex 
GET newindex/_mapping 

然後使用elasticsearch _reindex API從舊索引的數據移動到新的索引,並使用內嵌的腳本解析字段作爲雙(您可能需要enable inline scripting

POST _reindex 
{ 
    "source": { 
    "index": "oldindex" 
    }, 
    "dest": { 
    "index": "newindex" 
    }, 
    "script": { 
    "inline": "ctx._source.field2 = ctx._source.field2.toDouble()" 
    } 
} 

編輯:已更新爲使用_reindex端點

+0

非常感謝你masstroy。我的問題正如你所描述的那樣:「在掃描和滾動期間轉換json」。我發現使用google搜索的_reindex功能應該可以在_reindex請求查詢中創建一個新的腳本字段,但確切的語法或函數基本上找不到(根據我目前的經驗)。只是一個說明:我*不*使用logstash。 – leeway

+0

會像這樣[1]工作,如果我找到一個內嵌腳本的轉換函數,將字符串的值從字符串 - >浮動? [1] http://stackoverflow.com/questions/42423899/renaming-fields-to-new-index-in-elasticsearch/42425103#42425103 – leeway

+0

帖子最接近我在原始問題中提到的字符串 - >浮動轉換在這裏(對不起,以前沒有具體說明):http://stackoverflow.com/questions/30706361/convert-strings-to-floats-at-aggregation-time – leeway

0

您可以使用Logstash重新索引數據並轉換該字段。類似如下:

input { 
    elasticsearch { 
    hosts => "es.server.url" 
    index => "old_index" 
    query => "*" 
    size => 500 
    scroll => "5m" 
    docinfo => true 
    } 
} 

filter { 
    mutate { 
    convert => { "fieldname" => "long" } 
    } 
} 

output { 
    elasticsearch { 
    host => "es.server.url" 
    index => "new_index" 
    index_type => "%{[@metadata][_type]}" 
    document_id => "%{[@metadata][_id]}" 
    } 
} 
+0

謝謝!我不幸的是我沒有使用Logstash。 – leeway

相關問題