首先听一下官方的话:

https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

我尝试了使用Java作为Client向ES操作,结果发现这个家伙要引入大量的JAR包,而且还必须是JDK1.8!!!!!我只好使用python操作ES写入和操作数据了。

1、创建mapping

参考地址:

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

对应的python是这样的:

from elasticsearch import Elasticsearch
es_servers = [{
"host": "10.10.6.225",
"port": ""
}]
# 使用文档在这里: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)

2、查看mapping

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.html

python的脚本在这里:

# pip install elasticsearch
from elasticsearch import Elasticsearch
es_servers = [{
"host": "10.10.6.225",
"port": ""
}]
# 使用文档在这里: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

3、如果你想更新Mapping,那么就看看这里吧:

就是不行,不行,作废,重导入!!!!!

4、上传一些数据玩玩吧!

# pip install elasticsearch
import time
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk es_servers = [{
"host": "10.10.6.225",
"port": ""
}]
# 使用文档在这里: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)

5、按时间段进行聚合测试

from elasticsearch import Elasticsearch
es_servers = [{
"host": "10.10.6.225",
"port": ""
}]
# 使用文档在这里: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)

打完收工!

Elasticsearch 6.x 入门测试的更多相关文章

  1. Elasticsearch全文检索工具入门

    Elasticsearch全文检索工具入门: 1.下载对应系统版本的文件 elasticsearch-2.4.0.zip 1.1运行elasticsearch-2.4.0\elasticsearch- ...

  2. 最新版本elasticsearch本地搭建入门篇

    最新版本elasticsearch本地搭建入门篇 项目介绍 最近工作用到elasticsearch,主要是用于网站搜索,和应用搜索. 工欲善其事,必先利其器. 自己开始关注elasticsearch, ...

  3. elasticsearch.net search入门使用指南中文版(翻译)

    elasticsearch.net search入门使用指南中文版,elasticsearch.Net是一个非常底层且灵活的客户端,它不在意你如何的构建自己的请求和响应.它非常抽象,因此所有的elas ...

  4. elasticsearch.net search入门使用指南中文版

    原文:http://edu.dmeiyang.com/book/nestusing.html elasticsearch.net为什么会有两个客户端? Elasticsearch.Net是一个非常底层 ...

  5. Elasticsearch学习记录(入门篇)

    Elasticsearch学习记录(入门篇) 1. Elasticsearch的请求与结果 请求结构 curl -X<VERB> '<PROTOCOL>://<HOST& ...

  6. ElasticSearch极简入门总结

    一,目录 安装es 项目添加maven依赖 es客户端组件注入到spring容器中 es与mysql表结构对比 索引的删除创建 文档的crud es能快速搜索的核心-倒排索引 基于倒排索引的精确搜索. ...

  7. Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看

    一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...

  8. (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引

    Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...

  9. Elasticsearch【快速入门】

    前言:毕设项目还要求加了这个做大数据搜索,正好自己也比较感兴趣,就一起来学习学习吧! Elasticsearch 简介 Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引 ...

随机推荐

  1. python学习(20) 网络编程

    原文链接:http://www.limerence2017.com/2018/01/02/python20/ python 网络编程和基本的C语言编程一样,效率不是很高,如果为了封装通信库建议采用C/ ...

  2. C语言中的指针和内存泄漏几种情况

    引言 原文地址:http://www.cnblogs.com/archimedes/p/c-point-memory-leak.html,转载请注明源地址. 对于任何使用C语言的人,如果问他们C语言的 ...

  3. vue 开发过程中遇到的问题

    1. gitlab团队协作开发 2. element ui 问题集锦 3. 使用vue和ElementUI快速开发后台管理系统

  4. Elasticsearch相关概念了解

    mysql ⇒数据库databases       ⇒表tables     ⇒ 行rows        ⇒ 列columns es   ⇒索引indices        ⇒ 类型types    ...

  5. elk定时清理日志

    #!/bin/bash shijian=`date +%Y.%m.%d -d "5 days ago"` #echo $shijian curl -XDELETE "10 ...

  6. bzoj千题计划122:bzoj1034: [ZJOI2008]泡泡堂BNB

    http://www.lydsy.com/JudgeOnline/problem.php?id=1034 从小到大排序后 最大得分: 1.自己最小的>对方最小的,赢一场 2.自己最大的>对 ...

  7. thinkphp 带条件分页查询

    thinkphp 带条件分页查询:form表单传值时候,method='get'. 用 get 传值

  8. js正则匹配数字字母汉字

    1,匹配所有字母数字汉字:^[A-Za-z0-9\u4e00-\u9fa5]+$2,清空某项:$('#id').empty()3,某项功能关闭,不再执行:$('#id').off()4,查看数据类型: ...

  9. 训练赛第二场C题 zoj 2339 Hyperhuffman

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2339 解题报告:题目太长了,比赛的时候根本看不懂,完了之后问了什 ...

  10. Postman和Selenium IDE开局自带红蓝BUFF属性,就问你要还是不要

    话不多说,下面给大家介绍两款工具,selenium IDE和Postman. 为什么说是自带红蓝Buff,因为想做UI自动化和接口自动化的同学,很多时候,都难在了开头. 比如你要学习语言,你要学习框架 ...