Index Template 和Dynamic Template

这篇博客详细介绍了如何在 Elasticsearch 中管理模板和动态映射。内容包括创建、查看和更新模板,设置索引的副本数量,以及控制字段的自动类型检测。示例展示了数字和日期字段的映射,以及如何根据字段名动态定义字段类型。此外,还讨论了如何通过删除和重新创建索引来调整动态映射,并提供了具体的配置示例。
摘要由CSDN通过智能技术生成

//数字字符串被映射成text,日期字符串被映射成日期
PUT ttemplate/_doc/1
{
  "someNumber":"1",
  "someDate":"2019/01/01"
}

GET ttemplate/_mapping

//创建一个默认的template
PUT _template/template_default
{
  "index_patterns": ["*"],
  "order": 0,
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

PUT /_template/template_test
{
  "index_patterns": ["test*"],
  "order": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 2
  },
  "mappings": {
    "date_detection": false,
    "numeric_detection": true
  }
}

//查看template信息
GET /_template/template_default
GET /_template/temp*   

//写入新的数据,index以test开头
PUT testtemplate/_doc/1
{
  "someNumber":"1",
  "someDate":"2019/01/01"
}

GET testtemplate/_mapping
GET testtemplate/_settings


PUT testmy
{
  "settings": {
    "number_of_replicas": 5
  }
}
PUT testmy/_doc/1
{
  "key":"value"
}

GET testmy/_settings

//Dynamic Mapping 根据类型和字段名
DELETE my_index
PUT my_index/_doc/1
{
  "firstName":"Ruan",
  "isVIP":"true"
}
GET my_index/_mapping
DELETE my_index
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_boolean":{
          "match_mapping_type":"string",
          "match":"is*",
          "mapping":{
            "type":"boolean"
          }
        }
      },
      {
        "string_as_keywords":{
          "match_mapping_type":"string",
          "mapping":{
            "type":"keyword"
          }
        }
      }
    ]
  }
}

PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name":{
          "path_match":"name.*",
          "path_unmatch":"*.middle",
          "mapping":{
            "type": "text",
            "copy_to":"full_name"
          }
        }
      }
    ]
  }
}

PUT my_index/_doc/1
{
  "name":{
    "first":"John",
    "middle":"Winston",
    "last":"Lennon"
  }
}

GET my_index/_search?q=full_name:John

GET my_index/_mapping

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
动态数组类(DynamicArray)是一种可以动态增加或减少长度的数组。它的长度可以根据需要进行自动扩展或收缩。动态数组类通常用于需要频繁插入、删除或者动态调整大小的场合。 动态数组类的实现基于静态数组,通过动态申请新的内存空间,将原有的数据复制到新的内存空间中来实现自动扩展或收缩。当数组长度不够时,动态数组类会自动申请更大的内存空间,将原有的数据复制到新的内存空间中,并释放原有的内存空间;当数组长度过长时,动态数组类会自动收缩内存空间,以节省内存使用。 动态数组类的常见操作包括随机访问、插入元素、删除元素、获取数组长度等。具体实现可以使用模板类,支持不同类型的数据。以下是一个简单的动态数组类的示例代码: ```cpp template<typename T> class DynamicArray { private: T* data; int capacity; // 当前内存容量 int size; // 实际元素个数 void resize(int new_capacity) { T* new_data = new T[new_capacity]; for (int i = 0; i < size; i++) { new_data[i] = data[i]; } delete[] data; data = new_data; capacity = new_capacity; } public: DynamicArray() { data = new T[1]; capacity = 1; size = 0; } ~DynamicArray() { delete[] data; } int getSize() { return size; } T& operator[](int index) { if (index < 0 || index >= size) { throw "Index out of range."; } return data[index]; } void add(T item) { if (size == capacity) { resize(capacity * 2); } data[size++] = item; } void insert(int index, T item) { if (index < 0 || index > size) { throw "Index out of range."; } if (size == capacity) { resize(capacity * 2); } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = item; size++; } void remove(int index) { if (index < 0 || index >= size) { throw "Index out of range."; } for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; } size--; if (size <= capacity / 4) { resize(capacity / 2); } } }; ``` 这是一个基于模板类实现的动态数组类,支持任意类型的数据。它包括了常见的数组操作,如随机访问、添加元素、插入元素、删除元素等。通过动态扩展和收缩内存空间,可以实现数组长度的动态调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值