【问题标题】:Auto Increment a field value every time a doc is inserted in elastic search每次在弹性搜索中插入文档时自动增加字段值
【发布时间】:2018-08-26 07:38:18
【问题描述】:

我需要生成此格式的唯一编号 (ARN)

DD/MM/YYYY/1, DD/MM/YYYY/2

并将它们插入弹性搜索索引中。

我正在考虑的方法是在文档中创建一个自动增量字段并使用它来生成一个新条目并使用新的自动生成的数字来创建 ARN 并更新文档。

我打算使用的文档结构:

{ id: 1, arn: 17/03/2018/01 }

类似的东西。

如何在弹性搜索中获取自动增量字段?

【问题讨论】:

  • 你使用哪种编程语言来实现这个?
  • @NIKHILNEDIYODATH NodeJs
  • 试试看能不能用插件解决。但从长远来看,再次扩展性能可能是一个问题。

标签: node.js elasticsearch elasticsearch-5


【解决方案1】:

不可能一步完成。首先,您必须将记录插入数据库,然后使用它的 id 更新 ARN

【讨论】:

    【解决方案2】:

    没有自动增量等效项,例如,休眠 id 生成器。您可以使用Bulk API(如果您必须一次保存多个文档)并以编程方式增加 _id 和 ARN 值的结尾。

    注意: 如果你想把你的id当作一个数字,你应该自己实现(在这个例子中,我添加了一个新字段“my_id ",因为文档的_id被视为字符串。

    POST /bulk
    { "index" : { "_index" : "your_index", "_type" : "your_type", "_id" : "1" } }
    { "arn" : "2018/03/17/1", my_id: 1 }
    { "index" : { "_index" : "your_index", "_type" : "your_type", "_id" : "2" } }
    { "arn" : "2018/03/17/2", my_id: 2 }
    

    然后,下次要保存新文档时,查询最大 id 类似:

    POST /my_index/my_type/_search?size=1
    {
      "query": {
           "fields": ["my_id"],
            "sort": [{
               "my_id": { "order": "desc" } }
            ]
       }
    }
    

    如果您的唯一要求是此 ARN 应该是唯一的,您也可以让 elasticsearch 计算您的 _id,只需不设置它即可。然后你可以在一些独特的令牌生成器上中继(UID.randomUUID().toString() 如果使用 java)。伪代码如下:

    String uuid = generateUUID() // depends on the programming language 
    String payload = "{ \"arn\" : + uuid + "}" // concatenate the payload
    String url = "http://localhost:9200/my_index" // your target index
    
    executePost(url, payload) // implement the call with some http client library
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 2017-09-01
      相关资源
      最近更新 更多