一、安装
1.拉取镜像
docker pull kibana:7.4.2
2.运行容器
docker run -di --name kibana -e ELASTICSEARCH_URL=http://128.1.49.131:9200 -p 5601:5601 kibana:7.4.2
3.开9200、9300、5601端口
二、操作es(左侧导航栏dev tools)
1.操作索引
● 创建索引(包括创建映射)
PUT /viuman { "settings": { "number_of_shards": 1, "number_of_replicas": 0 }, "mappings": { "properties": { "images": { "type": "keyword", "index": false }, "price": { "type": "float" }, "saleable": { "type": "boolean" }, "stock": { "type": "long" }, "title": { "type": "text", "analyzer": "ik_max_word" } } } }
● 查看所有索引
GET *
● 查看索引信息
GET viuman
● 查看映射
GET viuman/_mapping
● 删除索引
DELETE viuman
● 导入数据
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" }
2.操作文档
● 添加文档(随机生成id)
POST /viuman/_doc/ { "title":"小米手机", "images":"http://image.leyou.com/12479122.jpg", "price":2699.00 }
● 添加/修改文档
PUT /viuman/_doc/3 { "title":"超米手机", "images":"http://image.leyou.com/12479122.jpg", "price":2899.00, "stock": 200, "saleable":true }
● 删除文档
DELETE /viuman/_doc/2
3.基本搜索
● 查所有文档
GET _search
● or搜索
GET /viuman/_search { "query":{ "match":{ "title":"小米电视" } } }
● and搜索
GET /viuman/_search { "query":{ "match": { "title": { "query": "小米电视", "operator": "and" } } } }
● 按匹配到总分词数量的百分比搜索
GET /viuman/_search { "query":{ "match":{ "title":{ "query":"小米曲面电视", "minimum_should_match": "75%" } } } }
● 多字段搜索
GET /viuman/_search { "query":{ "multi_match": { "query": "小米", "fields": [ "title", "subTitle" ] } } }
● 精确匹配
GET /viuman/_search { "query":{ "term":{ "price":2699.00 } } }
● 多词条精确匹配
GET /viuman/_search { "query":{ "terms":{ "price":[2699.00,2899.00,3899.00] } } }
● 只返回指定字段
GET /viuman/_search { "_source": ["title","price"], "query": { "term": { "price": 2699 } } }
● 不返回指定字段
GET /viuman/_search { "_source": { "excludes": ["images"] }, "query": { "term": { "price": 2699 } } }
4.高级搜索
● must
(与)、must_not
(非)、should
(或)布尔查询
GET /viuman/_search { "query":{ "bool":{ "must": { "match": { "title": "大米" }}, "must_not": { "match": { "title": "电视" }}, "should": { "match": { "title": "手机" }} } } }
● 范围查询(gt大于 gte大于等于 lt小于 lte小于等于)
GET /viuman/_search { "query":{ "range": { "price": { "gte": 1000.0, "lt": 2800.00 } } } }
● 模糊查询(默认最大编辑距离2)
GET /viuman/_search { "query": { "fuzzy": { "title": "appla" } } }
● 模糊查询(指定编辑距离)
GET /viuman/_search { "query": { "fuzzy": { "title": { "value":"appla", "fuzziness":1 } } } }
● 条件查询中进行过滤
GET /viuman/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手机" }}, "filter":{ "range":{"price":{"gt":2000.00,"lt":3800.00}} } } } }
● 无查询条件直接过滤
GET /viuman/_search { "query":{ "constant_score": { "filter": { "range":{"price":{"gt":2000.00,"lt":3000.00}} } } } }
● 单字段排序
GET /viuman/_search { "query": { "match": { "title": "小米手机" } }, "sort": [ { "price": { "order": "desc" } } ] }
● 多字段排序(首先按照价格排序,然后按照相关性得分排序)
GET /viuman/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手机" }}, "filter":{ "range":{"price":{"gt":2000,"lt":300000}} } } }, "sort": [ { "price": { "order": "desc" }}, { "_score": { "order": "desc" }} ] }
5.聚合
1)划分桶(查看各桶的数量)
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" } } } }
- size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
- aggs:声明这是一个聚合查询,是aggregations的缩写
- popular_colors:给这次聚合起一个名字,任意。
- terms:划分桶的方式,这里是根据词条划分
- field:划分桶的字段
- terms:划分桶的方式,这里是根据词条划分
- popular_colors:给这次聚合起一个名字,任意。
2)桶内度量
● 求平均值
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } } } } } }
- aggs:我们在上一个aggs(popular_colors)中添加新的aggs。可见
度量
也是一个聚合,度量是在桶内的聚合 - avg_price:聚合的名称
- avg:度量的类型,这里是求平均值
- field:度量运算的字段
● 桶内嵌套桶
GET /cars/_search { "size" : 0, "aggs" : { "popular_colors" : { "terms" : { "field" : "color" }, "aggs":{ "avg_price": { "avg": { "field": "price" } }, "maker":{ "terms":{ "field":"make" } } } } } }
- 原来的color桶和avg计算我们不变
- maker:在嵌套的aggs下新添一个桶,叫做maker
- terms:桶的划分类型依然是词条
- filed:这里根据make字段进行划分
新的聚合maker
被嵌套在原来每一个color
的桶中,每个颜色下面都根据 make
字段进行了分组
3)阶梯分桶Histogram
GET /cars/_search { "size":0, "aggs":{ "price":{ "histogram": { "field": "price", "interval": 5000, "min_doc_count": 1 } } } }
- interval:阶梯间隔
- min_doc_count:约束最少文档数量为1,过滤文档数量为0的桶
附:es常用类型
-
String类型,又分两种:
- text:可分词,不可参与聚合
- keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
-
Numerical:数值类型,分两类
- 基本数据类型:long、interger、short、byte、double、float、half_float
- 浮点数的高精度类型:scaled_float。需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。