es創建索引和mapping的實例
es創建索引和mapping
索引和type分開創建
1、創建index
http://127.0.0.1:9200/ negative/ put { "settings": { "index": { "search": { "slowlog": { "threshold": { "fetch": { "debug": "5s" }, "query": { "warn": "20s" } } } }, "indexing": { "slowlog": { "threshold": { "index": { "info": "20s" } } } }, "number_of_shards": "1", "number_of_replicas": "0" } } }
2、創建mapping
http://127.0.0.1:9200/ negative/negative/_mapping post {"properties":{ "id": { "type": "long" }, "yjlb": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "ejlb": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "sjlb": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "detail": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "ssyj": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } }}
索引和type一次創建
(註意:mapping下面一層的key值 是type名稱)
http://192.168.0.213:9200/ announcement/ put { "settings": { "index": { "search": { "slowlog": { "threshold": { "fetch": { "debug": "5s" }, "query": { "warn": "20s" } } } }, "indexing": { "slowlog": { "threshold": { "index": { "info": "20s" } } } }, "number_of_shards": "1", "number_of_replicas": "0" } }, "mappings": { "announcement": { "properties": { "id": { "type": "keyword" }, "createtime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "creatby": { "type": "keyword" }, "updatetime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "type": { "type": "keyword" }, "status": { "type": "keyword" }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "cont": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "files": { "type": "nested", "properties": { "id": { "type": "keyword" }, "filename": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } } } }
更改elasticsearch中索引的mapping
昨天研發說在kibana中統計userid字段不出圖,後來查到該字段顯示沖突瞭,然後再查看瞭GET test/_mapping下該索引的mapping,發現userid是long類型的,而userid.keyword是string類型的,出現這種情況的根本原因是日志中這個字段存的是數值類型的值,改成字符串類型即可,由於急著用,我司上線一般是下午6點30上線,所以臨時修改瞭下該字段的類型,步驟如下:
查看舊索引的mapping
GET test
/_mapping
找到userid這個字段,修改類型為keyword,如下:
{ "mappings": { "doc": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "beat": { "properties": { "hostname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "code": { "type": "long" }, "dip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "fields": { "properties": { "log_topic": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "host": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "message": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "method": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "offset": { "type": "long" }, "referer": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "sip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "source": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "tags": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "time": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "url": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "userid": { "type": "keyword" #修改此處 } } } } }
創建一個自定義mapping的新索引
PUT test-new { "mappings": { "doc": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "beat": { "properties": { "hostname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "code": { "type": "long" }, "dip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "fields": { "properties": { "log_topic": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "host": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "message": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "method": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "offset": { "type": "long" }, "referer": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "sip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "source": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "tags": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "time": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "url": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "userid": { "type": "keyword" } } } } }
把舊索引的數據reindex到新索引上
註意,舊索引先停止新數據的寫入
POST _reindex { "source": { "index": "test" }, "dest": { "index": "test-new" } }
刪除舊索引
DELETE test
按照步驟2創建test索引
PUT test { "mappings": { "doc": { "properties": { "@timestamp": { "type": "date" }, "@version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "beat": { "properties": { "hostname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "version": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "code": { "type": "long" }, "dip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "fields": { "properties": { "log_topic": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "host": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "message": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "method": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "offset": { "type": "long" }, "referer": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "sip": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "source": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "tags": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "time": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "url": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "userid": { "type": "keyword" } } } } }
把test-new索引的數據reindex到test索引上
POST _reindex { "source": { "index": "test-new" }, "dest": { "index": "test" } }
查看test索引的mapping
GET test/_mapping,執行命令後,可以看到userid的字段類型為keyword類型瞭
然後再打開該索引接收新數據的開關
總結
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- ElasticSearch學習之Es索引Api操作
- Elasticsearch 映射參數詳解 fields
- Elasticsearch中store field與non-store field的區別說明
- 使用logstash同步mysql數據到elasticsearch實現
- elasticsearch bucket 之rare terms聚合使用詳解