第1部分 Elasticsearch基础
一、安装
es端口:9200
kibana端口:5601
brew install elasticsearch
brew install elasticsearch
brew services start elasticsearch
brew services start kibana
二、elastic交互-基本
1、集群信息
访问数据模式REST
<HTTP Verb> /<Index>/<Type>/<ID>
查看集群健康检查
$ curl -X GET "localhost:9200/_cat/health?v"
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1551424260 07:11:00 elasticsearch_yjn green 1 1 0 0 0 0 0 0 - 100.0%
查看节点列表
$ curl -X GET "localhost:9200/_cat/nodes?v"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 24 99 23 2.89 mdi * d-0YrG2
查看所有指数
$ curl -X GET "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 oRuiVW1wSbWyFK4Wu0k6MA 1 0 1 0 3.6kb 3.6kb
2、索引
创建索引index
PUT /customer?pretty
GET /_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .kibana_1 oRuiVW1wSbWyFK4Wu0k6MA 1 0 1 0 3.6kb 3.6kb
yellow open customer Fe4Y2hU2Rcek0kl7SYZoKQ 5 1 0 0 1.1kb 1.1kb
- 目前只有一个节点,默认为此索引值创建了一个副本,所以为黄色。
删除索引
DELETE /customer?pretty
3、文档
索引文档
PUT /customer/_doc/1?pretty
{
"name": "John Doe"
}
结果
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询
GET /customer/_doc/1?pretty
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
替换文档
POST /customer/_doc?pretty
{
"name": "Jane Doe"
}
- 不指定id会随机产生一个id,创建相同id的已经存在就会被替换。
更新文档
POST /customer/_doc/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
- 结果:
GET /customer/_doc/1?pretty
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 3,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Jane Doe",
"age" : 20
}
}
使用脚本更新文档:
POST /customer/_doc/1/_update?pretty
{
"script" : "ctx._source.age += 5"
}
- 结果:
GET /customer/_doc/1?pretty
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Jane Doe",
"age" : 25
}
}
删除文档:
DELETE /customer/_doc/2?pretty
批量处理
POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
GET /customer/_doc/_search
Bulk API不会因其中一个操作失败而失败,如果单个操作因任何原因失败,它将继续处理其后的其余操作。
批量API返回时,它将为每个操作提供一个状态(按照发送的顺序),以便您可以检查特定操作是否失败。状态:"result" : "noop"(created,deleted,updated,not_found")
4、数据集处理例子
添加数据集
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
curl "localhost:9200/_cat/indices?v"
查询
GET /bank/_search?q=*&sort=account_number:asc&pretty
- q=* 表示匹配索引中的所有文档
- sort=account_number:asc 结果按照account_number升序排序
- pretty 好看的格式
使用查询表达式:结果同上
GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
结果分析:
{
"took" : 8, #es执行搜索的时间ms
"timed_out" : false, #是否超时
"_shards" : {
"total" : 5, #搜索到了多少分片
"successful" : 5, #成功的分片数
"skipped" : 0,
"failed" : 0
},
"hits" : { #查询结果
"total" : 1000, #符合条件的文档总数
"max_score" : null,
"hits" : [ { #实际的搜索结果数组(默认前10个文档)
"_index" : "bank",
"_type" : "_doc",
"_id" : "0",
"sort": [0],
"_score" : null,
"_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
}, {
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"sort": [1],
"_score" : null,
"_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}, ...
]
}
}
5、查询
查询语言
GET /bank/_search
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
- from:从那个文档索引开始,不指定from默认为0
- size:从from开始返回的文档数
GET /bank/_search
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}
- 按照balance降序排序
更复杂的查询
GET /_all/tweet/_search?q=name:(mary john) +date:>2014-09-10 +(aggregations geo)
- _all在所有索引中
- 在tweet类型中
- name 字段中包含 mary 或者 john
- date 值大于 2014-09-10
- _all 字段包含 aggregations 或者 geo
6、聚合
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
三、分析器
测试分析器
可以使用 analyze API 来看文本是如何被分析的
GET /_analyze
{
"analyzer": "standard",
"text": "Text to analyze"
}
结果:
token 是实际存储到索引中的词条。 position 指明词条在原始文本中出现的位置。 start_offset 和 end_offset 指明字符在原始字符串中的位置。
映射
查看索引bank类型_doc的映射
GET /bank/_mapping/_doc
四、请求体查询 及 五、排序与相关性
详情查看:
(https://www.cnblogs.com/yangjianan/p/10525925.html)
第1部分 Elasticsearch基础的更多相关文章
- ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程
1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...
- Elasticsearch 基础入门
原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...
- ElasticSearch 基础 1
ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...
- Elasticsearch基础但非常有用的功能之二:模板
文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484584&idx=1&sn=accfb65 ...
- 最完整的Elasticsearch 基础教程
翻译:潘飞(tinylambda@gmail.com) 基础概念 Elasticsearch有几个核心概念.从一开始理解这些概念会对整个学习过程有莫大的帮助. 接近实时(NRT) Ela ...
- ELK 之一:ElasticSearch 基础和集群搭建
一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...
- Elasticsearch基础教程
Reference: http://blog.csdn.net/cnweike/article/details/33736429 基础概念 Elasticsearch有几个核心概念.从一开始理解这些概 ...
- elasticsearch基础概念
接近实时(NRT) Elasticsearch是一个接近实时的搜索平台.这意味着,从索引一个文档直到这个文档能够被搜索到有一个轻微的延迟(通常是1秒). 集群(clu ...
- Elasticsearch基础知识分享
1. Elasticsearch背景介绍 Elasticsearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elast ...
- Elasticsearch基础教程分享
基础及相关资料 首先我们先了解一下什么是Elastisearch,Elasticsearch(简称es)是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,具有HTTP ...
随机推荐
- 记录:http协议+response+request+session+cookie
1.http协议 http协议也叫作超文本传输协议,定义了浏览器向怎样向服务器请求资源和服务器怎样将资源传给浏览器.http协议是面向事务的应用层协议,是万维网能够传递资源的可靠保障. 目前http协 ...
- Spring Boot 2.x(十六):玩转vue文件上传
为什么使用Vue-Simple-Uploader 最近用到了Vue + Spring Boot来完成文件上传的操作,踩了一些坑,对比了一些Vue的组件,发现了一个很好用的组件--Vue-Simple- ...
- Dart 库预览
容导航 dart:core - numbers, collections, strings, and more dart:async - asynchronous programming dart:m ...
- OpenCV 学习笔记(8)彩色图像RGB通道的分离、合并与显示
https://blog.csdn.net/ZYTTAE/article/details/42234989 由于算法的需要,需要把彩色图像的R.G.B值分离出来,OpenCV中正好有split() 和 ...
- ent 基本使用八 索引
我们可以在ent 的schema 中定义index 可以方便的控制数据约束,使用索引可以加速我们的访问以及数据的唯一性处理 配置字段索引 多字段索引 package schema import ( ...
- JS函数基础
一.函数 1.函数是什么 具有特定功能的n条语句的封装体. 只有函数是可执行的,其它类型的数据是不可执行的. 函数也是对象. 2.为什么要用函数 提高代码复用 便于阅读和交流 3.如何定义函数 函数声 ...
- 第04组alpha冲刺(3/4)
队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...
- linux高性能服务器编程 (四) --TCP/IP通信案例
第四章 TCP/IP通信案例 HTTP代理服务器的大致工作原理 在HTTP通信链上,客户端和服务器之间通常存在某些中转代理服务器.它们提供对目标资源的中转访问.一个HTTP请求可能被多个 ...
- Tecplot显示周期和对称算例
源视频链接:https://pan.baidu.com/s/1HdU3nsti8qLZhXvISxsSFA 提取码: 3kfu 模型链接:https://pan.baidu.com/s/1CQCGL7 ...
- 阿里P8架构师深度概述分布式架构
简介 作为一名架构师,我们要专业,要能看懂代码,及时光着臂膀去机房,也能独挡一面!及时同事搞不定问题,或者撂挑子,你也能给老大一个坚定的眼神:不怕,有我在!还能在会议室上滔滔不绝,如若无人,让不懂技术 ...