【问题标题】:Is there a way to Index a doc to Elasticsearch with a specific _id filed?有没有办法将具有特定 _id 归档的文档索引到 Elasticsearch?
【发布时间】:2020-09-06 17:52:41
【问题描述】:

我希望模拟一种状态,即我在索引中有一个特定的 _id 字段。

假设我想在我的示例中从 index1 获取完全相同的日志并将其索引到 index2。

像这样:

这是我的index1

{
   _index: "index-number-one",
   _type: "doc",
   _id: "S0meSpec!f!cID",
   _score: 1,
   _source: {
      message: "message1",
      type: "type1",
      tags: [
         "_bla"],
      number: 3
   }
}

现在我希望在我的 index2

中使用完全相同的日志
{
   _index: "index-number-two",
   _type: "doc",
   _id: "S0meSpec!f!cID",
   _score: 1,
   _source: {
      message: "message1",
      type: "type1",
      tags: [
         "_bla"],
      number: 3
   }
}

在 Elasticsearch 中找不到可以将文档插入到具有特定 _id 字段的索引的 API...(?)

如果无法执行此操作以使 Elasticsearch 集群不会在 _id 字段中出现重复项,我可以想象这是因为他们希望保留通过文档的 _id 搜索文档的能力 需要唯一的字段,在这种情况下,假设我不介意从 index1 中删除整个文档(可能将其保存为我的代码中的某个变量) ,但最后,我需要 index2 中的文档,将 EXACT _id 作为 index1 曾经拥有.

如果有办法编辑现有的 _id 字段,它也可以解决我的问题。

谁能解释一下如何实现这个目标?

【问题讨论】:

  • meta 属性,您可以在索引文档时传递该属性。示例:我从 python 中这样做 JiraIndex(meta={'id': 'XYKAL1776reLAO'}, jira_num='AQPEO74', ....)

标签: api elasticsearch logging indexing


【解决方案1】:

绝对可以做到这一点。每个索引的 ID 都是唯一的,而不是每个集群。

检查reindex API,它将一个索引复制到另一个索引并保留文档ID

还可以使用 reindex 调用中的脚本更改 ID。

【讨论】:

    【解决方案2】:

    回答我自己, 我发现它可以在索引上的 POST 请求中完成,如下所示:

    POST twitter/test-index-1234/abctype/Som3Cust0mID
    {
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
    }
    

    ES 中的结果:

    {
    _index: "test-index-1234",
    _type: "abctype",
    _id: "Som3Cust0mID",
    _score: 1,
    _source: {
       user: "kimchy",
       post_date: "2009-11-15T14:12:12",
       message: "trying out Elasticsearch"
       }
    }
    

    【讨论】:

    • 您可能需要从您的网址中删除twitter
    • 这只是像 Elasticsearch 这样的主机 URL 写在他们的文档上的一个例子。
    猜你喜欢
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多