ES说明
使用Kibana对Elasticsearch的基本操作
- 节点 (node):一个节点是一个Elasticsearch的实例
- 集群(cluster):多个协同工作的Elasticsearch节点的集合被称为集群
- 分片 (shard):将索引分为多个碎片,存储在多个节点上,提高性能
- 副本(replica):副本分片用于搜索,或者是在原有的主分片丢失后成为新的主分片
- 文档 (document):Elasticsearch是面向文档的,这意味着索引和搜索数据的最小单位是文档
- 索引 (index):索引是映射类型的容器(数据库表)
- 映射(mapping):存储分析链所需的所有信息(数据库字段)
索引库操作
1.创建索引库
PUT /demo
2.查看索引库
GET /demo
3.删除索引库
DELETE /demo
类型操作
1.添加映射
type:类型,可以是text、keyword、long、short、date、integer、object等index:是否索引(是否能被搜索),默认为truestore:是否存储,默认为falseanalyzer:分词器,这里的 ik_max_word 即使用ik分词器boost:网站权重,模糊搜索的优先级
PUT /demo/_mapping{"properties":{"goods":{"properties":{"title":{"type":"text","analyzer":"ik_max_word"},"images":{"type":"keyword","index":"false"},"price":{"type":"float"}}}}}
2.查看映射
GET /demo/_mapping/field/goods.*
3.创建索引库和类型
PUT /demo2{"settings":{},"mappings":{"properties":{"goods":{"properties":{"title":{"type":"text","analyzer":"ik_max_word"}}}}}}
文档操作
1.新增文档
- 新增文档并随机生成id
POST /demo/_doc/{"goods": {"title":"小米手机","images":"image.jpg","price":2699}}
2.查看文档
- 后面的一串是ES生成的随机id
GET /demo/_doc/4pQPYHgBwYkVQ8-p4d_T
3.新增文档设置ID
POST /demo/_doc/2{"goods": {"title":"红米手机","images":"image.jpg","price":1699}}
4.修改文档
PUT /demo/_doc/2{"goods": {"title":"华为手机","images":"image.jpg","price":4699}}
5.删除文档
DELETE /demo/_doc/2
查询操作
1.基本查询
- 查询类型:
match_all、match、term、range等等
GET /索引库名/_search{"query": {"查询类型": {"查询条件":"查询条件值"}}}
2.查询所有query
GET /demo/_search{"query": {"match_all": {}}}
3.匹配查询match
- 默认查询出(小米or手机)这两个词语的出现的数据
GET /demo/_search{"query": {"match": {"goods.title": "小米手机"}}}
- 设置查询关联查询(小米and手机)
GET /demo/_search{"query": {"match": {"goods.title": {"query":"小米手机","operator":"and"}}}}
4.词条匹配term
- 精确查询出结果
GET /demo/_search{"query": {"term": {"goods.price": "2699"}}}
5.布尔组合bool
- bool把各种其它查询通过must (与)、must_not (非)、should (或)的方式进行组合
GET /demo/_search{"query": {"bool": {"must": [ {"match": { "goods.title": "小米" } } ],"must_not": [ {"match": { "goods.title": "华为" } } ],"should": [ {"match": { "goods.title": "手机" } } ]}}}
6.范围查询range
- range查询参数有gt(大于)、gte(大于等于)、lt(小于)、lte(小于等于)
GET /demo/_search{"query": {"range": {"goods.price": {"gte": 2000,"lte": 3000}}}}
7.模糊查询fuzzy
- fuzzy 查询是 term 查询的模糊等价
GET /demo/_search{"query": {"fuzzy": {"goods.title": "华为"}}}
结果处理
1.指定显示字段
GET /demo/_search{"_source": ["goods.title","goods.price"],"query": {"term": {"goods.price": "2699"}}}
2.排除显示字段
includes:来指定想要显示的字段excludes:来指定不想要显示的字段
GET /demo/_search{"_source": {"excludes": ["goods.images"]},"query": {"term": {"goods.price": "2699"}}}
3.过滤数据
- 条件查询中进行过滤
GET /demo/_search{"query": {"bool": {"must": [ {"match": { "goods.title": "小米" } } ],"filter": [{ "range":{ "goods.price":{ "gt":3000.00, "lt":9000.00 } } }]}}}
- 无查询条件,直接过滤
GET /demo/_search{"query": {"constant_score": {"filter": {"range":{ "goods.price":{ "gt":3000.00, "lt":9000.00 } }}}}}
4.字段排序
GET /demo/_search{"query": {"match": {"goods.title": "手机"}},"sort": [{"goods.price": {"order": "desc"}}]}
5.多字段排序
- 再根据得分进行排序
GET /demo/_search{"query": {"match": {"goods.title": "手机"}},"sort": [{"goods.price": { "order": "desc" },"_score": { "order": "desc" }}]}
6.分页
from:第几页size:多少条
GET /demo/_search{"query": {"match": {"goods.title": "手机"}},"from": 0,"size": 2}
7.高量显示
pre_tags:前置标签post_tags:后置标签fields:需要高亮的字段
GET /demo/_search{"query": {"match": {"goods.title": "手机"}},"highlight": {"pre_tags": "<em>","post_tags": "</em>","fields": {"goods.title": {}}}}
聚合操作
1.相关说明
- 桶(bucket):桶的作用,是按照某种方式对数据进行分组(group by)
- 度量(metrics):分组完成以后,我们一般会对组中的数据进行聚合运算
2.聚合桶
aggs:声明这是一个聚合查询terms:聚合的类型,根据词条内容划分
GET /demo/_search{"size": 0,"aggs": {"类型": {"terms": {"field": "goods.type"}}}}
这里增加了goods.type字段
3.桶度量
avg:度量的类型
GET /demo/_search{"size": 0,"aggs": {"类型": {"terms": {"field": "goods.type"},"aggs": {"平均价": {"avg": {"field": "goods.price"}}}}}}
更多API - https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html
更新时间:{docsify-updated}
