ES关键字

一、管理性命令

1、查看集群的节点

GET /_cat/nodes?v

2、查看集群健康状况

GET /_cat/health

3、查看集群中的库(index)

GET /_cat/indices?v

二、库(index)操作

1、查看某个具体库

GET /_cat/indices/xxx

2、手动建库

PUT /库名
{
  "mappings": {
    "表名":{
      "properties":{
        "属性名1":{
          "type":"keyword"
        },
        "属性名2":{
          "type":"text"
        },
        "属性名3":{
          "type":"integer"
        },
        "属性名4":{
          "type":"date"
        }
      }
    }
  }
}

2、自动建库

# 如果使用put则必须添加id号,使用post不添加id号会自动创建一个id号
POST /库名/表名/id号
{
  "属性名": 属性值,
  "属性名": 属性值,
  "属性值名": [
    属性值,
    属性值
  ]
}

3、删库

DELETE /库名

4、判断库是否存在

#404不存在 200存在

HEAD /库名

三、表(type)操作

1、查看库下表的信息

# (6.0版本,默认只有1个,7.0版本没有type)
# 查看某个库中所有type信息,包含mapping和setting
GET /库名

#查看某个index的某个type
GET /test2/_mapping/table2

#只查看mapping信息
GET /库名/_mapping

#只查看setting信息
GET /库名/_setting

2、建表操作同建库

四、数据基本操作

1、增

#自动生成id,无规则的
POST /db1/persons
{
  "name":"jack",
  "age": 30,
  "gender":"male"
  
}

#手动指定ID
POST /db1/persons/1
{
  "name":"jack1",
  "age": 30,
  "gender":"male"
  
}

2、改

#如果id已经存在,就更新,不存在就新增
#默认全量更新
POST /db1/persons/1
{
  "name":"jack1",
  "age": 40
}

#增量更新
POST /db1/persons/1/_update
{
	 "age": 41
}

#只有post可以增量更新

3、查询

# 全表查询
GET /库名/表名/_search

4、判断id是否存在

HEAD /库名/表名/id

5、删除某个id

DELETE /db1/persons/2

五、分词、子属性

注意:格式是keyword不能切词

# 默认英文按照空格切分
GET _analyze
{
  "text":"I am a programmer"
}

#默认切词器切中文,按字切
GET _analyze
{
  "text":"我是程序员"
}

#指定分词器,使用IK分词器的ik_smart切词法,
#特点:切的词的总字数=文本总字数
GET _analyze
{
  "analyzer": "ik_smart",
  "text":"我是程序员"
}

#使用IK分词器的ik_max_word切词法,
特点:切的词的总字数>=文本总字数
GET _analyze
{
  "analyzer": "ik_max_word",
  "text":"我是程序员"
}

子属性

text类型的字段,如果将来需要聚合,一定需要为其设置一个子属性,子属性的类型必须是keyword类型!
例如:
	name是text属性
	name.keyword是keyword类型
	
 "name" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword",
                "ignore_above" : 256
              }
            }
          }

六、批量导入

#导入数据:
#_bulk代表批量写
#格式 :  {"action": {metadata}}\n {data}
# action: insert:插入,update:更新,delete:删除, index(upsert): 存在就更新,不存在就插入
#metadata 指定当前向哪个index,哪个type,哪个id进行写,
#   格式:"_index":库名,"_type":表名,"_id":id号

#所有的数据插入同一个库,表中
POST /test/emps/_bulk
{"index":{"_id":"1"}}
{"empid":1001,"age":20,"balance":2000,"name":"李三","gender":"男","hobby":"吃饭睡觉"}
{"index":{"_id":"2"}}

或者:
#可以指定每个数据插入的不同的库,表中
POST /_bulk
{"index":{"_index":"test2","_type":"t2","_id":"1"}}
{"name":"lisi","age":30}
{"index":{"_index":"test","_type":"t1","_id":"1"}}
{"name":"wangwu","age":22}

七、查询

两种方式:


1、url的方式
GET /test/emps/_search?q=*

2、DSL语法
GET /test/emps/_search
{
  "query": {
    "match_all": {}
  }
}

常用参数

关键字含义在SQL中
query查询select
bool多个组合条件selext xxx from xxx where age=20 and gender=male
filter一个过滤条件where
term精确匹配=
match全文检索,会分词 (分词器ik)
must在过滤条件中使用,代表必须包含
fuzzy模糊音匹配dick 联想到 nick pick
from从哪一条开始取
size取多少条limit
_source只选择某些字段select 字段
match_phrase短语匹配,将输入的查询内容整个作为整体进行查询,不切词
multi_match一次匹配多个字段
must_not不包含
should最好也符合… 不符合也行,符合更好。一旦满足should会加分
range匹配某个范围between and
filter过滤,留下指定的where

八、聚合

语法:
GET /库名/表名/_search
{

 【"query": {
    ...
  }, 】
  "aggs": {
    "聚合后的名称": {
      "terms": {
        "field": "聚合的字段:必须要是keyword类型",
        "size": 10
      }
    }
	#子聚合
	【,"aggs": {
    "聚合后的名称": {
      "terms": {
        "field": "聚合的字段:必须要是keyword类型",
        "size": 10
      }
    }
  }】
  }
  
  【#跟聚合
  ,"aggs": {
    "聚合后的名称": {
      "terms": {
        "field": "聚合的字段:必须要是keyword类型",
        "size": 10
      }
    }
  }】
}

九、索引

建库时添加索引

PUT 库名
{  
  "aliases": {
    "别名": {},
	"别名": {}
  }, 
  "mappings": {
    "表名":{
      "properties": {
        "属性名":{
          "type": "long"
        },
        "属性名":{
          "type": "text",
          "analyzer": "ik_smart"
        }
      }
    }
  }
}

2、建库后添加索引

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "库名",
        "alias": "别名"
      }
    }
  ]
}

3、查询添加的别名

#查询别名: 查看ES中已经起了哪些别名
GET /_cat/aliases?v

4、给查询结果起别名(只能使用精确匹配term),建立一个子集视图

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "库名",
        "alias": "别名",
        "filter": {
         "terms": {
           "属性名": [
             "属性值1",
             "属性值2"
           ]
         }
        }
      }
    }
  ]
}

十、模板

PUT _template/库名
{
  "index_patterns": ["xxx*"],  #创建xxx开头的库会自动匹配该模板
  "aliases" : { 
    "{index}-xxx": {}, #别名1,将index作为变量,假设index名为class,则别名为calss-xxx
    "xxx-query":{} #别名2
  },
  "mappings": { 
"表名": {
      "properties": {
        "属性名": {
          "type": "keyword"
        },
        "属性值": {
          "type": "text",
          "analyzer": "ik_smart"
        }
      }
    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值