https://www.elastic.co/guide/en/elasticsearch/reference/current/index.htmlhtml
我嘗試了使用Java做爲Client向ES操做,結果發現這個傢伙要引入大量的JAR包,並且還必須是JDK1.8!!!!!我只好使用python操做ES寫入和操做數據了。python
一、建立mappingapp
from elasticsearch import Elasticsearch es_servers = [{ "host": "10.10.6.225", "port": "9200" }] # 使用文檔在這裏:http://elasticsearch-py.readthedocs.io/en/master/ es = Elasticsearch(es_servers) # 初始化索引的Mappings設置 _index_mappings = { "mappings": { "doc": { "properties": { "title": {"type": "text"}, "name": {"type": "text"}, "age": {"type": "integer"}, "created": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" } } } } } # 若是索引不存在,則建立索引 if es.indices.exists(index='blog_index') is not True: es.indices.create(index='blog_index', body=_index_mappings)
URL:http://10.10.6.225:9200/my_index/_mapping/doc 方式:GET 返回: { "my_index": { "mappings": { "doc": { "properties": { "age": { "type": "integer" }, "created": { "type": "date" }, "name": { "type": "text" }, "title": { "type": "text" } } } } } }
參考網址:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.htmlelasticsearch
python的腳本在這裏:ide
# pip install elasticsearch from elasticsearch import Elasticsearch es_servers = [{ "host": "10.10.6.225", "port": "9200" }] # 使用文檔在這裏:http://elasticsearch-py.readthedocs.io/en/master/ es = Elasticsearch(es_servers) print(es.indices.get(index='blog_index')['blog_index']['mappings'])
返回:測試
C:\Python36\python.exe D:/Work/ELK/run.py {'doc': {'properties': {'age': {'type': 'integer'}, 'created': {'type': 'date'}, 'name': {'type': 'text'}, 'title': {'type': 'text'}}}} Process finished with exit code 0
三、若是你想更新Mapping,那麼就看看這裏吧:ui
就是不行,不行,做廢,重導入!!!!!spa
四、上傳一些數據玩玩吧!3d
# pip install elasticsearch import time from elasticsearch import Elasticsearch from elasticsearch.helpers import bulk es_servers = [{ "host": "10.10.6.225", "port": "9200" }] # 使用文檔在這裏:http://elasticsearch-py.readthedocs.io/en/master/ es = Elasticsearch(es_servers) index_name = 'blog_index' doc_type_name = 'doc' # 建立ACTIONS ACTIONS = [] line_list = [ {'age': 25, 'created': '2018-02-09 12:00:01', 'name': '黃海', 'title': '厲害了個人國'}, {'age': 32, 'created': '2018-02-19 12:00:01', 'name': '李勇', 'title': '紅海行動'}, {'age': 25, 'created': '2018-02-13 12:00:01', 'name': '趙志', 'title': '湄公河行動'}, {'age': 22, 'created': '2018-02-03 12:00:01', 'name': '李四', 'title': '極品相師'}, {'age': 18, 'created': '2018-02-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 43, 'created': '2018-01-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 28, 'created': '2018-01-02 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 23, 'created': '2018-01-04 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 22, 'created': '2018-03-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 21, 'created': '2018-03-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 25, 'created': '2018-03-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 34, 'created': '2018-03-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 33, 'created': '2017-04-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 54, 'created': '2017-04-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'}, {'age': 25, 'created': '2017-04-01 12:00:01', 'name': '劉勇', 'title': '鄉村愛情故事'} ] for line in line_list: action = { "_index": index_name, "_type": doc_type_name, "_source": { "age": line['age'], "created": line['created'], "name": line['name'], "title": line['title'] } } ACTIONS.append(action) # 批量處理 success, _ = bulk(es, ACTIONS, index=index_name, raise_on_error=True)
五、按時間段進行聚合測試code
from elasticsearch import Elasticsearch es_servers = [{ "host": "10.10.6.225", "port": "9200" }] # 使用文檔在這裏:http://elasticsearch-py.readthedocs.io/en/master/ es = Elasticsearch(es_servers) print(es.indices.get(index='blog_index')['blog_index']['mappings']) body= { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "created", "interval": "month", "format": "yyyy-MM" } } } } res = es.search(index="blog_index", body=body) print(res)
打完收工!