Elasticsearch 6.x 入门测试
首先听一下官方的话:
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 入门测试的更多相关文章
- Elasticsearch全文检索工具入门
Elasticsearch全文检索工具入门: 1.下载对应系统版本的文件 elasticsearch-2.4.0.zip 1.1运行elasticsearch-2.4.0\elasticsearch- ...
- 最新版本elasticsearch本地搭建入门篇
最新版本elasticsearch本地搭建入门篇 项目介绍 最近工作用到elasticsearch,主要是用于网站搜索,和应用搜索. 工欲善其事,必先利其器. 自己开始关注elasticsearch, ...
- elasticsearch.net search入门使用指南中文版(翻译)
elasticsearch.net search入门使用指南中文版,elasticsearch.Net是一个非常底层且灵活的客户端,它不在意你如何的构建自己的请求和响应.它非常抽象,因此所有的elas ...
- elasticsearch.net search入门使用指南中文版
原文:http://edu.dmeiyang.com/book/nestusing.html elasticsearch.net为什么会有两个客户端? Elasticsearch.Net是一个非常底层 ...
- Elasticsearch学习记录(入门篇)
Elasticsearch学习记录(入门篇) 1. Elasticsearch的请求与结果 请求结构 curl -X<VERB> '<PROTOCOL>://<HOST& ...
- ElasticSearch极简入门总结
一,目录 安装es 项目添加maven依赖 es客户端组件注入到spring容器中 es与mysql表结构对比 索引的删除创建 文档的crud es能快速搜索的核心-倒排索引 基于倒排索引的精确搜索. ...
- Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看
一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...
- (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...
- Elasticsearch【快速入门】
前言:毕设项目还要求加了这个做大数据搜索,正好自己也比较感兴趣,就一起来学习学习吧! Elasticsearch 简介 Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引 ...
随机推荐
- apt代理设置
内网apt使用代理 /etc/apt/apt.conf Acquire::http::Proxy "http://guest:password@ip:port";
- lightoj 1205 数位dp
1205 - Palindromic Numbers PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...
- linux(ubuntu) 常用指令
1.新建文件夹 mkdir mkdir test 2.进入文件夹 cd cd test 3.创建/修改文件 vim vim a.txt 如果不存在a.txt,就会新增a.txt; 如果存在,则修改 先 ...
- [理论篇]一.JavaScript中的死连接`javascript:void(0)`和空连接`javascript:;`
void 运算符 void 运算符会对给定的表达式进行求值,然后直接返回 undefined void 运算符通常只用于获取 undefined 的原始值,一般使用 void(0)(等同于 void ...
- 【记录】css样式
记录css的样式设置,方便以后使用. 1.绝对定位,自适应父级大小css: .search-icon-delete { background: url('../../assets/images/sea ...
- WebStorm 使用webpack打包(build) Vue 静态资源无法访问(路径不对)问题
在WebStorm中使用webpack打包 (命令npm run build) 后生成在项目的dist目录下,在浏览器打开,静态资源js.css等无法加载.因为打包时,资源使用了绝对路径. 解决: 打 ...
- 从github上下载一个csv文件
when u open the raw file(i.e. csv) on github, then point to RAW button, then right click the mouse, ...
- List(JDK1.7)(2)
LinkedList List接口和Deque接口的一种双向链表实现.非同步的. 快速失败机制.ConcurrentModificationException 结点结构 插入结点 删除结点 add() ...
- IFrame跨域处理方法-Javascript
在漫长的前端开发旅途上,无可避免的会接触到ajax,而且一般情况下都是用在同一域下的ajax请求:但是如果请求是发生在不同的域下,请求就无法执行,并且会抛出异常提示不允许跨域请求,目前我没有找到明确的 ...
- HDU 1556 Color the ball (树状数组 区间更新+单点查询)
题目链接 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽&quo ...
