由于elasticsearch7.x取消了type(类型的概念)对应数据库表的概念

kibana的配置以及安装地址:https://www.cnblogs.com/TJ21/p/12642219.html

添加一个索引

  1. PUT 索引名
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 0
  6. }
  7. }

创建映射字段

analyzer:分词器    下载地址:https://github.com/medcl/elasticsearch-analysis-ik

  1. PUT /索引名/_mapping
  2. {
  3. "properties": {
  4. "title":{
  5. "type": "text",
  6. "analyzer": "ik_max_word"
  7. },
  8. "images":{
  9. "type": "keyword",
  10. "index": false
  11. },
  12. "price":{
  13. "type": "float"
  14. }
  15. }
  16. }

查看映射关系

  1. GET /索引名/_mapping

新增数据

随机生成id

  1. POST /索引库名/_doc
  2. {
  3. "title":"大米手机",
  4. "images":"http://image.leyou.com/12479122.jpg",
  5. "price":2899.00
  6. }

自定义id

自定义id值不能重复,否则数据将会被覆盖

  1. POST /索引库名/_doc/自定义id
  2. {
  3. "title":"超米手机",
  4. "images":"http://image.leyou.com/12479122.jpg",
  5. "price":3699.00,
  6. "Saleable":true
  7. }

修改数据,

将上面自定义id的请求方式修改

  1. PUT /索引库/_doc/id
  2. {
  3. "title":"超大米手机",
  4. "images":"http://image.leyou.com/12479122.jpg",
  5. "price":3899.00,
  6. "stock": 100,
  7. "saleable":true
  8. }

删除数据

  1. DELETE /索引库名/_doc/id

查询

查询所有

  1. GET /索引库名/_search
    {
    "query": {
    "match_all": {}
    }
    }

响应内容:

  1. {
  2. "took" : 0,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "goods",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "title" : "小米手机",
  24. "images" : "http://image.leyou.com/12479122.jpg",
  25. "price" : 2699.0,
  26. "Saleable" : true
  27. }
  28. },
  29. {
  30. "_index" : "goods",
  31. "_type" : "_doc",
  32. "_id" : "mmHtSnEBVcsVh4Caiarl",
  33. "_score" : 1.0,
  34. "_source" : {
  35. "title" : "大米手机",
  36. "images" : "http://image.leyou.com/12479122.jpg",
  37. "price" : 2899.0
  38. }
  39. },
  40. {
  41. "_index" : "goods",
  42. "_type" : "_doc",
  43. "_id" : "2",
  44. "_score" : 1.0,
  45. "_source" : {
  46. "title" : "超米手机",
  47. "images" : "http://image.leyou.com/12479122.jpg",
  48. "price" : 3699.0,
  49. "Saleable" : true
  50. }
  51. },
  52. {
  53. "_index" : "goods",
  54. "_type" : "_doc",
  55. "_id" : "3",
  56. "_score" : 1.0,
  57. "_source" : {
  58. "title" : "小米电视4A",
  59. "images" : "http://image.leyou.com/12479122.jpg",
  60. "price" : 4699.0,
  61. "Saleable" : true
  62. }
  63. },
  64. {
  65. "_index" : "goods",
  66. "_type" : "_doc",
  67. "_id" : "4",
  68. "_score" : 1.0,
  69. "_source" : {
  70. "title" : "华为手机",
  71. "subTitle" : "小米",
  72. "images" : "http://image.leyou.com/12479122.jpg",
  73. "price" : 4699.0
  74. }
  75. },
  76. {
  77. "_index" : "goods",
  78. "_type" : "_doc",
  79. "_id" : "5",
  80. "_score" : 1.0,
  81. "_source" : {
  82. "title" : "oppo",
  83. "subTitle" : "小米",
  84. "images" : "http://image.leyou.com/12479122.jpg",
  85. "price" : 4899.0
  86. }
  87. }
  88. ]
  89. }
  90. }

字段解析:

  1. - took:查询花费时间,单位是毫秒
  2. - time_out:是否超时
  3. - _shards:分片信息
  4. - hits:搜索结果总览对象
  5. - total:搜索到的总条数
  6. - max_score:所有结果中文档得分的最高分
  7. - hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
  8. - _index:索引库
  9. - _type:文档类型
  10. - _id:文档id
  11. - _score:文档得分
  12. - _source:文档的源数据

# 匹配查询

GET /索引库名/_search
{
"query": {
"match": {
"title": {
"query": "小米手机电视",
"minimum_should_match": "60%"
}
}
}
}

#多字段查询

title,subTitle字段名

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "小米",
  6. "fields":["title","subTitle"]
  7. }
  8. }
  9. }

#1.词条查询

可分割的最小词条单位   title为字段名  [ "字段值" ]

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "title": ["小米","手机"]
  6. }
  7. }
  8. }

#2.多词条查询

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "title": ["小米","手机"]
  6. }
  7. }
  8. }

# 结果过滤

excludes:不显示的字段    includes: 显示的字段

  1. GET /索引库名/_search
  2. {
  3. "_source": {
  4. "excludes": "{images}"
  5. },
  6. "query": {
  7. "terms": {
  8. "title": ["小米","手机"]
  9. }
  10. }
  11. }

#布尔查询

标题一定有小米,或者价格为2699,4699

bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合

GET /索引库名/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "小米"
}
}
],
"should": [
{"terms": {
"price": [
"2699",
"2799"
]
}}
]
}
}
}

# 范围查询

价格大于等于2799 小于等于3899

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "range": {
  5. "price": {
  6. "gte": 2799,
  7. "lte": 3899
  8. }
  9. }
  10. }
  11. }

  

# 模糊查询

标题为oppo 默认允许错误一个字母,最大为两个字母 正确标题 oppo

fuzziness:配置篇里

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "title": {
  6. "value": "oope",
  7. "fuzziness": 2
  8. }
  9. }
  10. }
  11. }

# 过滤filter

不会影响查询的分数_score

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "title": "小米"
  9. }
  10. }
  11. ],
  12. "filter": [
  13. {
  14. "range": {
  15. "price": {
  16. "gte": 2699,
  17. "lte": 4999
  18. }
  19. }
  20. }
  21. ]
  22. }
  23. }
  24. }

#排序

  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": [
  6. {
  7. "range": {
  8. "price": {
  9. "gte": 2699,
  10. "lte": 4999
  11. }
  12. }
  13. }
  14. ]
  15. }
  16. },
  17. "sort": [
  18. {
  19. "price": {
  20. "order": "desc"
  21. }
  22. },
  23. {
  24. "_id":{
  25. "order": "asc"
  26. }
  27. }
  28. ]
  29. }

聚合 aggregations

聚合可以让我们极其方便的实现对数据的统计、分析。例如:

  • 什么品牌的手机最受欢迎?

  • 这些手机的平均价格、最高价格、最低价格?

  • 这些手机每月的销售情况如何?

实现这些统计功能的比数据库的sql要方便的多,而且查询速度非常快,可以实现实时搜索效果。

4.1 基本概念

Elasticsearch中的聚合,包含多种类型,最常用的两种,一个叫,一个叫度量

桶(bucket)

桶的作用,是按照某种方式对数据进行分组,每一组数据在ES中称为一个,例如我们根据国籍对人划分,可以得到中国桶英国桶日本桶……或者我们按照年龄段对人进行划分:0~10,10~20,20~30,30~40等。

Elasticsearch中提供的划分桶的方式有很多:

  • Date Histogram Aggregation:根据日期阶梯分组,例如给定阶梯为周,会自动每周分为一组

  • Histogram Aggregation:根据数值阶梯分组,与日期类似

  • Terms Aggregation:根据词条内容分组,词条内容完全匹配的为一组

  • Range Aggregation:数值和日期的范围分组,指定开始和结束,然后按段分组

  • ……

bucket aggregations 只负责对数据进行分组,并不进行计算,因此往往bucket中往往会嵌套另一种聚合:metrics aggregations即度量

度量(metrics)

分组完成以后,我们一般会对组中的数据进行聚合运算,例如求平均值、最大、最小、求和等,这些在ES中称为度量

比较常用的一些度量聚合方式:

  • Avg Aggregation:求平均值

  • Max Aggregation:求最大值

  • Min Aggregation:求最小值

  • Percentiles Aggregation:求百分比

  • Stats Aggregation:同时返回avg、max、min、sum、count等

  • Sum Aggregation:求和

  • Top hits Aggregation:求前几

  • Value Count Aggregation:求总数

  • ……

使用聚合先加入新的索引

  1. PUT /cars
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 0
  6. },
  7. "mappings": {
  8. "properties": {
  9. "color": {
  10. "type": "keyword"
  11. },
  12. "make": {
  13. "type": "keyword"
  14. }
  15. }
  16. }
  17. }

批量添加数据

  1. POST /cars/_bulk
  2. { "index": {}}
  3. { "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
  4. { "index": {}}
  5. { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
  6. { "index": {}}
  7. { "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
  8. { "index": {}}
  9. { "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
  10. { "index": {}}
  11. { "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
  12. { "index": {}}
  13. { "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
  14. { "index": {}}
  15. { "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
  16. { "index": {}}
  17. { "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

#聚合为桶

  1. GET /cars/_search
  2. {
  3. "aggs": {
  4. "color": {
  5. "terms": {
  6. "field": "color"
  7. }
  8. }
  9. }
  10. }

#桶内度量

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "color": {
  6. "terms": {
  7. "field": "color"
  8. },
  9. "aggs": {
  10. "avg_price": {
  11. "avg": {
  12. "field": "price"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

#桶内嵌套桶

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "color": {
  6. "terms": {
  7. "field": "color"
  8. },
  9. "aggs": {
  10. "avg_price": {
  11. "avg": {
  12. "field": "price"
  13. }
  14. },
  15. "mark":{
  16. "terms": {
  17. "field": "make"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

#阶梯分组

对价格进行阶梯分组,最小数量为1才显示

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "price_histogram": {
  6. "histogram": {
  7. "field": "price",
  8. "interval": 5000,
  9. "min_doc_count": 1
  10. }
  11. }
  12. }
  13. }

#范围分组

  1. GET /cars/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "price_range": {
  6. "range": {
  7. "field": "price",
  8. "ranges": [
  9. {
  10. "from": 5000,
  11. "to": 15000
  12. },
  13. {
  14. "from": 15000,
  15. "to": 20000
  16. },
  17. {
  18. "from": 20000,
  19. "to": 25000
  20. },
  21. {
  22. "from": 25000,
  23. "to":35000
  24. },
  25. {
  26. "from": 35000,
  27. "to":40000
  28. }
  29. ]
  30. }
  31. }
  32. }
  33. }

使用kibana操作elasticsearch7.x 教程的更多相关文章

  1. 在Python中使用lambda高效操作列表的教程

    在Python中使用lambda高效操作列表的教程 这篇文章主要介绍了在Python中使用lambda高效操作列表的教程,结合了包括map.filter.reduce.sorted等函数,需要的朋友可 ...

  2. Navicat操作MySQL简易教程

    前言: 日常使用 MySQL 的过程中,我们可能会经常使用可视化工具来连接 MySQL ,其中比较常用的就是 Navicat 了.平时也会遇到某些同学问, Navicat 怎么安装,如何使用等问题.本 ...

  3. Ubuntu操作系统安装使用教程 (转)

    随着微软的步步紧逼,包括早先的Windows黑屏计划.实施,逮捕番茄花园作者并判刑,种种迹象表明,中国用户免费使用盗版Windows的日子将不会太长久了,那么这个世界上有没有即免费又易用的操作系统呢? ...

  4. Kibana插件sentinl使用教程

    简介 对于Kibana的一些数据我们有时候是想要对某些字段进行持续关注的,这时候通过报警的手段就可以大幅提升对这些信息状态了解的及时性及可靠性.使用sentinl插件就可以帮助我们实现这个功能. 此教 ...

  5. kibana查询语法 使用教程

    1. 使用双引号包起来作为一个短语搜索: "like Gecko" 2. ? 匹配单个字符; * 匹配0到多个字符 例如:kiba?a, el*search ? * 不能用作第一个 ...

  6. springboot使用RestHighLevelClient7简单操作ElasticSearch7增删查改/索引创建

    本次操作是在  Windows上安装ElasticSearch7  进行操作 导入依赖 <?xml version="1.0" encoding="UTF-8&qu ...

  7. python 操作 elasticsearch-7.0.2 遇到的问题

    错误一:TypeError: search() got an unexpected keyword argument 'doc_type',得到不预期外的参数 解决方法:elasticsearch7里 ...

  8. ELK-全文检索技术-kibana操作elasticsearch

    前言:建议kibana语法一定要学好! 1       软件安装 1.1     ES的安装 第一步:解压压缩包,放到一个没有中文没有空格的位置 第二步:修改配置文件 1.  jvm.options ...

  9. kibana操作

    一些KIBANA的操作,记录下,免下次重复写 #创建索引名为kb_question的索引,并添加mapping,即各字段属性 PUT kb_question { "mappings" ...

随机推荐

  1. [译]HTML&CSS Lesson7: 设置背景和渐变色

    背景对网站的设计有重大的影响.它有利于建立网站的整体感觉,设置分组,分配优先级,对网站的可用性也有相当大的影响. 在CSS中,元素的背景可以是一个纯色,一张图,一个渐变色或者它们的组合.在我们决定如何 ...

  2. 一个轻量级的基于 .NET Core 的 ORM 框架 HSQL

    HSQL 是一种轻量级的基于 .NET Core 的数据库对象关系映射「ORM」框架 HSQL 是一种可以使用非常简单且高效的方式进行数据库操作的一种框架,通过简单的语法,使数据库操作不再成为难事.目 ...

  3. cmd 输入输出

    cmd 输入输出 首先在编写如: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> vo ...

  4. vue如何新建一个项目

    第一步:安装node 首先下载安装node 安装步骤参考:https://www.cnblogs.com/qdwz/p/10820554.html window+R打开控制命令行cmd node -v ...

  5. 对javaweb项目中web.xml重用配置的理解(个人学习小结)

    <!-- 所有的总结描述性与语言都在注释中 --><?xml version="1.0" encoding="UTF-8"?> < ...

  6. 《JavaScript 模式》读书笔记(2)— 基本技巧1

    这篇文章的主要内容,介绍了一些js编程中的基本技巧,其实这些技巧,大家在开发的过程中,或多或少都在使用,或者已经可以熟练的应用于自己的代码或项目中了.那么,这篇文章,就一起来回顾下这些“基本技巧”. ...

  7. R语言实战(二) 创建数据集

    2.1 数据集的概念 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和变量(variable),数据库分析师则称其为记录(record)和字段(field),数据 ...

  8. [转载]Linux服务器丢包故障的解决思路及引申的TCP/IP协议栈理论

    Linux服务器丢包故障的解决思路及引申的TCP/IP协议栈理论 转载至:https://www.sdnlab.com/17530.html 我们使用Linux作为服务器操作系统时,为了达到高并发处理 ...

  9. Python专题——五分钟带你了解map、reduce和filter

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题第6篇文章,给大家介绍的是Python当中三个非常神奇的方法:map.reduce和filter. 不知道大家看到ma ...

  10. web自动化原理

    在说原理之前我想说下我所理解的selenium: (1).支持多语言,多平台,多浏览器 (2).它是一个工具包 (3).提供所有的网页操作api,是一个功能库 通过selenium来实现web自动化, ...