[英]Importing and updating data in Elasticsearch
我們有一個現有的搜索功能,它涉及SQL Server中多個表的數據。 這會導致我們的數據庫負載過重,所以我試圖找到一種更好的方法來搜索這些數據(它不會經常更改)。 我使用包含120萬條記錄的導入,一直在使用Logstash和Elasticsearch大約一周。 我的問題基本上是“如何使用我的'主鍵'更新現有文檔”?
CSV數據文件(管道分隔)如下所示:
369|90045|123 ABC ST|LOS ANGELES|CA
368|90045|PVKA0010|LA|CA
367|90012|20000 Venice Boulvd|Los Angeles|CA
365|90045|ABC ST 123|LOS ANGELES|CA
363|90045|ADHOCTESTPROPERTY|DALES|CA
我的logstash配置如下所示:
input {
stdin {
type => "stdin-type"
}
file {
path => ["C:/Data/sample/*"]
start_position => "beginning"
}
}
filter {
csv {
columns => ["property_id","postal_code","address_1","city","state_code"]
separator => "|"
}
}
output {
elasticsearch {
embedded => true
index => "samples4"
index_type => "sample"
}
}
elasticsearch中的文檔,如下所示:
{
"_index": "samples4",
"_type": "sample",
"_id": "64Dc0_1eQ3uSln_k-4X26A",
"_score": 1.4054651,
"_source": {
"message": [
"369|90045|123 ABC ST|LOS ANGELES|CA\r"
],
"@version": "1",
"@timestamp": "2014-02-11T22:58:38.365Z",
"host": "[host]",
"path": "C:/Data/sample/sample.csv",
"property_id": "369",
"postal_code": "90045",
"address_1": "123 ABC ST",
"city": "LOS ANGELES",
"state_code": "CA"
}
我想希望_id
字段中的唯一ID替換為property_id
的值。 這個想法是后續數據文件將包含更新。 我不需要保留以前的版本,也不會出現我們在文檔中添加或刪除密鑰的情況。
elasticsearch輸出的document_id
設置不會將該字段的值放入_id
(它只是放入“property_id”並且只存儲/更新一個文檔)。 我知道我在這里遺漏了一些東西。 我只是采取了錯誤的方法嗎?
編輯:工作!
使用@ rutter的建議,我已將output
配置更新為:
output {
elasticsearch {
embedded => true
index => "samples6"
index_type => "sample"
document_id => "%{property_id}"
}
}
現在通過按預期將新文件放入數據文件夾來更新文檔。 _id
和property_id
是相同的值。
{
"_index": "samples6",
"_type": "sample",
"_id": "351",
"_score": 1,
"_source": {
"message": [
"351|90045|Easy as 123 ST|LOS ANGELES|CA\r"
],
"@version": "1",
"@timestamp": "2014-02-12T16:12:52.102Z",
"host": "TXDFWL3474",
"path": "C:/Data/sample/sample_update_3.csv",
"property_id": "351",
"postal_code": "90045",
"address_1": "Easy as 123 ST",
"city": "LOS ANGELES",
"state_code": "CA"
}
從評論轉換:
您可以通過發送具有相同ID的其他文檔來覆蓋文檔...但這可能會對您以前的數據造成困難,因為默認情況下您將獲得隨機ID。
您可以使用輸出插件的document_id
字段設置ID,但它需要一個文字字符串,而不是字段名稱。 要使用字段的內容,可以使用sprintf格式字符串 ,例如%{property_id}
。
像這樣的東西,例如:
output {
elasticsearch {
... other settings...
document_id => "%{property_id}"
}
}
declaimer - 我是ESL的作者
您可以使用elasticsearch_loader將psv文件加載到elasticsearch中。
要設置_id字段,您可以使用--id-field = property_id。 例如:
elasticsearch_loader --index=myindex --type=mytype --id-field=property_id csv --delimiter='|' filename.csv
您是否嘗試將配置更改為:
filter {
csv {
columns => ["_id","postal_code","address_1","city","state_code"]
separator => "|"
}
}
通過將property_id命名為_id,它應該在索引期間使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.