Elasticsearch 创建默认模板,以后再也不需要再重新建索引了

本文详细介绍了如何使用curl命令向Elasticsearch发送PUT请求来创建一个名为'template_t_url'的索引模板。该模板定义了包括'starttime'在内的多个字段的映射和设置,如'descid'、'url'等,并设置了3个主分片和1个副本。

curl -XPUT 'http://192.168.1.181:9200/_template/template_t_url' -d ' {
 "template": "t_url-*",
  "settings": {
    "index": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "info": {
      "properties": {
        "referer": {
          "type": "string",
          "index": "not_analyzed"
        },
        "descid": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "line": {
          "type": "string",
          "index": "not_analyzed"
        },
        "words": {
          "type": "string",
          "index": "not_analyzed"
        },
        "dip": {
          "type": "ip",
          "index": "not_analyzed"
        },
        "starttime": {
          "type": "date",
          "format": "yyy-MM-dd HH:mm:ss"
        },

        "dir": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "rid": {
          "type": "string",
          "index": "not_analyzed"
        },
        "type": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "grpids": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "platform": {
          "type": "string",
          "index": "not_analyzed"
        },
        "url": {
          "type": "string",
          "index": "not_analyzed"
        },
        "dport": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "subtype": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "bytes": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "browser": {
          "type": "string",
          "index": "not_analyzed"
        },
        "domain": {
          "type": "string",
          "index": "not_analyzed"
        },
        "action": {
          "type": "string",
          "index": "not_analyzed"
        },
        "termtype": {
          "type": "string",
          "index": "not_analyzed"
        },
        "sip": {
          "type": "ip",
          "index": "not_analyzed"
        },
        "sport": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "username": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}'

Elasticsearch 7.9中,创建索引是一个基本操作,用于存储和检索数据。以下是创建索引的步骤: 1. **打开Elasticsearch客户端**:你可以使用curl命令行工具,Elasticsearch REST API客户端(如Sense或Beats中的Kibana Dev Tools),或者Elasticsearch的Java、Python等官方客户端库。 2. **定义索引信息**:首先,你需要指定索引的名字。例如,如果你想创建一个名为`my-index`的索引,可以这样表示: ```json { "index": { "name": "my-index" } } ``` 3. **发送请求**:将这个JSON结构作为POST请求发送到`/_create`端点。完整的URL可能是`http://localhost:9200/my-index/_create`,其中`localhost:9200`是Elasticsearch默认地址,需要根据实际情况替换。 示例用curl命令: ```sh curl -X POST 'http://localhost:9200/my-index/_create' -H 'Content-Type: application/json' -d ' { "index": { "name": "my-index" } } ' ``` 4. **检查响应**:如果索引创建成功,Elasticsearch会返回一个响应确认索引创建。如果索引已经存在,或者创建过程中出现错误,响应会包含相应的错误信息。 5. **配置索引模板**(可选):为了更高效地管理索引,可以预先定义索引模板,设置字段映射、分析器等属性。 6. **将文档插入索引**:创建索引后,你可以开始将文档(通常是JSON格式)插入到索引中。 注意:在实际使用中,可能还需要设置一些选项,比如分片和副本数,以提高查询性能和容错性。具体配置应根据应用需求调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学海无涯苦做java

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

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

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

打赏作者

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

抵扣说明:

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

余额充值