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

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

添加一个索引

PUT 索引名
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}

创建映射字段

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

PUT /索引名/_mapping
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float"
}
}
}

查看映射关系

GET /索引名/_mapping

新增数据

随机生成id

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

自定义id

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

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

修改数据,

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

PUT /索引库/_doc/id值
{
"title":"超大米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}

删除数据

DELETE /索引库名/_doc/id值

查询

查询所有

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

响应内容:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"title" : "小米手机",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 2699.0,
"Saleable" : true
}
},
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "mmHtSnEBVcsVh4Caiarl",
"_score" : 1.0,
"_source" : {
"title" : "大米手机",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 2899.0
}
},
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "超米手机",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 3699.0,
"Saleable" : true
}
},
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "小米电视4A",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 4699.0,
"Saleable" : true
}
},
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"title" : "华为手机",
"subTitle" : "小米",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 4699.0
}
},
{
"_index" : "goods",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"title" : "oppo",
"subTitle" : "小米",
"images" : "http://image.leyou.com/12479122.jpg",
"price" : 4899.0
}
}
]
}
}

字段解析:

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

# 匹配查询

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

#多字段查询

title,subTitle字段名

GET /索引库名/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields":["title","subTitle"]
}
}
}

#1.词条查询

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

GET /索引库名/_search
{
"query": {
"terms": {
"title": ["小米","手机"]
}
}
}

#2.多词条查询

GET /索引库名/_search
{
"query": {
"terms": {
"title": ["小米","手机"]
}
}
}

# 结果过滤

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

GET /索引库名/_search
{
"_source": {
"excludes": "{images}"
},
"query": {
"terms": {
"title": ["小米","手机"]
}
}
}

#布尔查询

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

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

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

# 范围查询

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

GET /索引库名/_search
{
"query": {
"range": {
"price": {
"gte": 2799,
"lte": 3899
}
}
}
}

  

# 模糊查询

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

fuzziness:配置篇里

GET /索引库名/_search
{
"query": {
"fuzzy": {
"title": {
"value": "oope",
"fuzziness": 2
}
}
}
}

# 过滤filter

不会影响查询的分数_score

GET /索引库名/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": 2699,
"lte": 4999
}
}
}
]
}
}
}

#排序

GET /索引库名/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"price": {
"gte": 2699,
"lte": 4999
}
}
}
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_id":{
"order": "asc"
}
}
]
}

聚合 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:求总数

  • ……

使用聚合先加入新的索引

PUT /cars
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}

批量添加数据

POST /cars/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

#聚合为桶

GET /cars/_search
{
"aggs": {
"color": {
"terms": {
"field": "color"
}
}
}
}

#桶内度量

GET /cars/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

#桶内嵌套桶

GET /cars/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"mark":{
"terms": {
"field": "make"
}
}
}
}
}
}

#阶梯分组

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

GET /cars/_search
{
"size": 0,
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 5000,
"min_doc_count": 1
}
}
}
}

#范围分组

GET /cars/_search
{
"size": 0,
"aggs": {
"price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 5000,
"to": 15000
},
{
"from": 15000,
"to": 20000
},
{
"from": 20000,
"to": 25000
},
{
"from": 25000,
"to":35000
},
{
"from": 35000,
"to":40000
}
]
}
}
}
}

使用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. 使用QT绘制一个多边形

    目录 1. 概述 2. 实现 2.1. 代码 2.2. 解析 3. 结果 1. 概述 可以通过QT的重绘事件和鼠标事件来绘制多边形,最简单的办法就是在继承QWidget的窗体中重写paintEvent ...

  2. 前端每日实战:52# 视频演示如何用纯 CSS 创作一个小球绕着圆环盘旋的动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKxyWo 可交互视频 此视频是可 ...

  3. java线程组

    1 简介 一个线程集合.是为了更方便地管理线程.父子结构的,一个线程组可以集成其他线程组,同时也可以拥有其他子线程组. 从结构上看,线程组是一个树形结构,每个线程都隶属于一个线程组,线程组又有父线程组 ...

  4. Java锁的理解

    目录: 1.为什么要使用锁? 2.锁的类型? 1.为什么要使用锁? 通俗的说就是多个线程,也可以说多个方法同时对一个资源进行访问时,如果不加锁会造成线程安全问题.举例:比如有两张票,但是有5个人进来买 ...

  5. ubunto python + vnstat 限制每天流量使用 使用iptables

    上次想使用 iptables 转发80 端口,试了一段时间,没有成功.哪位知道是什么原因,还麻烦告诉我. 这次使用 iptables 禁用 80 443 出站,经过试验可以成功. 通过 iptable ...

  6. pyppeteer基本使用demo

    # -*- coding: utf-8 -*- # 类似selenium,支持异步,不需要再单独安装环境,pyppeteer自动安装环境 # 异步await要写到一个函数的内部 from pyppet ...

  7. C#如何实现大小写转换

    2020-03-16  每日一例第8天 1.新建窗体应用程序,拖入label/textbox/radiobutton控件,并改text值:  2.button控件输入代码: if (radioButt ...

  8. C++ 按行读取文件并打印

    #include<iostream> #include<fstream> #include<string> #include <vector> #inc ...

  9. 【Weiss】【第03章】练习3.22、3.23、3.24:无代码题,栈的思考题

    [练习3.22] a.提出支持栈的Push和Pop操作以及第三种操作FindMin的数据结构,其中FindMin 返回该数据结构的最小元素,所有操作在最坏情况下的运行时间都是O(1). b.证明,如果 ...

  10. 数字逻辑与EDA设计

    目录 第一章 数字逻辑基础 1.1数制与码制★★★ 数制 码制 1.2基本及常用的逻辑运算★★ 1.2逻辑函数表示方法★★ 1.3逻辑函数的化简★★★ 1.4常用74HC系列门电路芯片★ 第二章 组合 ...