ES说明

使用Kibana对Elasticsearch的基本操作

  • 节点 (node):一个节点是一个Elasticsearch的实例
  • 集群(cluster):多个协同工作的Elasticsearch节点的集合被称为集群
  • 分片 (shard):将索引分为多个碎片,存储在多个节点上,提高性能
  • 副本(replica):副本分片用于搜索,或者是在原有的主分片丢失后成为新的主分片
  • 文档 (document):Elasticsearch是面向文档的,这意味着索引和搜索数据的最小单位是文档
  • 索引 (index):索引是映射类型的容器(数据库表)
  • 映射(mapping):存储分析链所需的所有信息(数据库字段)

索引库操作

1.创建索引库

  1. PUT /demo

2.查看索引库

  1. GET /demo

3.删除索引库

  1. DELETE /demo

类型操作

1.添加映射

  • type:类型,可以是text、keyword、long、short、date、integer、object等
  • index:是否索引(是否能被搜索),默认为true
  • store:是否存储,默认为false
  • analyzer:分词器,这里的 ik_max_word 即使用ik分词器
  • boost:网站权重,模糊搜索的优先级
  1. PUT /demo/_mapping
  2. {
  3. "properties":{
  4. "goods":{
  5. "properties":{
  6. "title":{
  7. "type":"text",
  8. "analyzer":"ik_max_word"
  9. },
  10. "images":{
  11. "type":"keyword",
  12. "index":"false"
  13. },
  14. "price":{
  15. "type":"float"
  16. }
  17. }
  18. }
  19. }
  20. }

2.查看映射

  1. GET /demo/_mapping/field/goods.*

3.创建索引库和类型

  1. PUT /demo2
  2. {
  3. "settings":{},
  4. "mappings":{
  5. "properties":{
  6. "goods":{
  7. "properties":{
  8. "title":{
  9. "type":"text",
  10. "analyzer":"ik_max_word"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }

文档操作

1.新增文档

  • 新增文档并随机生成id
  1. POST /demo/_doc/
  2. {
  3. "goods": {
  4. "title":"小米手机",
  5. "images":"image.jpg",
  6. "price":2699
  7. }
  8. }

2.查看文档

  • 后面的一串是ES生成的随机id
  1. GET /demo/_doc/4pQPYHgBwYkVQ8-p4d_T

3.新增文档设置ID

  1. POST /demo/_doc/2
  2. {
  3. "goods": {
  4. "title":"红米手机",
  5. "images":"image.jpg",
  6. "price":1699
  7. }
  8. }

4.修改文档

  1. PUT /demo/_doc/2
  2. {
  3. "goods": {
  4. "title":"华为手机",
  5. "images":"image.jpg",
  6. "price":4699
  7. }
  8. }

5.删除文档

  1. DELETE /demo/_doc/2

查询操作

1.基本查询

  • 查询类型:match_allmatchtermrange等等
  1. GET /索引库名/_search
  2. {
  3. "query": {
  4. "查询类型": {
  5. "查询条件":"查询条件值"
  6. }
  7. }
  8. }

2.查询所有query

  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

3.匹配查询match

  • 默认查询出(小米or手机)这两个词语的出现的数据
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": "小米手机"
  6. }
  7. }
  8. }
  • 设置查询关联查询(小米and手机)
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": {"query":"小米手机","operator":"and"}
  6. }
  7. }
  8. }

4.词条匹配term

  • 精确查询出结果
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "term": {
  5. "goods.price": "2699"
  6. }
  7. }
  8. }

5.布尔组合bool

  • bool把各种其它查询通过must (与)、must_not (非)、should (或)的方式进行组合
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [ {"match": { "goods.title": "小米" } } ],
  6. "must_not": [ {"match": { "goods.title": "华为" } } ],
  7. "should": [ {"match": { "goods.title": "手机" } } ]
  8. }
  9. }
  10. }

6.范围查询range

  • range查询参数有gt(大于)、gte(大于等于)、lt(小于)、lte(小于等于)
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "range": {
  5. "goods.price": {
  6. "gte": 2000,
  7. "lte": 3000
  8. }
  9. }
  10. }
  11. }

7.模糊查询fuzzy

  • fuzzy 查询是 term 查询的模糊等价
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "goods.title": "华为"
  6. }
  7. }
  8. }

结果处理

1.指定显示字段

  1. GET /demo/_search
  2. {
  3. "_source": ["goods.title","goods.price"],
  4. "query": {
  5. "term": {
  6. "goods.price": "2699"
  7. }
  8. }
  9. }

2.排除显示字段

  • includes:来指定想要显示的字段
  • excludes:来指定不想要显示的字段
  1. GET /demo/_search
  2. {
  3. "_source": {
  4. "excludes": ["goods.images"]
  5. },
  6. "query": {
  7. "term": {
  8. "goods.price": "2699"
  9. }
  10. }
  11. }

3.过滤数据

  • 条件查询中进行过滤
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [ {"match": { "goods.title": "小米" } } ],
  6. "filter": [
  7. { "range":{ "goods.price":{ "gt":3000.00, "lt":9000.00 } } }
  8. ]
  9. }
  10. }
  11. }
  • 无查询条件,直接过滤
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "range":{ "goods.price":{ "gt":3000.00, "lt":9000.00 } }
  7. }
  8. }
  9. }
  10. }

4.字段排序

  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": "手机"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "goods.price": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

5.多字段排序

  • 再根据得分进行排序
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": "手机"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "goods.price": { "order": "desc" },
  11. "_score": { "order": "desc" }
  12. }
  13. ]
  14. }

6.分页

  • from:第几页
  • size:多少条
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": "手机"
  6. }
  7. },
  8. "from": 0,
  9. "size": 2
  10. }

7.高量显示

  • pre_tags:前置标签
  • post_tags:后置标签
  • fields:需要高亮的字段
  1. GET /demo/_search
  2. {
  3. "query": {
  4. "match": {
  5. "goods.title": "手机"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": "<em>",
  10. "post_tags": "</em>",
  11. "fields": {
  12. "goods.title": {}
  13. }
  14. }
  15. }

聚合操作

1.相关说明

  • 桶(bucket):桶的作用,是按照某种方式对数据进行分组(group by)
  • 度量(metrics):分组完成以后,我们一般会对组中的数据进行聚合运算

2.聚合桶

  • aggs:声明这是一个聚合查询
  • terms:聚合的类型,根据词条内容划分
  1. GET /demo/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "类型": {
  6. "terms": {
  7. "field": "goods.type"
  8. }
  9. }
  10. }
  11. }

这里增加了goods.type字段

3.桶度量

  • avg:度量的类型
  1. GET /demo/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "类型": {
  6. "terms": {
  7. "field": "goods.type"
  8. },
  9. "aggs": {
  10. "平均价": {
  11. "avg": {
  12. "field": "goods.price"
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

更多API - https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html

更新时间:{docsify-updated}