ElasticSearch 7.4.0 mapping新增字段并赋值

本文详细介绍了如何使用Elasticsearch的API进行字段的新增和赋值操作。通过PUT请求创建字段映射,然后利用POST请求的_update_by_query方法更新已有文档的字段值。示例包括为资产表的area字段批量赋值为'无',以及动态添加ipSection字段并根据ip字段计算值。这些方法展示了Elasticsearch在数据维护上的灵活性。
摘要由CSDN通过智能技术生成

一、新增字段

请求url:http://111.11.11.111:19200/es的index/_mapping
请求方法:put
数据格式:
{
  "es的type": {
    "properties": {
      "字段1": {
        "type": "string"
      },
      "字段2": {
        "type": "string"
      }
    }
  }
}

二、给新加字段赋值

请求url:http://111.11.11.111:19200/es的index/_update_by_query/
请求方法:post
数据格式:
{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.字段= '值' "
  }
}

 

ElasticSearch的update_by_query语句可以很方便地为原有es表修改字段和新增字段,如下面的例子所示:

三、将资产表中area为空的字段赋值为'无'

POST template/_update_by_query
{
  "script": {
    "source": "ctx._source['area']='无'" 
  },
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "area"
          }
        }
      ]
    }
  }
}

 

四、添加一个网段字段,其值根据已有字段ip截取而来

 
POST template/_update_by_query
{
  "script": {
    "source": "def a=ctx._source['ip'].lastIndexOf('.');def sec=ctx._source['ip'].substring(0,a);ctx._source['ipSection']=sec+'.0'"
  },
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "ip"
          }
        }
      ]
    }
  }
}

其中script的语法为painless

Elasticsearch是一种流行的开源搜索和分析引擎,通常用于大规模数据的搜索和分析。要设置Elasticsearch 7.4.0集群并设置密码,需要执行以下步骤: 1. 首先,在每个节点上安装Elasticsearch,并确保它们都运行。 如果所有节点都在同一局域网内,则它们应该可以相互看到和通信。 2. 然后,在集群的任何一个节点上,使用curl命令来设置初始密码。 示例命令如下: curl -XPOST -u elastic 'localhost:9200/_security/user/elastic/_password?pretty' -H 'Content-Type: application/json' -d' { "password": "new-password" } ' 3. 接下来,编辑每个节点的elasticsearch.yml文件,以指定初始密码,并在所有节点上使用相同的密码。 示例配置如下: xpack.security.enabled: true xpack.security.authc.api_key.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate xpack.security.http.ssl.enabled: true xpack.security.http.ssl.verification_mode: certificate xpack.security.authc.realms.native.native1.order: 0 xpack.security.authc.realms.native.native1.enabled: true xpack.security.authc.realms.native.native1.users: elastic: password: "new-password" roles: ["superuser"] 4. 最后,重启所有Elasticsearch节点,以使配置生效。可以使用curl命令测试是否设置了密码,示例命令如下: curl -u elastic -XGET 'localhost:9200/_cluster/health?pretty' 以上是设置Elasticsearch 7.4.0集群并设置密码的大致步骤。要注意的是,本文仅提供了一种方法,并且可能不适用于所有情况。在实现任何更改之前,请仔细阅读Elasticsearch文档,并在测试环境中进行测试。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

戴国进

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

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

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

打赏作者

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

抵扣说明:

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

余额充值