您当前的位置: 首页 >  ar

cuiyaonan2000

暂无认证

  • 2浏览

    0关注

    248博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Elasticsearch: Index Template

cuiyaonan2000 发布时间:2022-08-17 11:13:14 ,浏览量:2

序言

索引模板用于我们在新建索引的时候,不用重复的去设置相关的配置新.而是直接沿用现成的.

同时根据索引模板已经建成的索引,不会因为模板的修改而发生改变cuiyaonan2000@163.com

create index API请求中指定的设置和映射会覆盖索引模板中指定的任何设置或映射,即索引模板配置设置优先级不是最高的.

Mapping&Setting

首先创建一个索引

PUT cuiyaonan_index

然后过去该索引的配置信息即:Mapping 和Setting信息

GET cuiyaonan_index

返回如下内容

{
  "cuiyaonan_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "cuiyaonan_index",
        "creation_date" : "1660703634990",
        "number_of_replicas" : "1",
        "uuid" : "dNyxiwJhSDGO--UFd71jVA",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}

因为是新建的空白表所以没有mapping.如下的是logstash的索引配置信息,可以对照着看一下cuiyaonan2000@163.com

{
  "my-filebeat-5-logstash-2022.08.10" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "agent" : {
          "properties" : {
            "ephemeral_id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "hostname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "version" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "ecs" : {
          "properties" : {
            "version" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "host" : {
          "properties" : {
            "architecture" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "containerized" : {
              "type" : "boolean"
            },
            "hostname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "id" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "ip" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "mac" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "os" : {
              "properties" : {
                "codename" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "family" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "kernel" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "platform" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "type" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "version" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            }
          }
        },
        "input" : {
          "properties" : {
            "type" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "log" : {
          "properties" : {
            "file" : {
              "properties" : {
                "path" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },
            "flags" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "offset" : {
              "type" : "long"
            }
          }
        },
        "message" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "mytime" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "my-filebeat-5-logstash-2022.08.10",
        "creation_date" : "1660704979105",
        "number_of_replicas" : "1",
        "uuid" : "ZstZ-zJISsGx5zTM5h3OMA",
        "version" : {
          "created" : "7140099"
        }
      }
    }
  }
}
Mapping

Mapping 是用于定义 Document 及其包含的字段类型和索引方式相关的信息,但在Elasticsearch中,可以动态的识别新增的字段值来推测出字段类型动态增加。

1、定义索引中字段的名称

2、定义字段的数据类型,如:字符串、数字、boolean等

3、可对字段设置倒排索引的相关配置,如是否需要分词,使用什么分词器

 "mappings" : {      #mappings 关键字
      "properties" : {     #关键字
        "end_time" : {     #自定义的document中的字段
          "type" : "long"  #为该字段定义的字段类型
        },
        "endpoint" : {
          "type" : "keyword"
        }
}

如上end_time中的其它可以选设置如下所示:

"field": {  
         "type":  "text", //文本类型  ,指定类型
       
         "index": "false"// ,设置成false,字段将不会被索引  
         
         "analyzer":"ik"//指定分词器  
         
         "boost":1.23//字段级别的分数加权  
         
         "doc_values":false//对not_analyzed字段,默认都是开启,analyzed字段不能使用,对排序和聚合能提升较大性能,节约内存,如果您确定不需要对字段进行排序或聚合,或者从script访问字段值,则可以禁用doc值以节省磁盘空间:
         
         "fielddata":{"loading" : "eager" }//Elasticsearch 加载内存 fielddata 的默认行为是 延迟 加载 。 当 Elasticsearch 第一次查询某个字段时,它将会完整加载这个字段所有 Segment 中的倒排索引到内存中,以便于以后的查询能够获取更好的性能。
         
         "fields":{"keyword": {"type": "keyword","ignore_above": 256}} //可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词  
         
         "ignore_above":100 //超过100个字符的文本,将会被忽略,不被索引
           
         "include_in_all":ture//设置是否此字段包含在_all字段中,默认是true,除非index设置成no选项  
         
         "index_options":"docs"//4个可选参数docs(索引文档号) ,freqs(文档号+词频),positions(文档号+词频+位置,通常用来距离查询),offsets(文档号+词频+位置+偏移量,通常被使用在高亮字段)分词字段默认是position,其他的默认是docs  
         
         "norms":{"enable":true,"loading":"lazy"}//分词字段默认配置,不分词字段:默认{"enable":false},存储长度因子和索引时boost,建议对需要参与评分字段使用 ,会额外增加内存消耗量  
         
         "null_value":"NULL"//设置一些缺失字段的初始化值,只有string可以使用,分词字段的null值也会被分词  
         
         "position_increament_gap":0//影响距离查询或近似查询,可以设置在多值字段的数据上火分词字段上,查询时可指定slop间隔,默认值是100  
         
         "store":false//是否单独设置此字段的是否存储而从_source字段中分离,默认是false,只能搜索,不能获取值  
         
         "search_analyzer":"ik"//设置搜索时的分词器,默认跟ananlyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能  
         
         "similarity":"BM25"//默认是TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效  
         
         "term_vector":"no"//默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用  
       }  
注意事项
  • mapping中字段类型一旦设定后 禁止直接修改。因为lucene实现的倒排索引生成后不允许修
  • Elasticsearch有动态映射功能(Dynamic Mapping ):在写入文档时,如果索引不存在,会自动创建索引 .这种机制,使得我们无需手动定义mappings。Elasticsearch会自动根据文档信息,推算出字段的类型

Setting

Setting 是用于定义 Index 分片数量及副本数量等相关的配置信息,其中分片数量在设定后不能修改,而副本数量可以在运行过程中根据业务需要动态调整。因此Setting的配置分为静态配置和动态配置

静态配置

所谓的静态配置即索引创建后不能修改,静态配置只能在创建索引时或者在状态为 closed index的索引(闭合的索引)上设置

index.number_of_shards :主分片数,默认为5.只能在创建索引时设置,不能修改

index.shard.check_on_startup :是否在索引打开前检查分片是否损坏,当检查到分片损坏将禁止分片被打开
可选值:false:不检测;checksum:只检查物理结构;true:检查物理和逻辑损坏,相对比较耗CPU;fix:类同与false,7.0版本后将废弃。默认值:false。

index.codec:数据存储的压缩算法,默认算法为LZ4,也可以设置成best_compression,best_compression压缩比较好,但存储性能比LZ4差

index.routing_partition_size :路由分区数,默认为 1,只能在索引创建时设置。此值必须小于index.number_of_shards,如果设置了该参数,其路由算法为: (hash(_routing) + hash(_id) % index.routing_parttion_size ) % number_of_shards。如果该值不设置,则路由算法为 hash(_routing) % number_of_shardings,_routing默认值为_id。
动态配置
index.number_of_replicas :每个主分片的副本数,默认为 1,该值必须大于等于0

index.auto_expand_replicas :基于可用节点的数量自动分配副本数量,默认为 false(即禁用此功能)

index.refresh_interval :执行刷新操作的频率,这使得索引的最近更改可以被搜索。默认为 1s。可以设置为 -1 以禁用刷新。
 
index.max_result_window :用于索引搜索的 from+size 的最大值。默认为 10000
 
index.max_rescore_window : 在搜索此索引中 rescore 的 window_size 的最大值
 
index.blocks.read_only :设置为 true 使索引和索引元数据为只读,false 为允许写入和元数据更改。

index.blocks.read_only_allow_delete:与index.blocks.read_only基本类似,唯一的区别是允许删除动作。
 
index.blocks.read :设置为 true 可禁用对索引的读取操作
 
index.blocks.write :设置为 true 可禁用对索引的写入操作。
 
index.blocks.metadata :设置为 true 可禁用索引元数据的读取和写入。
 
index.max_refresh_listeners :索引的每个分片上可用的最大刷新侦听器数

index.max_docvalue_fields_search:一次查询最多包含开启doc_values字段的个数,默认为100。

常用配置
# 设置分片数量
number_of_shards 
# 设置副本数量
number_of_replicas
# 设置自动扩展副本数量
index.auto_expand_replicas
# 设置 Index Buffer 刷新间隔
refresh_interval
# 设置自定义分词器
analysis
创建模板

使用如下命令创建

PUT /_template/

查看索引模板
GET 索引名称

关注
打赏
1638267374
查看更多评论
立即登录/注册

微信扫码登录

0.0377s