简体   繁体   中英

How to update an index with new variables in Elasticsearch?

I have an index 'user' which has a mapping with field of "first", "last", and "email". The fields with names get indexed at one point, and then the field with the email gets indexed at a separate point. I want these indices to have the same id though, corresponding to one user_id parameter. So something like this:

function indexName(client, id, name) {
  return client.update({
    index: 'user',
    type: 'text',
    id: id,
    body: {
      first: name.first
      last: name.last 
    }
  })
}

function indexEmail(client, id, email) { 
  return client.update({
    index: 'user',
    type: 'text',
    id: id,
    body: {
      email: email
    }
  })
}

When running:

indexName(client, "Jon", "Snow").then(indexEmail(client, "jonsnow@gmail.com"))

I get an error message saying that the document has not been created yet. How do I account for a document with a variable number of fields? And how do I create the index if it has not been created and then subsequently update it as I go?

The function you are using, client.update , updates part of a document . What you actually needs is to first create the document using the client.create function . To create and index, you need the indices.create function .

About the variable number of fields in a document type, it is not a problem because Elastic Search support dynamic mapping . However, it would be advisable to provide a mapping when creating the index, and try to stick to it. Elastic Search default mapping can create you problems later on, eg analyzing uuids or email addresses which then become difficult (or impossible) to search and match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM