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 风格的搜索和数据分析引 ...
随机推荐
- redis 新开端口号
2012 ps aux | grep redis 2013 cd /usr/local/redis/ 2014 ls 2015 cd etc/ 2016 ls 2017 cp redis.conf r ...
- 「Vue」vue cli3中axios的基本用法
1.安装axiosnpm i axios -S2.main.js中设置import axios from 'axios'Vue.prototype.$axios = axiosPS:这里有个小坑,ax ...
- webapi框架搭建-安全机制(三)-简单的基于角色的权限控制
webapi框架搭建系列博客 上一篇已经完成了“身份验证”,如果只是想简单的实现基于角色的权限管理,我们基本上不用写代码,微软已经提供了authorize特性,直接用就行. Authorize特性的使 ...
- 2015/11/6用Python写游戏,pygame入门(6):控制大量的对象
昨天我们已经实现了这个游戏的三个基本类. 但是现在它还是没办法做成一个适合玩的游戏,毕竟只有一架敌机的游戏是很乏味的.所以,我们需要好多子弹,也需要好多敌机. 所以,我们要创建list,这个list存 ...
- JAVA编程之——反射Reflect
说到反射,首先要说一下Java中的类和对象. 在Java中万事万物皆对象(有两个 例外,一个是普通数据类型,另一个是静态的东西,静态的东西不是对象的,是属于类的). 在Java中,类也是对象,类是ja ...
- Elasticsearch之Java实战
资料 http://www.cnblogs.com/kamong/p/6099914.html 搭建Elasticsearch服务器
- Mycat从入门到放弃
https://blog.csdn.net/u013235478/article/details/53178657
- 面向对象__construct(构造方法)、__destruct(析构方法)
//1.创建子类的时候构造函数会直接取创建函数时传的参数,例如下面例子中构造函数直接取了new Person("张三","男", 20);里面的3个参数. // ...
- SDL封装的系统操作(转载)
Andrew Haung bluedrum@163.com SDL封装很多操作系统的功能,为了保证SDL程序可移植性,最好尽量用这一些封装函数,哪果没有的话,才使用各种操作本地函数. 对于如何封各个 ...
- aarch64_l4
livestreamer-1.12.2-7.fc26.noarch.rpm 2017-02-11 17:38 537K fedora Mirroring Project lizardfs-adm-3. ...
