Elasticsearch5.X索引说明
主要内容
基本数据类型
- 这里只说常用的类型,5.X与1.X类型改进了一些
- 官方说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html
- string类型:5.X没有这个类型,在5.X以下版本才会有string类型,在5.X中string类型被拆分为text和keyword类型
1.1. text类型:文本块类型,用于存放文章的正文信息,可以理解为存放大内容的文本数据类型,可对数据进行分词检索
1.2. keyword类型:文本关键信息类型,用于存放文章中的关键信息,如姓名,手机号,邮箱地址等,不可对此类型数据进行分词 - 数值类型:用于存放整数与小数,包含以下类型:long, integer, short, byte, double, float, half_float, scaled_float,这些类型都比较熟悉,就不多说了,不同的只是取值范围
- date类型:日期时间类型,用于存放日期与时间,可接受多种日期时间格式输入
3.1 友好化的日期时间格式输入,如:2015-10-10 或 2015-10-10 16:15:10
3.2 iso 8601标准日期时间格式输入,如:2015-01-01T12:10:30Z
3.3 时间截格式输入,如:1420070400001
3.4 自定义日期时间输出格式,如在索引中加入:”format”: “yyyy-MM-dd HH:mm:ss”,即在索引中保存的日期时间会以定义的格式输出 - boolean类型:布尔类型,表示真或假,可接受json中的true/false,也可以接受字符串的”true/false”,”off/no”,”1/0”,也可以接受数字1/0来表示真或假值
- 数值范围类型:可用于表示一个范围的数字,包含对日期时间,IP范围的表示,具体见https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html
- array类型:列表类型,支持将多个数据以列表形式存储,如:[ “one”, “two” ],[ 1, [ 2, 3 ]]
- object类型:对象类型,支持直接将一个json数据对象化展开
- geo_point类型:表示经纬度数据类型,通过两个坐标来标示一个位置点,可接受多种格式数据输入
8.1. 数字格式输入:{“lat”:41.12,”lon”:-71.34}
8.2. 字符格式输入:”41.12,-71.34”
8.3. geohash:哈希格式输入,如:”drm3btev3e86”
8.4. 列表格式输入:[ -71.34, 41.12 ] - ip类型:支持ipv4和ipv6格式的地址
索引属性
index属性
- 用于设置索引特性的一些属性,如索引的数据分块,备份等
- number_of_shards属性:指定索引数据的分块数量,默认为5,即对索引数据分为5个数据块存储。如果集群机器结点多,分块越多是越好的,因为数据分布更平均,但一个结点的数据块也不能过多,最好3个左右
注意:这个值必须在创建索引的时候指定,指定后是不能修改,但5.X版本是可以通过接口减少分片数量,而不能扩大分片数量 - number_of_replicas属性:指定数据块备份的数量,默认为1,即对索引数据块进行一个备份。如果对数据要求比较高,建议至少有一个备份,elk会将备份的数据块平均分布到不同的结点,这样如果某一结点丢失,备份数据还能继搜索,当然也不能100%保证,要看备份数据块的分布。但备份过多,对磁盘消耗会过大。
注意:这个值是可以通过接口改变的,所以不用太在意 - 更多索引属性: 如果想设置更多索引属性,见https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html
其中有限制数据查询条目数量的属性:max_rescore_window等
mappings属性
analyzer分词属性
- 用于数据分词定义
- analyzer: ik_max_word
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”
索引示例
{
"settings" : {
"index" : {
"number_of_shards" : 30,
"number_of_replicas" : 0
}
},
"mappings": {
"hhscan": {
"properties": {
"date": {
"type": "date"
},
"redunip": {
"type": "ip"
},
"otherInfo": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"type": "text"
},
"geoip": {
"type": "geo_point"
},
"target": {
"type": "keyword"
},
"portType": {
"type": "keyword"
},
"port": {
"type": "integer"
}
}
}
}
}
索引操作
检查索引是否存在
from elasticsearch import Elasticsearch
from elasticsearch.client import IndicesClient
elkClient = Elasticsearch(["http://ip:9200"])
elkIndex = IndicesClient(elkClient)
test = elkIndex.exists(index="test_test_test")
print test
索引创建
from elasticsearch import Elasticsearch
from elasticsearch.client import IndicesClient
elkClient = Elasticsearch(["http://ip:9200"])
elkIndex = IndicesClient(elkClient)
mapp = {
"settings" : {
"index" : {
"number_of_shards" : 5,
"number_of_replicas" : 0
}
},
"mappings": {
"hhscan": {
"properties": {
"date": {
"type": "date"
},
"redunip": {
"type": "ip"
}
}
}
}
}
test = elkIndex.create(index="test_test_test", body=mapp)
print json.dumps(test)
索引修改
from elasticsearch import Elasticsearch
from elasticsearch.client import IndicesClient
elkClient = Elasticsearch(["http://ip:9200"])
elkIndex = IndicesClient(elkClient)
attUpdate = {
"index" : {
"number_of_replicas" : 1
}
}
elkIndex.put_settings(body=attUpdate,index="test_test_test")
索引删除
from elasticsearch import Elasticsearch
from elasticsearch.client import IndicesClient
elkClient = Elasticsearch(["http://ip:9200"])
elkIndex = IndicesClient(elkClient)
elkIndex.delete(index="test_test_test")
索引复制
from elasticsearch import Elasticsearch
from elasticsearch import helpers
elkClient = Elasticsearch(["http://ip:9200"])
test = helpers.reindex(elkClient,source_index="test_test_test",target_index="test_test_test_aaa")
print json.dumps(test)
其它索引操作