文章elasticsearch版本7.13.1
es和mysql的一些概念区分:
索引操作
创建索引
# 创建user索引库
PUT /user
{
"mappings": {
"properties": {
"info": {
"type": "text", //text代表需要分词
"analyzer": "ik_smart" //使用IK分词器
},
"email": {
"type": "keyword", //keyword不需要分词
"index": false //false代表不需要参与搜索,不写默认true
},
"name": {
"type": "object", //对象类型
"properties": {
"firstName": {
"type": "keyword"
},
"lastWord": {
"type": "keyword"
}
}
}
}
}
}
上面执行结果:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "user"
}
user对应的json数据样例是【json数据就是文档】:
{
"info": "我是程序员",
"email": "123@qq.com",
"name": {
"firstName": "三",
"lastName": "张"
}
}
查询索引
# 查询索引
GET /user
结果:
{
"user" : {
"aliases" : { },
"mappings" : {
"properties" : {
"email" : {
"type" : "keyword",
"index" : false
},
"info" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"name" : {
"properties" : {
"firstName" : {
"type" : "keyword"
},
"lastWord" : {
"type" : "keyword"
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "user",
"creation_date" : "1716640645630",
"number_of_replicas" : "1",
"uuid" : "xXpEEgf2TWuqGfXfPAH9gA",
"version" : {
"created" : "7130199"
}
}
}
}
}
修改索引
es索引创建之后就不能修改,只能新增字段
# 修改索引,添加字段
PUT /user/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}
执行结果:
{
"acknowledged" : true
}
通过GET /user 查询索引看看结果[一部分数据]:
{
"user" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"email" : {
"type" : "keyword",
"index" : false
}......
删除索引
#删除索引
DELETE /user
执行结果:
{
"acknowledged" : true
}
然后通过GET /user 查询已经404了:
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index [user]",
"resource.type" : "index_or_alias",
"resource.id" : "user",
"index_uuid" : "_na_",
"index" : "user"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index [user]",
"resource.type" : "index_or_alias",
"resource.id" : "user",
"index_uuid" : "_na_",
"index" : "user"
},
"status" : 404
}
文档操作
新增文档
# 新增文档:1代表文档id
POST /user/_doc/1
{
"info":"我是写PHP的程序员",
"email":"123@qq.com",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
执行结果:
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
查询文档
#查询文档
GET /user/_doc/1
结果:
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"info" : "我是写PHP的程序员",
"email" : "123@qq.com",
"name" : {
"firstName" : "云",
"lastName" : "赵"
}
}
}
全量修改文档
# 全量修改文档,id为1存在
PUT /user/_doc/1
{
"info":"我是写PHP的程序员",
"email":"zhaoyun@qq.com",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
执行结果:
{
"_index" : "user",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated", //修改
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
如果文档id存在就全量修改,如果不存在就新增
# 全量修改文档,id为10不存在
PUT /user/_doc/10
{
"info":"我是写PHP的程序员",
"email":"zhaoyun@qq.com",
"name":{
"firstName":"云",
"lastName":"赵"
}
}
结果:
{
"_index" : "user",
"_type" : "_doc",
"_id" : "10",
"_version" : 1,
"result" : "created", //新增
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
局部修改文档
# 局部修改文档,只修改email字段
POST /user/_update/1
{
"doc": {
"email":"ZhaoYun@qq.com"
}
}
删除文档
DELETE /user/_doc/1
查询所有文档
#查询所有
GET /hotel/_search
分词
提供了两个分词算法:
ik_max_word:最细粒度划分
POST /_analyze
{
"text": "程序员学习Java太棒了,白嫖",
"analyzer":"ik_max_word"
}
结果:
{
"tokens" : [
{
"token" : "程序员",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "程序",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "员",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "学习",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "java",
"start_offset" : 5,
"end_offset" : 9,
"type" : "ENGLISH",
"position" : 4
},
{
"token" : "太棒了",
"start_offset" : 9,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "太棒",
"start_offset" : 9,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "了",
"start_offset" : 11,
"end_offset" : 12,
"type" : "CN_CHAR",
"position" : 7
}
]
}
ik_smart:最少切分
POST /_analyze
{
"text": "程序员学习Java太棒了",
"analyzer":"ik_smart"
}
结果:
{
"tokens" : [
{
"token" : "程序员",
"start_offset" : 0,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "学习",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "java",
"start_offset" : 5,
"end_offset" : 9,
"type" : "ENGLISH",
"position" : 2
},
{
"token" : "太棒了",
"start_offset" : 9,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 3
}
]
}