#创建索引
#PUT 索引名称
PUT test_index
#PUT 索引
#增加配置:JSON格式的主体内容
PUT test_index_1
{
"aliases": {
"test1": {}
}
}
#删除索引
DELETE test_index_1
# 修改索引配置
# ES 软件不允许修改索引信息
PUT test_index_1
{
"aliases": {
"test1": {}
}
}
# HEAD 索引 HTTP状态那:200 404
HEAD test_index
#查询索引
#GET 索引名称
GET test_index
GET test_index_1
GET test1
#查询所有索引
GET _cat/indices
#创建文档(索引数据)
PUT test_doc
#PUT 增加数据 路径后面需要增加唯一标识,且标识是自己自定义的
PUT test_doc/_doc/1001
{
"id":1001,
"name":"张胜男",
"age":30
}
#POST 增加数据路径后面不需要增加唯一ID,ID是系统自动生成
POST test_doc/_doc
{
"id":1001,
"name":"张胜男",
"age":30
}
#查询文档
#查询文档中所有的数据
GET test_doc/_search
GET test_doc/_doc/1001
#修改数据
PUT test_doc/_doc/1001
{
"id" : 10011,
"name" : "张胜男",
"age" : 30,
"tel":123
}
POST test_doc/_doc/1001
{
"id":1001,
"name":"张胜男",
"age":300
}
#删除数据
DELETE test_doc/_doc/ZvksjowB6-gES7OmSZxt
PUT test_query
# 批量操作用_bulk
PUT test_query/_bulk
{"index": {"_index": "test_query","_id": "1001"}}
{"id": "1001","name":"zhang san","age":30}
{"index": {"_index": "test_query","_id": "1002"}}
{"id": "1002","name":"li si","age": 40}
{"index": {"_index": "test_query","_id": "1003"}}
{"id": "1003", "name": "wang wu","age" : 50}
{"index": {"_index": "test_query","_id": "1004"}}
{"id": "1004","name": "zhangsan", "age" : 30}
{"index": {"_index": "test_query","_id": "1005"}}
{"id": "1005","name": "lisi","age":40}
{"index": {"_index": "test_query","_id": "1006"}}
{"id": "1006", "name ": "wangwu","age" : 50}
GET test_query/_search
#基础查询语法
#match 是分词查询,ES会奖数据分词(关键词)保存
# 是分词的模糊匹配
GET test_query/_search
{
"query": {
"match": {
"name": "zhang li"
}
}
}
#_source对查询结果的字段进行限制
GET test_query/_search
{
"_source": ["name","age"],
"query": {
"match": {
"name": "zhang li"
}
}
}
#精确匹配
GET test_query/_search
{
"query": {
"term": {
"name": {
"value": "zhang san"
}
}
}
}
#组合多个条件 or
GET test_query/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "zhang"
}
},
{
"match": {
"age": 40
}
}
]
}
}
}
#排序后查询
GET test_query/_search
{
"query": {
"match": {
"name": "zhang li"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
#分页查询
GET test_query/_search
{
"query": {
"match_all": {}
},
"from": 2,
"size": 4
}
#分组查询
# ageGroup1 聚合名称顺便取值
GET test_query/_search
{
"aggs": {
"ageGroup1": {
"terms": {
"field": "age"
}
}
}
}
#分组后聚合(求和)
# 在语法关键字同级别下,后面继续语法操作
GET test_query/_search
{
"aggs": {
"ageGroup": {
"terms": {
"field": "age"
},
"aggs": {
"ageSum": {
"sum": {
"field": "age"
}
}
}
}
}
}
#求年龄平均值
GET test_query/_search
{
"aggs": {
"avgAge": {
"avg": {
"field": "age"
}
}
}
}
#获取前几名操作
GET test_query/_search
{
"aggs": {
"top3": {
"top_hits": {
"sort": [
{
"age": {
"order": "desc"
}
}
],
"size": 3
}
}
}
}
#索引模版
PUT test_temp
GET test_temp
GET test_temp_1
PUT test_temp_1
{
"settings": {
"number_of_shards":2
}
}
# index_patterns 匹配上自定义模版的规则 名称是my 开头
PUT _template/mytemplate
{
"index_patterns": [
"my*"
],
"settings": {
"index":{
"number_of_shards":"2"
}
},
"mappings": {
"properties": {
"now":{
"type": "date",
"format": "yyyy/MM/dd"
}
}
}
}
#查询模版
GET _template/mytemplate
PUT my_test_temp
GET my_test_temp
DELETE my_test_temp
#删除模版
DELETE _template/mytemplate
#分词器
GET _analyze
{
"analyzer": "ik_max_word",
"text": ["我是一个三好学生"]
}
#文档评分机制
PUT test_score
# 公式 boost * idf * tf
#tf 词频
# idf 逆文档频率
#explain=true
GET test_score/_search?explain=true
{
"query": {
"match": {
"text": "zhang"
}
}
}
PUT test_score/_doc/1001
{
"text":"zhang kai shou bi,ying jie tai yang"
}
PUT test_score/_doc/1002
{
"text":"zhang san"
}
PUT atguigu
PUT atguigu/_doc/1001
{
"text":"java"
}
PUT atguigu/_doc/1002
{
"text":"java bigData"
}
#tf freq / (freq + k1 * (1 - b + b * dl / avgdl))
#idf log(1 + (N - n + 0.5) / (n + 0.5))
GET atguigu/_search?explain=true
{
"query": {
"match": {
"text": "java"
}
}
}
PUT atguigu/_doc/1003
{
"text":"bigData",
"content":"java bigData"
}
GET atguigu/_search
{
"query": {
"match": {
"text": "java"
}
}
}
DELETE atguigu
PUT testscore
GET testscore/_search
PUT /testscore/_doc/1001
{
"title":"Hadoop is a Framework",
"content":"Hadoop 是一个大数据基础框架"
}
PUT /testscore/_doc/1002
{
"title":"Hive is a SQL Tools",
"content":"Hive 是一个SQL工具"
}
PUT /testscore/_doc/1003
{
"title":"Spark is a Framework",
"content":"Spark是一个分布式计算引擎"
}
#查询的权重
GET /testscore/_search?explain=true
{
"query": {
"bool": {
"should": [
{
"match": {
"title": {"query": "Hadoop","boost": 1}
}
},
{
"match": {
"title": {"query": "Hive","boost": 2}
}
},
{
"match": {
"title": {"query": "Spark","boost": 1}
}
}
]
}
}
}
ES 操作历史记录
于 2024-02-25 15:31:58 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)