• elasticsearch创建索引和mapping


    1. 创建索引的同时指定特殊字段的类型

    PUT /gunspoc
    {
      "mappings": {
        "doc":{
          "properties":{
            "name":{
              "type":"keyword"
            },
            "age":{
              "type": "long"
            },
            "address":{
              "type":"text"
            },
            "birthday":{
               "type": "date",
               "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    这里建立了一个索引,且指定了name的类型为keyword,这样name字段就可以用term来精准搜索了,long类型的age就可以用来计算和比较大小了,birthday指定为日期类型,其可以是format中的格式化类型也可以是时间戳类型,其他的没有指定类型的字段将以默认的映射来在es中建立字段。

    2. 查看mapping

    GET /gunspoc/_mapping
    
    • 1

    结果:

    {
      "gunspoc" : {
        "mappings" : {
          "doc" : {
            "properties" : {
              "address" : {
                "type" : "text"
              },
              "age" : {
                "type" : "long"
              },
              "birthday" : {
                "type" : "date",
                "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
              },
              "name" : {
                "type" : "keyword"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3. 查看settings

    GET /gunspoc/_settings?pretty
    
    • 1

    结果:

    {
      "gunspoc" : {
        "settings" : {
          "index" : {
            "creation_date" : "1607391267314",
            "number_of_shards" : "5",
            "number_of_replicas" : "1",
            "uuid" : "Oljl4kqeSFiWCFz_1M5XvA",
            "version" : {
              "created" : "6050499"
            },
            "provided_name" : "gunspoc"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    number_of_shards: 该索引的的分片数
    number_of_replicas: 该索引的副本数
    (这里的number_of_shards: 5和 number_of_replicas:1 是建立索引的时候的默认值,也可以在建立索引的时候自定义)

    4. 建立索引同时设置settings

    PUT /gunspoc2
    {
      "settings": {
        "number_of_shards": 6,
        "number_of_replicas": 1,
        "refresh_interval": "10s",
        "translog":{
          "flush_threshold_size":"1gb",
          "sync_interval":"30s",
          "durability":"async"
        }
      },
      "mappings": {
        "doc":{
          "properties":{
            "name":{
              "type":"keyword"
            },
            "age":{
              "type": "long"
            },
            "address":{
              "type":"text"
            },
            "birthday":{
               "type": "date",
               "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    number_of_shards: 5 (主分片数量一旦设置后就不能修改了)
    number_of_replicas:1(副本数量可以修改)
    refresh_interval :索引的刷新时间间隔(即数据写入es到可以搜索到的时间间隔,设置越小越靠近实时,但是索引的速度会明显下降,),默认为1秒,如果我们对实时搜索没有太大的要求,反而更注重索引的速度,那么我们就应该设置的稍微大一些,这里我设置10s

    translog中存储了ES的操作记录,写入的索引并没有实时落盘到索引文件,而是先双写到内存和translog文件。因此不难看出translog的作用就是保证ES数据不丢失。为了保证性能,插入ES的数据并不会立刻落盘,而是首先存放在内存当中,等到条件成熟后触发flush操作,内存中的数据才会被写入到磁盘当中。如果ES服务器突然断电,那么停留在内存中还没来得及写入磁盘中的数据是不是就丢失了呢?还好translog保留了这些数据的操作日志,在ES服务重启的时候,会读取translog,恢复这部分数据。

    durability:async ,异步刷写translog日志,(默认是同步的)
    “flush_threshold_size”:“1gb” (当translog的大小达到此值时会进行一次flush操作。默认是512mb。)
    “sync_interval”:“30s”,每隔30s检查translog 刷到硬盘(默认5s)。

  • 相关阅读:
    webpack处理html资源
    go语言安装与环境配置
    集火全屋智能“后装市场”,真正玩得转的没几个
    初步了解Nginx
    智能运维应用之道,告别企业数字化转型危机
    leetcode 96 不同的二叉搜索树
    c语言初学者对scanf函数难分辨理解的两个点(欢迎补充)
    采用Linux 6.5 内核的Manjaro Linux 23.0正式上线
    【SSL】用Certbot生成免费HTTPS证书
    期末复习【微机原理】
  • 原文地址:https://blog.csdn.net/web18484626332/article/details/126361229