首先听一下官方的话:

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. Java基础之疑难知识点

    问题列表: 1. Java中子类中可以有与父类相同的属性名吗? 2. Java中子类继承了父类的私有属性及方法吗? 3. Java中抽象类到底能不能被实例化? 1. Java中子类中可以有与父类相同的 ...

  2. 源码中transient的用途

    Java的serialization提供了一种持久化对象实例的机制.当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它.为了在一个特定对象的一个域上关闭se ...

  3. vue的全局指令

    vue有四个全局指令:directive.extent.set.component directive:自定义指令 //写一个改变颜色的指令 Vue.directive('amie',function ...

  4. Ubuntu下配置支持Windows访问的Samba共享

    一.安装Ubuntu samba服务器 $ sudo apt-get install samba $ sudo apt-get install smbclient # Linux客户端测试用 二.创建 ...

  5. 递归和静态static

    function sum($n){ if($==1){ return 1; } return $n+sum($n-1); } echo sum(100); ---------------------- ...

  6. MyEclipse和Eclipse中jsp、html格式化自动排版问题

    一.myeclipse的漂亮排版设置 步骤: 在左侧快捷 “搜索” 框里面输入 html . 点击选中左侧HTML Source . line - width 是设置当前行里面有多少字符时,就换行.这 ...

  7. Web API: Client: Call a Web API from a .net client

    原文地址: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client 翻译地址:h ...

  8. nodejs express 上传文件 (格式 FormData)

    前台代码使用jQuery的ajax: <script type="text/javascript"> $(function(){ $('#file_upload').c ...

  9. [转载]8 种提升 ASP.NET Web API 性能的方法

    http://www.oschina.net/translate/8-ways-improve-asp-net-web-api-performance 英文原文:8 ways to improve A ...

  10. 生死相依:说说JQuery中die()、live()详解[翻译]

    一个web前端工程师,应该知道jquery的.live()函数,知道它是做什么用的,但是不知它是怎么样工作的,使用起来也是不得得心应手的,甚至也没听说过.die()(去掉bind事件).即使你能熟悉这 ...