ES作为一个索引及搜索服务,对外提供丰富的REST接口,快速入门部分的实例使用[kibana]来测试,目的是对ES的使用方法及流程有个初步的认识。
基本概念
- Index(索引)
- 动词:保存(插入)
- 名词:类似MySQL数据库,给数据
- Type(类型)
- 已废弃,以前类似MySQL的表
- 现在用索引对数据分类
- Document(文档)
- 真正要保存的一个JSON数据
- 对应MySql中表的一行数据
- Field(字段)
- 列字段信息
- 对应MySql中表的字段信息
索引管理
创建索引
索引库。包含若干相似结构的 Document 数据,相当于数据库的database。
语法:PUT /index_name
put /product
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
响应
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "product"
}
number_of_shards - 表示一个索引库将拆分成多片分别存储不同的结点,提高了ES的处理能力 number_of_replicas - 是为每个 primary shard分配的replica shard数,提高了ES的可用性,如果只有一台机器,设置为0
修改索引
注意:索引一旦创建,primary shard 数量不可变化,可以改变replica shard 数量
语法:PUT /index_name/_settings
PUT /product/_settings
{
"number_of_replicas" : 2
}
响应
{
"acknowledged" : true
}
ES 中对 shard 的分布是有要求的,有其内置的特殊算法:
Replica shard 会保证不和他的那个 primary shard 分配在同一个节点上;如过只有一个节点,则此案例执行后索引的状态一定是yellow
http://127.0.0.1:9200/_cluster/health?pretty
查询索引 语法:`GET /index_name
GET /product
响应
{
"product" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "2",
"provided_name" : "product",
"creation_date" : "1693453817186",
"number_of_replicas" : "0",
"uuid" : "3wNi2hgoTEOcl6a9oTCfHw",
"version" : {
"created" : "7150299"
}
}
}
}
}
关闭索引
在有些场景下,某个索引暂时不使用,但是后期可能又会使用,这里的使用是指数据写入和数据搜索。这个索引在某一段时间内属于冷数据或者归档数据,这时可以使用索引的关闭功能。索引关闭时,只能通过ES的API或者监控工具看到索引的元数据信息,但是此时该索引不能写入和搜索数据,待该索引被打开后,才能写入和搜索数据。 先把索引person关闭,请求形式如下:
POST /product/_close
打开索引
索引关闭后,需要开启读写服务时可以将其设置为打开状态
POST /product/_open
删除索引
DELETE /product
响应
{
"acknowledged" : true
}
mapping管理
映射,创建映射就是向索引库中创建field(类型、是否索引、是否存储等特性)的过程
创建mapping
语法:POST /index_name/_mapping
POST /product/_mapping
{
"properties": {
"name": {
"type": "text"
},
"description": {
"type": "text"
},
"skuCode": {
"type": "keyword"
}
}
}
响应
{
"acknowledged" : true
}
查询mapping
语法:GET /index_name/_mapping
GET /product/_mapping
响应
{
"product" : {
"mappings" : {
"properties" : {
"description" : {
"type" : "text"
},
"name" : {
"type" : "text"
},
"skuCode" : {
"type" : "keyword"
}
}
}
}
}
更新mapping
映射创建成功可以添加新字段,已有字段不允许更新。
PUT /product/_mapping
{
"properties": {
"spuCode": {
"type": "keyword"
}
}
}
响应
{
"acknowledged" : true
}
删除mapping
通过删除索引来删除映射。
Document管理
创建document
此操作为 ES 手动生成 id 的新增 Document 方式。重复执行会进行update
语法:POST /index_name/_doc/id
POST /product/_doc/1
{
"name":"笔记本电脑",
"description":"人生苦短,我用Python",
"skuCode":"201002"
}
或
PUT /product/_doc/1
{
"name":"笔记本电脑",
"description":"人生苦短,我用Python",
"skuCode":"201002"
}
响应
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 7,
"result" : "updated",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 6,
"_primary_term" : 1
}
以下为自动生成id的方式
POST /product/_doc
{
"name":".net从入门到放弃",
"description":".net程序员谁都不服",
"skuCode":"201003"
}
响应
{
"_index" : "product",
"_type" : "_doc",
"_id" : "f3qmSooBIX5MsNarprwK",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
POST-增量字段更新
POST /product/_doc/1/_update
{
"doc":{
"name":"笔记本电脑222"
}
}
响应
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 23,
"result" : "updated",
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 23,
"_primary_term" : 1
}
查询文档
# 查看所有
GET /product/_search
# 根据ID查询
GET /product/_doc/1
响应
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_version" : 23,
"_seq_no" : 23,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "笔记本电脑222",
"description" : "人生苦短,我用Python111",
"skuCode" : "201002"
}
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "product",
"_type" : "_doc",
"_id" : "fnqmSooBIX5MsNaroryI",
"_score" : 1.0,
"_source" : {
"name" : ".net从入门到放弃",
"description" : ".net程序员谁都不服",
"skuCode" : "201003"
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "笔记本电脑222",
"description" : "人生苦短,我用Python111",
"skuCode" : "201002"
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "fHqjSooBIX5MsNarDbwt",
"_score" : 1.0,
"_source" : {
"name" : ".net从入门到放弃",
"description" : ".net程序员谁都不服",
"skuCode" : "201003"
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "fXqjSooBIX5MsNarwrwx",
"_score" : 1.0,
"_source" : {
"name" : ".net从入门到放弃",
"description" : ".net程序员谁都不服",
"skuCode" : "201003"
}
},
{
"_index" : "product",
"_type" : "_doc",
"_id" : "f3qmSooBIX5MsNarprwK",
"_score" : 1.0,
"_source" : {
"name" : ".net从入门到放弃",
"description" : ".net程序员谁都不服",
"skuCode" : "201003"
}
}
]
}
}
高级检索
# DSL 查询条件以Json
POST /product/_search
{
"query": {
"match_all": {}
}
}
导入数据
POST /product/_doc/1001
{
"name":".笔记本",
"description":"1111",
"skuCode":"20231001"
}
POST /product/_doc/1002
{
"name":"手机",
"description":"22222",
"skuCode":"20231002"
}
POST /product/_doc/1003
{
"name":"电脑",
"description":"33333",
"skuCode":"20231003"
}
POST /product/_doc/1004
{
"name":"学习机",
"description":"4444",
"skuCode":"20231004"
}
POST /product/_doc/1005
{
"name":"mp3",
"description":"55555",
"skuCode":"20231005"
}
match-全文索引精确查询
POST /product/_search
{
"query": {
"match": {
"skuCode": "20231005"
}
}
}
multi_match-多个字段查询
POST /product/_search
{
"query":{
"multi_match": {
"query": "20231005",
"fields": ["skuCode","name"]
}
}
}
term-精确查询
POST /product/_search
{
"query": {
"term": {
"name": {
"value": "mp3"
}
}
}
}
terms-查询
# 查询skuCode是20231005,20231001的
POST /product/_search
{
"query": {
"terms": {
"skuCode": [
"20231005",
"20231001"
]
}
}
}
查询部分字段
POST /product/_search
{
"query": {
"match": {
"name": "mp3"
}
},
"_source": ["name","skuCode"]
}
区间查询
POST /product/_search
{
"query": {
"range": {
"skuCode": {
"gte": 20231001,
"lte": 20231005
}
}
}
}
复合查询
POST /product/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"skuCode": {
"gte": 20231001,
"lte": 20231005
}
}
},{
"match": {
"name": "mp3"
}
}
]
}
}
}
过滤查询
POST /product/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"skuCode": {
"gte": 20231001,
"lte": 20231005
}
}
},{
"match": {
"name": "mp3"
}
}
]
}
}
}
判断文档是否存在
#判断文档是否存在
HEAD /product/_doc/1005
exists查询
#找到有对应属性的文档
POST /product/_search
{
"query": {
"exists": {
"field": "name"
}
}
}
ID批量查询
#批量查询ID
POST /product/_doc/_mget
{
"ids":["1001","1002"]
}
排序
POST /product/_search
{
"query": {
"match_all": {
}
},
"sort": [
{
"skuCode": {
"order": "asc"
}
}
]
}
高亮
#高亮
GET product/_search
{
"query": {
"match": {
"skuCode": "20231005"
}
},
"highlight": {
"fields": {
"skuCode": {
"pre_tags": "<span style='color:res'>",
"post_tags": "</span>"
}
}
}
}
_source-部分字段
POST /product/_search
{
"query": {
"match": {
"name": "mp3"
}
},
"_source": ["name"]
}
分页
#分页
POST /product/_search
{
"query": {
"match_all": {
}
},
"from": 0,
"size": 2
}