Elasticsearch script在reindex与script_fields script get当中的应用


本篇博客主要介绍了Elasticsearch script在reindex与script_fields script get当中的应用,主要是通过原有的字段拼接得到想要的新字段,同时在使用reindex时,指定dest_index的"_d"的值。


1、Elasticsearch script在_reindex中的应用

POST _reindex?wait_for_completion=false
{
  "source": {
    "index": "source_index",
    "query": {
      "bool": {
        "must": [
          {
            "range": {
              "snapshot_time": {
                "gte": "2022-06-01",
                "lte": "2022-07-01"
              }
            }
          }
        ]
      }
    }
  },
  "dest": {
    "index": "dest_index",
    "op_type": "index"
  },
  "script": {
    "inline": """
    ctx._source.bvid_page_time=ctx._source.bvid+'_'+ctx._source.page+'_'+ctx._source.snapshot_time;
    ctx._id=ctx._source.bvid_page_time;
    ctx._source.id=0
    """,
    "lang": "painless"
  },
  "conflicts": "proceed"
}

script中参数解释

ctx._source.bvid_page_time=ctx._source.bvid+'_'+ctx._source.page+'_'+ctx._source.snapshot_time
表示在迁移时,在source_index中增加字段"bvid_page_time",它是由source_index中的"bvid" "page" "snapshot_time"三个字段构成的,并在dest_index中也会有“bvid_page_time”字段。

ctx._id=ctx._source.bvid_page_time
表示将dest_index中的"_id"在迁移时指定为source_index中的"bvid_page_time"构成,而不是默认

ctx._source.id=0
表示在迁移过程中将source_index中的字段"id"设置为0,即dest_index中的"id"变为0,而source_index中的"id"不变

例子如下:

注意,这里使用的语句就是上面的"POST _reindex"
在这里插入图片描述
图一 source_index

在这里插入图片描述
图二 dest_index


2、Elasticsearch script_fields script在get当中的应用

GET bili_comment_data_20220520/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "bvid": {
              "value": "BV1******7Me"
            }
          }
        },
        {
          "term": {
            "id": {
              "value": "3045078"
            }
          }
        }
      ]
    }
  },
  "size": 12,
  "script_fields": {
    "bvid_page_time": {
      "script": {
        "lang": "painless", 
        "source": "doc['bvid'].value+'_'+doc['page'].value+'_'+doc.snapshot_time.date.year+doc.snapshot_time.date.monthOfYear+doc.snapshot_time.date.dayOfMonth+doc.snapshot_time.date.hourOfDay+doc.snapshot_time.date.minuteOfHour+doc.snapshot_time.date.secondOfMinute"
      }
    }
  }
}

参数说明

表示查询是构建新的字段"bvid_page_time"返回,且仅返回构建的字段;

列子如下:

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小雏菊的成长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值