1

I am working on chat application, i need to push chat information to elasticsearch for specified index using nodejs.

I have cretaed index and push data to elasticsearch using below code.

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

client.ping({
  requestTimeout: 30000,

  // undocumented params are appended to the query string
  hello: "elasticsearch"
}, function (error) {
  if (error) {
    console.error('elasticsearch cluster is down!');
  } else {
    console.log('All is well');
  }
});



  var result = client.index({
    index: 'chennaiboxchat',
    type: 'posts',
    id: '1',
    body: {
      "glossary": {
        "title": "example Test",
        "GlossDiv": {
          "title": "S",
          "GlossList": {
            "GlossEntry": {
              "ID": "SGML",
              "SortAs": "SGML",
              "GlossTerm": "Standard Generalized Markup Language",
              "Acronym": "SGML",
              "Abbrev": "ISO 8879:1986",
              "GlossDef": {
                "para": "A meta-markup language, used to create markup languages such as DocBook.",
                "GlossSeeAlso": ["GML", "XML"]
              },
              "GlossSee": "mark"
            }
          }
        }
      }
    },
    refresh: true
  });

But, if I run this code next time overwrite the data.so, I need to Update the data in same index in elasticsearch using Nodejs. Suggest me how to update data in single index.

1 Answer 1

2

BY default you have to provide the complete document. So obtain the document using a get, change the fields you want to change and put the document back like you did before. There is an update api to provide only partial documents. This actually does the same, but elasticsearch does the get and put part for you. Often you want to do something with the version attribute if you really want to do this. More information can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_updates_with_a_partial_document

The javascript driver that node.js uses also supports the update endpoint. Check documentation here: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.