Python客户端创建索引

Catalogue
  1. 验证
  2. 完整脚本
  • 安装elasticsearch模块
  • 进入python交互界面
  • 引入es的模块
    1
    from elasticsearch import Elasticsearch
  • 定义es链接及变量
    1
    2
    doc_type = 'log'
    es = Elasticsearch(urls = ['http://192.168.1.108:9200','http://192.168.1.106:9200'], timeout = 60, max_retries = 0)
  • 设置mapping
  • 创建索引
    1
    es.index(index = 'app-log', doc_type='log', body = mapping)
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
33
34
mapping = {
'settings': {
'index': {
'number_of_replicas': 1,
'number_of_shards': 6,
'refresh_interval': '5s'
}
},
'mappings': {
'_default_': {
'_all': {
'enabled': False
}
},
doc_type : {
'properties' : {
'day': { 'type': 'string', 'index': 'not_analyzed'},
'time': { 'type': 'string', 'index': 'not_analyzed'},
'nanoTime': { 'type': 'string', 'index': 'not_analyzed'},
'created': { 'type': 'date', 'index': 'not_analyzed'},
'app': { 'type': 'string', 'index': 'not_analyzed'},
'host': { 'type': 'string', 'index': 'not_analyzed'},
'thread': { 'type': 'string', 'index': 'not_analyzed'},
'level': { 'type': 'string', 'index': 'not_analyzed'},
'eventType': { 'type': 'string', 'index': 'not_analyzed'},
'pack': { 'type': 'string', 'index': 'not_analyzed'},
'clazz': { 'type': 'string', 'index': 'not_analyzed'},
'line': { 'type': 'string', 'index': 'not_analyzed'},
'messageSmart': { 'type': 'text', 'analyzer': 'ik_smart', 'search_analyzer': 'ik_smart', 'include_in_all': 'true', 'boost': 8},
'messageMax': { 'type': 'text', 'analyzer': 'ik_max_word', 'search_analyzer': 'ik_max_word', 'include_in_all': 'true', 'boost': 8}
}
}
}
}

验证

1
curl '192.168.1.108:9200/_cat/indices?v'

完整脚本

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys
import datetime
from pyelasticsearch import ElasticSearch
from pyelasticsearch import bulk_chunks

def main(argv):
doc_type = 'log'
index = 'app-log'
es = ElasticSearch(urls = ['http://192.168.1.108:9200'], timeout = 60, max_retries = 0)


mapping = {
'settings': {
'index': {
'number_of_replicas': 1,
'number_of_shards': 6,
'refresh_interval': '5s'
}
},
'mappings': {
'_default_': {
'_all': {
'enabled': False
}
},
doc_type : {
'properties' : {
'day': { 'type': 'string', 'index': 'not_analyzed'},
'time': { 'type': 'string', 'index': 'not_analyzed'},
'nanoTime': { 'type': 'string', 'index': 'not_analyzed'},
'created': { 'type': 'date', 'index': 'not_analyzed'},
'app': { 'type': 'string', 'index': 'not_analyzed'},
'host': { 'type': 'string', 'index': 'not_analyzed'},
'thread': { 'type': 'string', 'index': 'not_analyzed'},
'level': { 'type': 'string', 'index': 'not_analyzed'},
'eventType': { 'type': 'string', 'index': 'not_analyzed'},
'pack': { 'type': 'string', 'index': 'not_analyzed'},
'clazz': { 'type': 'string', 'index': 'not_analyzed'},
'line': { 'type': 'string', 'index': 'not_analyzed'}
# 'messageSmart': { 'type': 'string', 'analyzer': 'ik_smart', 'search_analyzer': 'ik_smart', 'include_in_all': 'true', 'boost': 8},
}
}
}
}

es.create_index(index = index, settings = mapping)

if __name__ == '__main__':
main(sys.argv)