【问题标题】:Auto Increment a field value every time a doc is updated in elasticsearch每次在 elasticsearch 中更新文档时自动增加字段值
【发布时间】:2020-03-02 07:52:40
【问题描述】:

这是有效载荷

 {
  "videourl": "*****",
"name": "ABCqq",
"description": "AAAnb",
"tags": "#AAAzx",
"uploadedtime": "2020-02-24T05:48:37.527Z",
"uploadedby": "Dr AAAgh",
"thumbnail": "http://",
"duration": "5:32",
"postedby": "AAAdf",
"doctorimage": "AAA12",
"doctorname": "nnn",
}

表格中的结果

{"_index": "rwe",
"_type": "_doc",
"_id": "8wEed3ABcYN_H8khP4hB",
"_score": 1,
"_source": {
    "videourl": "*****",
    "name": "ABCqq",
    "description": "AAAnb",
    "tags": "#AAAzx",
    "uploadedtime": "2020-02-24T05:48:37.527Z",
    "uploadedby": "Dr AAAgh",
    "thumbnail": "http://",
    "duration": "5:32",
    "postedby": "AAAdf",
    "doctorimage": "AAA12",
    "doctorname": "nnn"
}
}

这是一个文档,我想在每次更新此文档时增加字段的计数值。 我们必须添加名为 counter_value 的新字段。

预期结果

{"_index": "rwe",
"_type": "_doc",
"_id": "8wEed3ABcYN_H8khP4hB",
"_score": 1,
"_source": {
    "videourl": "*****",
    "name": "ABCqq",
    "description": "AAAnb",
    "tags": "#AAAzx",
    "uploadedtime": "2020-02-24T05:48:37.527Z",
    "uploadedby": "Dr AAAgh",
    "thumbnail": "http://",
    "duration": "5:32",
    "postedby": "AAAdf",
    "doctorimage": "AAA12",
    "doctorname": "nnn",
    "counter_value": 1
}
}

【问题讨论】:

  • 如何索引您的文档?你能解释一下你的过程吗?
  • 我们只有 1 个 index = 'rwe' 和 type = '_doc'
  • 我在询问您的流程。你如何索引文档?
  • 我们添加了简单的字段(视频网址、名称、描述等)作为有效负载

标签: elasticsearch versioning auto-increment


【解决方案1】:

您可以通过脚本增加计数器,请参阅herehere。但是,elastic 已经有一个版本字段。根据您的用例,将version 参数添加到您的查询中可能就足够了,如here 所述:

curl -XGET 'http://localhost:9200/rwe/_search?version=true'

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 2016-12-24
    • 2012-03-09
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多