基本概念

Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。

对比(MySQL)关系

索引(indices)----------------------Databases 数据库
类型(type)-------------------------Table 数据表
文档(Document)---------------------Row 行
字段(Field)------------------------Columns 列
映射配置(mappings)字段的数据类型、属性、是否索引、是否存储等特性

ES常用命令

1.创建索引curl -XPUT http://localhost:9200/music/    #music为索引名称
2.查看全部索引curl -XGET http://localhost:9200/_cat/indices?v
3.插入文档curl -H "Content-Type:application/json" -XPUT http://localhost:9200/music/songs/1 -d '{"name":"Deck the Halls","year":1885,"lyrics":"Fa la la la la"}'
4.查看文档curl -XGET http://localhost:9200/music/songs/1
5.更新文档ID相同则更新,不同则新增
6.删除文档curl -XDELETE http://localhost:9200/music/songs/1
7.查看索引下全部记录curl http://localhost:9200/music/songs/_search
8.删除索引curl -XDELETE http://localhost:9200/music
9.删除全部索引curl -XDELETE http://localhost:9200/_all 或者 curl -XDELETE http://localhost:9200/*

IK分词器下载地址

https://github.com/medcl/elasticsearch-analysis-ik/releases
  1. 下载后解压重命名文件夹为 analysis-ik
  2. 将该文件夹放入Elasticsearch的plugins目录,重启Elasticsearch

使用kibana常用命令

概念:Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求。kibana的控制台,对http请求进行简化。

创建索引的请求格式:
请求方式:PUT
请求路径:/索引库名
请求参数:json格式

----------------------------A.索引操作----------------------------

1.创建索引

#创建--某个test索引PUT test/{"settings":{                    可以不设置默认5和1"index":{"number_of_shards":5,      是数据分片数,默认为5,有时候设置为3"number_of_replicas":1     是数据备份数,如果只有一台机器,设置为0}}}#删除--某个索引DELETE test/#修改--副本数量设置
PUT test/_settings
{"number_of_replicas":2
}#查--某个索引GET test/#查--所有索引信息GET _cat/indices?v#查--test的设置信息GET test/_settings#查--所有的设置信息GET _all/_settings

2.创建映射

索引有了,接下来肯定是添加数据。但是,在添加数据之前必须定义映射。
什么是映射?
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等。

映射说明:

PUT /索引库名/_mapping/类型名称(默认_doc类型)
{"properties": {"title": {    字段名"type": "text",      字段类型,text、long、short、date、integer、object、keyword:不可分词 基本数据类型:long、interger、short、byte、double、float、half_float浮点数的高精度类型:scaled_float等."index": true,                 是否索引,默认为true"store": true,                 是否存储,默认为false"analyzer": " ik_max_word"      分词器 ik_max_word和ik_smar},}
}

映射示例:

/*******************新增时间类型映射************************/
PUT test/_mapping/goods
{"properties": {"title": {"type": "text","analyzer": "ik_max_word"}
}
/*******************新增时间类型映射************************/
PUT test1/_mapping/_doc
{"properties": {"TimeFormat": {"type": "date","format": "yyyy-MM-dd HH:mm:ss"}}
}

3.查看映射

GET /test/_mapping

type字段类型介绍

String类型,又分两种:

text:可分词,不可参与聚合
keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合

Numerical:数值类型,分两类

基本数据类型:
long、interger、short、byte、double、float、half_float浮点数的高精度类型:
scaled_float
需要指定一个精度因子,比如10或100。
elasticsearch会把真实值乘以这个因子后存储,取出时再还原。

Date:日期类型

elasticsearch可以对日期格式化为字符串存储,
但是建议我们存储为毫秒值,存储为long,节省空间。

----------------------------B.数据操作----------------------------

1.增加数据

注:修改必须指定id,id对应文档 存在/不存在,则修改/新增。
如下: id=1存在,则为修改"price":8888.00

//指定id为1
PUT /test/goods/1
{"title":"华为手机","images":"http://image.baidu.com/165422.jpg","type": "text","price":8888.00
}
#2.使用_builk 批量添加文档
PUT student //假设一张表,含有name和age字段
{"mappings" : {"properties" : {"name" : {"type" : "keyword"},"age" : {"type" : "integer"}}}
}
// 批量添加文档
POST _bulk
{ "index" : { "_index" : "student", "_id" : "1" } }
{ "name" : "张三" }
{ "index" : { "_index" : "student", "_id" : "2" } }
{ "name" : "李四", "age": 10 }
{ "index" : { "_index" : "student", "_id" : "3" } }
{ "name" : "王五", "age": 11 }

2.删除数据

DELETE /索引库名/类型名/id值

#1.删除一条记录(文章)
DELETE /test/goods/1#2.删除一个type(表)
#DELETE test/goods
#尝试了一下,结果不行,表明ES5不再支持删除type#3.删除整个 index (库)
DELETE test

3.修改数据

#1.如果存在1则为修改,没有则新增数据
PUT /test/goods/1
{"title":"华为手机","images":"http://image.baidu.com/165422.jpg","price":8888.00
}# 2.相当于update goods set prices = 2000 where id = 1
POST test/goods/1/_update
{"doc":{"prices":2000}
}
#3.批量修改'包含' tid字段>0,将cate字段赋值=0
#lang代表使用的脚本方式
#params需要脚本传递的参数
#inline脚本字符串 (新版本请使用source)
GET _search
POST test/_doc/_update_by_query?confilcts=proceed
{"query":{“range”: {"tid": {"gt": 0}}},"script": {"lang": "painless","source": "ctx._source.cate=0"}
}
#4.批量修改'不包含' tid字段,将cate字段赋值=1
POST test/_doc/_update_by_query?conflicts=proceed
{"query":{"bool":{"must_not": {"exists": {"field": "tid"}}}},"script": {"lang": "painless","source": "ctx._source.cate=1"}
}
#5.修改两个字段值
POST test/_doc/_update_by_query
{"script": {"lang": "painless","source": "ctx._source.cate = paroms.cate";ctx._source.tid =  paroms.tid,"paroms": {"cate'":  0 ,."tid": 1}}
}
#6.创建 mapping 时,可以为字符串(专指 keyword)
#指定 ignore_above ,用来限定字符长度。
#超过 ignore_above 的字符会被存储,但不会被索引。
#在动态生成的 mapping 中,keyword类型会被设置ignore_above: 256。
PUT my_index/_mappings
{"properties" : {"note" : {"type" : "keyword","ignore_above": 2}}
}
#7.修改两个字段值
POST test/_update_by_query
{"script": { #修改"source": "ctx._source['字段1']='改的值'; ctx._source['字段2']='改的值';},"query": { #条件"bool": {"must": [{"match": {"字段":"值"}},{"match": {"字段":"值"}}]}}}

4.查看数据

query :代表查询对象
match_all :代表查询所有
match 类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系。
term 查询被用于精确值 匹配,精确值可能是数字、时间、布尔或者那些未分词的字符串。
查询类型:
例如: match_all , match , term , range 等等

#不同风格写法#这个语句相当于sql中的  select title,city from goods where id = 1
GET test/goods/1?_source=title,city#这个语句相当于sql中的  select * from goods where id = 1
GET test/goods/1?_source
#1.查询es库中所有索引
GET _search
{"query":{"match_all":{}}
}
#2.查询test索引 goods类型所有
GET test/goods/_search
{"query":{"match_all":{}}
}
#3.and同时包含 华为和 手机 的词条才会被搜索到
GET /test/goods/_search
{
"query":{"match": {"title": {"query": "华为手机","operator": "and"}}}
}
#4.结果过滤_source  只显示title和price字段  只显示0到100数据
GET test/goods/_search
{"query":{"match_all":{}},“_source”:["title","price"],"from": 0,"size": 100
}GET test/goods/_search
{"query":{"term":{"price":2699.00},“_source”:["title","price"],"from": 0,"size": 100
}
#5.布尔组合bool 把查询通过 must (与)、 must_not (非)、 should(或)的方式进行组合
GET /test/goods/_search
{"query":{"bool":{"must": {"match": {"title": "华为"}},"must_not": { "match": {"title": "电视" }},"should": { "match": { "title": "手机" }}},}
}
#6.查包含price 字段 的数据  must_not为不包含
GET test/goods/_search
{"query":{"bool":{"must': {"exists": {"field": "price"}}}},
}
#7.单词条精确匹配(term) 数值精确price字段为2699的数据
GET /test/goods/_search
{"query":{"term":{"price":2699.00}}
}
#8.多词条精确匹配(terms) 数值精确price字段多数值的数据
GET /test/goods/_search
{"query":{"term":{"price": [2699.00,2899.00,3899.00]}}
}
#9.范围查询(range) 查询找出那些落在指定区间内的数字或者时间
gt 大于
gte 大于等于
lt 小于
lte 小于等于GET /test/goods/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
#10.模糊查询(fuzzy)title包含手机  fuzziness 来指定允许的编辑距离
GET /test/goods/_search
{
"query":{
"fuzzy": {
"title": "手机",
//"fuzziness":1
}
}
}
#11.过滤(filter) 有查询条件进行过滤
GET /test/goods/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "华为手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
#12.过滤(filter) 没有查询条件,不希望进行评分,直接过滤,我们可以使用 constant_score
GET /test/goods/_search
{
"query":{
"constant_score": {
"filter": {
"range":{"price":{"gt":2000.00,"lt":3000.00}}
}
}
}
#13.单字段排序
GET /bdqn/goods/_search
{"query": {"match": {"title": "华为手机"}
},
"sort": [{"price": {"order": "desc"}}]
}
#14.多字段排序-----使用 price和 _score(得分) 进行查询,
#并且匹配的结果首先按照价格排序,然后按照相关性得分排序
GET bdqn/goods/_search
{"query":{"bool":{"must":{ "match": { "title": "华为手机" }},"filter":{"range":{"price":{"gt":200000,"lt":300000}}}}
},
"sort": [{"price": {"order": "desc" }},{"_score": { "order": "desc" }}]
}

-----------------------查询实战1-----------------------

创建索引:

PUT student
{"mappings" : {"properties" : {"name" : {"type" : "keyword"},"age" : {"type" : "integer"},"height": {"type": "integer"}}}
}

查询 11 岁的张三的信息:
因为没有数据,所以下面的查询结果为空:

POST student/_search
{"query": {"bool": {"must": [{ "term" : { "name": "张三" } },{ "term" : { "age": 11 } }]}}
}

查询 11岁、12岁的所有学生信息
注意,是terms,不是 term。

POST student/_search
{"query": {"bool": {"must": [{ "terms" : { "age": [11, 12] } }]}}
}

查询小于11岁的学生信息

POST student/_search
{"query": {"bool": {"must": [{"range": {"age": {"lt": "11"}}}]}}
}

查询11岁到13岁的学生信息

POST student/_search
{"query": {"bool": {"must": [{"range": {"age": {"gte": "11","lte": "13"}}}]}}
}

查询有身高 height 记录的学生

POST student/_search
{"query": {"exists": {"field": "height"}}
}

查询没有身高 height 记录的学生

POST student/_search
{"query": {"bool": {"must_not": [{"exists": { "field": "height"}}]}}
}

在 ES 2.2.0 之前有一个 missing 指令,效果相同。不过已经被废弃。

是否存在11岁的学生

POST student/_search
{"query": {"bool": {"must": [{"term": {"age": 11}}]}},"from": 0,"size": 1
}

若结果中有记录,则认为存在。

11 岁的学生总人数

方法1:


# 请求POST student/_count
{"query": {"bool": {"must": [{"term": {"age": 11}}]}}
}# 响应
{"count" : 2,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0}
}

方法2:使用聚合查询

# 请求
POST student/_search
{"aggs":{"age_count": {"terms": {"field": "age"}}},"size": 0
}# 响应
{"took" : 71,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"age_count" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 11,"doc_count" : 2},{"key" : 10,"doc_count" : 1},{"key" : 12,"doc_count" : 1}]}}
}

可以看到,11 岁的有2个,10岁的1个,12岁的1个。

方法3:查询后进行聚合:

# 请求
POST student/_search
{"query": {"bool": {"must": [{"term": {"age": 11} }]}},"aggs":{"age_count": {"terms": {"field": "age"}}},"size": 0
}# 响应
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"age_count" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 11,"doc_count" : 2}]}}
}

方法4:

# 请求
POST student/_search
{"size": 0,"aggregations": {"group_by_age": {"aggregations": {"count_age": {"value_count": {"field": "_index"}}},"terms": {"field": "age"}}}
}# 响应
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"group_by_age" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 11,"doc_count" : 2,"count_age" : {"value" : 2}},{"key" : 10,"doc_count" : 1,"count_age" : {"value" : 1}},{"key" : 12,"doc_count" : 1,"count_age" : {"value" : 1}}]}}
}

学生的平均岁数
方式1:

# 请求
POST student/_search
{"aggs":{"age_stat": {"stats": {"field": "age"}}},"size": 0
}# 响应
{"took" : 45,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"age_stat" : {"count" : 4,"min" : 10.0,"max" : 12.0,"avg" : 11.0,"sum" : 44.0}}
}

stats 指令,会计算出指定字段的 count、min、max、avg、sum。

方式2:

# 请求
POST student/_search
{"aggs":{"age_stat": {"avg": {"field": "age"}}},"size": 0
}# 响应
{"took" : 5,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"age_stat" : {"value" : 11.0}}
}

最高的学生是谁?
最高的学生可能有多个。

方式1:找到最高的身高值,然后根据身高搜索学生信息。

方式2:按照身高排序。找到第1个的身高,然后根据身高搜索所有学生信息:

POST student/_search
{"query": {"match_all": {}},"sort" : [{"height": {"order": "desc"}}],"from": 0,"size": 1
}

每个年龄的平均身高是多少?

# 请求
POST student/_search
{"size": 0,"aggregations": {"group_by_age": {"aggregations": {"avg_height": {"avg": {"field": "height"}}},"terms": {"field": "age"}}}
}# 响应
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"group_by_age" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 11,"doc_count" : 2,"avg_height" : {"value" : 109.5}},{"key" : 10,"doc_count" : 1,"avg_height" : {"value" : 112.0}},{"key" : 12,"doc_count" : 1,"avg_height" : {"value" : null}}]}}
}

获取每个年龄的平均身高,并按照年龄从小打大排序
方式1:

# 请求
POST student/_search
{"size": 0,"aggregations": { "group_by_age": {"aggregations": {"avg_height": {"avg": {"field": "height"}}},"terms": {"field": "age","order": {"_term": "asc"}}}}
}# 响应 (响应中指出 _term 已经废弃,应使用 _key)
#! Deprecation: Deprecated aggregation order key [_term] used, replaced by [_key]
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"group_by_age" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 10,"doc_count" : 1,"avg_height" : {"value" : 112.0}},{"key" : 11,"doc_count" : 2,"avg_height" : {"value" : 109.5}},{"key" : 12,"doc_count" : 1,"avg_height" : {"value" : null}}]}}
}

方式2:

POST student/_search
{"size": 0,"aggregations": { "group_by_age": {"aggregations": {"avg_height": {"avg": {"field": "height"}},"bucket_sort_by_avg_height": {"bucket_sort": {"sort": [{"_key": {"order": "asc"}}]}}},"terms": {"field": "age"}}}
}

获取每个年龄的平均身高,并按照平均身高从大到小排序

# 请求POST student/_search
{"size": 0,"aggregations": { "group_by_age": {"aggregations": {"avg_height": {"avg": {"field": "height"}},"bucket_sort_by_avg_height": {"bucket_sort": {"sort": [{"avg_height": {"order": "desc"}}]}}},"terms": {"field": "age"}}}
}# 响应
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 4,"relation" : "eq"},"max_score" : null,"hits" : [ ]},"aggregations" : {"group_by_age" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : 10,"doc_count" : 1,"avg_height" : {"value" : 112.0}},{"key" : 11,"doc_count" : 2,"avg_height" : {"value" : 109.5}}]}}
}

kibana对Elasticsearch操作命令 侵删(持续更新)相关推荐

  1. elasticsearch下载太慢在国内, 我把包放到了云盘上,还有kibana,logstash.有需要自取,持续更新版本

    elasticsearch下载太慢在国内, 我把包放到了云盘上,还有kibana,logstash.有需要自取,持续更新版本 国内下载elasticsearch哪叫一个慢, 为了方便大家学习, 博主将 ...

  2. ElasticSearch搜索引擎详解-持续更新中

    ElasticSearch搜索引擎详解 1. ElasticSearch概述 1.1 elasticsearch是什么 1.2 全文搜索引擎 1.3 elasticsearch and solr 1. ...

  3. elasticsearch+kibana使用中踩的坑,持续更新中。

    1存进elasticsearch中的数据在kibana的discover中看不到,但是通过_id在Dev Tools可以查到,也可以直接用python取出来.最后发现存进去的时候没加时间戳. doc ...

  4. 转:elasticsearch下载太慢在国内, 我把包放到了云盘上,还有kibana,logstash.有需要自取,持续更新版本

    转载地址:https://blog.csdn.net/weixin_37281289/article/details/101483434 国内下载elasticsearch哪叫一个慢, 为了方便大家学 ...

  5. Android学习相关文章汇总(持续更新,文章均来自网络,转侵删)

    本文主要是记录在使用Android踩坑时的一些有用的文章.以及Android进阶文章等相关链接的记录,并且附带一些简单的备注等.主要用来备忘.方便日后查看.会持续更新 1. AS相关问题 项目第一次g ...

  6. 110道 Elasticsearch面试题及答案(持续更新)

    最新Elasticsearch面试题[附答案解析]ES面试题及答案,ES最新面试题及答案,ES面试题新答案已经全部更新完了,有些答案是自己总结的,也有些答案是在网上搜集整理的.这些答案难免会存在一些错 ...

  7. Java 最常见的 10000+ 面试题及答案整理:持续更新

    Java面试题以及答案整理[最新版]Java高级面试题大全(2021版),发现网上很多Java面试题都没有答案,所以花了很长时间搜集,本套Java面试题大全,汇总了大量经典的Java程序员面试题以及答 ...

  8. 关于大数据相关的问答汇总,每天持续更新中哦~

    NO.1 想要学好大数据需掌握哪些技术? 答:1,Java编程技术 Java编程技术是大数据学习的基础,Java是一种强类型语言,拥有极高的跨平台能力,可以编写桌面应用程序.Web应用程序.分布式系统 ...

  9. JAVA面试大全(持续更新中...)

    本文旨在收集Java面试过程中出现的问题,力求全面,仅作学习交流,欢迎补充,持续更新中-,部分段落选取自网上,部分引用文章已标注,部分已记不清了,如侵权,联系本人 Java基础 1.面向对象的概述 面 ...

最新文章

  1. python话雷达图-Python 详解雷达图/蛛网图
  2. 服务器内存技术知识充电
  3. Educational Codeforces Round 81 (Rated for Div. 2) B. Infinite Prefixes 数学
  4. 二叉排序树和平衡二叉排序树
  5. Leetcode--17.电话号码的字母组合
  6. mysql seconds_behind_master_MySQL中的seconds_behind_master的理解
  7. C语言 strcat函数实现
  8. 其它 博客园 自己写的文章 标题含有小写字母 查看文章时 标题就变成大写的了...
  9. java io流不关闭_Java IO流关闭问题的深入研究
  10. 微服务、容器、云原生、Kubernetes、SOA、PaaS平台、Devops 之间的关系
  11. 数学软件Maple使用教程
  12. urule决策引擎实现增量打包部署
  13. 纹理分析方法:共生矩阵的计算
  14. knn代码实现+可视化
  15. mapreduce流量统计与自定义分区算法:手机号码按归属地输出
  16. [论文阅读] (15)英文SCI论文审稿意见及应对策略学习笔记总结(letpub爬虫)
  17. c语言 m个数 取n个数,本题要求编写程序,根据公式Cnm=m!(n−m)!n!算出从n个不同元素中取出m个元素(m≤n)的组合数。...
  18. iOS 应用创建APNS 和 VOIP的pem证书流程
  19. View的实现和优化相关面试题
  20. spring boot多数据库数据源启动报错“required a single bean, but 2 were found”的正确解决办法

热门文章

  1. 1、猜数字游戏:一个类A有两个成员变量v、num,v有一个初值100。定义一个方法guess,对A类的成员变量v,用num进行猜。如果大了则提示大了,小了则提示小了。等于则提示猜测成功。在main方法
  2. java生成文件夹_java 文件和文件夹的创建
  3. java案例之吃货联盟订餐系统
  4. 二维激光SLAM( 使用Laser Scan Matcher )
  5. 米拓模板:软件信息公司网站模板推荐
  6. 企业如何规划直播带货?
  7. 进程同步——临界区(操作系统)
  8. ansible 特殊变量
  9. Java高级编程架构——Spring实战:Spring初探
  10. 前台离岗提示语_商品住宅前台会客区标牌提示语