简体   繁体   English

如何在Elasticsearch中使用新变量更新索引?

[英]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. 我希望这些索引具有相同的ID,对应于一个user_id参数。 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 . 您正在使用的函数client.update更新文档的一部分 What you actually needs is to first create the document using the client.create function . 您真正需要的是首先使用client.create函数创建文档。 To create and index, you need the indices.create function . 要创建索引和索引,您需要indices.create函数

About the variable number of fields in a document type, it is not a problem because Elastic Search support dynamic mapping . 关于文档类型中可变字段数,这不是问题,因为Elastic Search支持动态映射 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. 弹性搜索默认映射可能会在以后给您带来问题,例如分析uuid或电子邮件地址,然后变得难以(或不可能)搜索和匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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