Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

Logstash add an event to an existing document in elasticsearch

I already have a document in Elasticsearch with id (let's say 1). I need to configure Logstash so that an event with the same id gets inserted into Elasticsearch.

Example document where an ID already exists:

{
  "name": "abc"
}

Logstash event:

{
  "address": "new york",
  "mobile no": "xxx"
}

The final result in Elasticsearch:

{
  "name": "abc",
  "address": "new york",
  "mobile no": "xxx"
}

I tried using update script in output plugin:

elasticsearch {
    action => "update"
    hosts => [ "localhost:9200" ]
    index => "details"
    scripted_upsert => true
    document_id => "%{id}"
    script => "ctx._source.name = params.event.get('name')"
}

This allows me to add each field (name, address, etc) but I need to insert the entire json event without specifying each field. How to do that?

question from:https://stackoverflow.com/questions/65934441/logstash-add-an-event-to-an-existing-document-in-elasticsearch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think it's way easier than you think ;)

Elasticsearch supports upserting: https://www.elastic.co/guide/en/logstash/current/plugins-outputs-elasticsearch.html#plugins-outputs-elasticsearch-doc_as_upsert

So combining action => update together with doc_as_upsert => true should do the job:

elasticsearch {
    action => "update"
    hosts => [ "localhost:9200" ]
    index => "details"
    doc_as_upsert => true
    document_id => "%{id}"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...