elasticsearch基本操作

本文记录一些 es 的基础查询。

目录

_cat系列

常用字段类型

操作索引 Index

操作文档 Document

analize分析

基本查询

bool查询(多条件查询)

聚合查询


_cat系列

所有命令后面的 "?v" 可不加,加了的话可以显示列名等信息。

# 检查集群健康状态
GET /_cat/health?v
# 查看节点信息
GET /_cat/nodes?v
# 查看索引信息
GET /_cat/indices?v
# 显示为索引配置的别名,包含过滤器、路由信息等
GET /_cat/aliases?v
# 显示每个节点分片的数量以及硬盘空间使用的情况
GET /_cat/allocation?v
# 显示master的IP和所在节点
GET /_cat/master?v
# 显示节点信息
GET /_cat/nodes?v
# 详细的描述了节点包含的分片信息
GET /_cat/shards?v

常用字段类型

在创建索引、文档前,有必要先了解下es中常用的字段类型

类型名称包含种类说明
字符串text:用于全文索引,搜索时会自动使用分词器进行分词再匹配
keyword:不分词,搜索时需要匹配完整的值
数值整型:byte、short、integer、long
浮点型:float、double
日期date

额外参数 format:对日期进行格式化。

例如:"yyyy-MM-dd HH:mm:ss"

范围integer_range、long_range、float_range、double_range、date_range在插入/更新文档时,字段需要使用 json 形式
布尔        true、false
其他如对象、数组、ip等

操作索引 Index

# 【结构化】创建索引book,设定mappings、settings。
# 并给两个text类型的字段,设置ik分词器
PUT /book
{
	"settings":{
		"number_of_shards": 1,
		"number_of_replicas": 1
	},
	"mappings":{
		"properties":{
		  "author":{
				 "type": "keyword"
			},
			"title":{
				"type": "text",
				"analyzer": "ik_max_word", "search_analyzer": "ik_smart"
			},
			"description":{
				"type": "text",
				"analyzer": "ik_max_word", "search_analyzer": "ik_smart"
			},
			"price":{
			  "type": "double"
			},
			"words":{
			  "type": "integer"
			},
			"publish_date":{
				"type": "date",
				"format": "yyyy-MM-dd"
			},
			"is_deleted":{
			  "type": "boolean"
			}
		}
	}
}

# 删除索引
DELETE /book

# 查询索引信息,返回结果包含aliases、mappings、settings
GET /book
# 查看索引的mapping信息
GET /book/_mapping
# 查看索引的setting信息,里面包含了索引的分区数、副本数、创建时间等信息
GET /book/_settings
# 查看索引统计信息,包含文档数、占用存储空间等
GET /book/_stats

操作文档 Document

文档的增删改操作

操作文档的时候路径中需要增加 “_doc”!

# 一、创建文档。指定ID,可重复创建,会覆盖
PUT /book/_doc/5/
{
  "author":"丸山黄金",
  "title":"overlord 14卷",
  "description":"灭国的魔女",
  "price":"57",
  "word":1210000,
  "publish_date":"2019-08-20",
  "is_deleted":false
}

# 不可重复创建,当ID存在时会报错
POST /book/_doc/3/_create
{
  "author":"xjy",
  "title":"spring ioc源码",
  "price":"20",
  "word":42000
}

# 自动生成ID
POST /book/_doc
{
  "title":"Redis深度解析"
}

# 修改文档,可以使用POST或PUT
# PUT全量更新,写法跟创建一样。ID存在就是更新
PUT /book/_doc/1
{
	"author":"xujingyi",
	"price":20.5
}

# POST增量更新,必须要使用 POST 和 "_update"
POST /book/_doc/1/_update
{
  "doc":{
    "title":"Reids",
    "price":34.5
  }
}

# 按条件删除
POST /prometheusbeat-7.3.1/_delete_by_query
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2021-09-15 08:41:00",
        "lte": "2021-09-15 08:42:00",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

analize分析

# 分析字段的分词情况等
GET /book/_analyze
{
 "field": "title",
 "text": "Springboot源码解析"
}

基本查询

# 查询文档总数
GET /book/_count

# 一、主键查询
GET /book/_doc/1

# 根据ID列表查询,也可以是/book/_doc/_mget
GET /book/_mget
{
  "ids":[1,2]
}

# 查询全部
GET /book/_search

GET /book/_search
{
  "query": {
    "match_all": {}
  }
}

# 单个条件模糊查询 match
GET /book/_search
{
  "query": {
    "match": {
      "title": "spring源码"
    }
  }
}

# 普通match查询
GET /book/_search
{
  "query": {
    "match": {
      "title": {
        "query": "spring源码"
      }
    }
  }
}

# 查询大于某个时间区间的数据
GET /prometheusbeat-7.3.1/_search
{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "2021-09-15 08:36:00",
        "lte": "now",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

# 按复杂对象(json)中的字段进行查询
GET /prometheusbeat-7.3.1/_search
{
  "query": {
    "match": {
      "labels.job": "kube-state-metrics"
    }
  }
}

# 必须要有 minimum_should_match 个关键词匹配,才能查出来
GET /book/_search
{
  "query": {
    "match": {
      "title": {
        "query": "mybatis源码",
        "minimum_should_match": 2
      }
    }
  }
}

# 单个条件模糊查询,并限制返回的字段 _source
GET /book/_search
{
  "query": {
    "match": {
      "author": "丸山黄金"
    }
  },
  "_source": "title"
}

# 精确查找 term
# 注意:term用于数字、日期、keyword等类型时没问题,但是对于text类型,只能匹配到分词
GET /book/_search
{
  "query": {
    "term": {
      "price": "55"
    }
  }
}

# 多个字段中只要有一个跟搜索的字段匹配 multi_match
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "骑士",
      "fields":["title","description"]
    }
  }
}

# 给multi_match设置字段权重
GET /book/_search
{
  "query": {
    "multi_match": {
      "query": "骑士",
      "fields":["title^2","description"], 
      "tie_breaker": 0.3
    }
  }
}

# 一个字段中必须包含指定的多个分词
GET /book/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "ioc AND 源码"
    }
  }
}

# 分页查询 from & size
GET /book/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

# 排序 sort
GET /book/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

bool查询(多条件查询)

# 多条件查询 AND 效果,使用 must
GET /book/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "spring"
          }
        },{
          "match": {
            "description": "aop"
          }
        }
      ]
    }
  }
}

# 多条件查询 OR 效果,使用 should
GET /book/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": "spring"
          }
        },{
          "match": {
            "description": "aop"
          }
        }
      ]
    }
  }
}

# 查询的结果必须【不包含】指定的条件
GET /book/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "title": "spring"
          }
        }
      ]
    }
  }
}

# 范围查询 filter。可以和must与should结合使用。
# 不过要注意如何和should一起使用,当should没匹配时,filter仍然会发挥作用!
# gte和lte可以分开使用,以达到查询大于某值或者小于某值的效果
GET /book/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "gte": 50,
              "lte": 60
            }
          }
        }
      ]
    }
  }
}

# constant_score也可以配合filter使用,
# filter正常不会返回sorce,跟constant_score结合使用时,可以返回boost指定的score
# 注意:这里跟bool方式写法上有个不同点是,这里filter只有一个,而bool方式是一个数组
GET /book/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "price": {
              "gte": 50,
              "lte": 60
            }
        }
      }, 
      "boost": 1.2
    }
  }
}

聚合查询

聚合查询类似于 mysql 中的 group by 以及一些函数,比如 sum、avg 等。

# 查询一个字段的最大值[max]。[min、avg、sum]也是同样用法
# 可以跟query结合使用,先过滤一部分数组,再做聚合!
GET /book/_search
{
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    }
  }
}

# 文档中某个值的个数 [value_count],一般和其他聚合结合使用
GET /book/_search
{
  "aggs": {
    "price_count":{
      "value_count": {
        "field": "price"
      }
    }
  }
}

# 计算文档中某个字段值的非重复个数。注意返回的是个数。[cardinality]
GET /book/_search
{
  "aggs": {
    "price_distinct":{
      "cardinality": {
        "field": "price"
      }
    }
  }
}

# 计算某个字段的统计数据,返回它的[count、min、max、avg、sum]数值。[stats]
GET /book/_search
{
  "aggs": {
    "price_stats":{
      "stats": {
        "field": "price"
      }
    }
  }
}

# 根据某个字段进行分组,会返回组内元素的个数。 [terms]
GET /book/_search
{
  "aggs": {
    "author_terms":{
      "terms": {
        "field": "author",
        "size": 10
      }
    }
  }
}

# 和query结合使用,过滤后再做聚合
GET /book/_search
{
  "query": {
    "match": {
      "description": "好汉"
    }
  }, 
  "aggs": {
    "author_terms":{
      "terms": {
        "field": "author",
        "size": 5
      }
    }
  }
}

# 过滤聚合(filter Aggregation)
# 通常这将用于将当前聚合上下文缩小到一组特定的文档
GET /book/_search
{
  "aggs": {
    "author_terms": {
      "filter": {
        "match": {
          "title": "spring"
        }
      }, 
      "aggs": {
        "price_terms": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

# 范围聚合(Range Aggregation) [range]
GET /book/_search
{
  "aggs": {
    "price_ranges":{
      "range": {
        "field": "price",
        "ranges": [
          { "to": 50 },
          { "from": 50, "to": 100 },
          { "from": 100 }
        ]
      }
    }
  }
}

# 时间范围聚合(Date Range Aggregation) [date_range]
GET /book/_search
{
  "aggs": {
    "publish_date_range":{
      "date_range": {
        "field": "publish_date",
        "ranges": [
          { "to": "2017-08-20"},
          { "from": "2017-08-20", "to": "2019-08-20"},
          { "from": "2019-08-20"}
        ]
      }
    }
  }
}

# 按某查询条件查询,然后group by,最后sum
GET /prometheusbeat-7.3.1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "container_cpu_usage_seconds_total"
          }
        },
        {
          "match": {
            "labels.container": "train-py-test"
          }
        }
      ]
    }
  }, 
  "aggs": {
    "group_by_time":{
      "terms": {
        "field": "@timestamp",
        "size": 100
      }
      ,"aggs": {
        "value_sum": {
          "sum": {
            "field": "value"
          }
        }
      }
    }
  }
}

# 聚合后按照原字段排序 [_term]
GET /prometheusbeat-7.3.1/_search
{
  "aggs": {
    "group_by_time":{
      "terms": {
        "field": "@timestamp",
        "order": {
          "_term": "asc"
        }, 
        "size": 100
      }
      ,"aggs": {
        "value_sum": {
          "sum": {
            "field": "value"
          }
        }
      }
    }
  }
}

# 聚合后按照计算出来的字段排序
GET /prometheusbeat-7.3.1/_search
{
  "aggs": {
    "group_by_time":{
      "terms": {
        "field": "@timestamp",
        "order": {
          "value_sum": "asc"
        }, 
        "size": 100
      }
      ,"aggs": {
        "value_sum": {
          "sum": {
            "field": "value"
          }
        }
      }
    }
  }
}

 先记到这吧,持续补充。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储、搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档。文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值