j记录索引、映射、文档的相关操作。
Elasticsearch基础操作
索引操作
创建索引
PUT /${index_name}
{
"settings":{
...
},
"mappings":{
...
}
}
示例:
PUT /wql
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
删除索引
DELETE /wql
关闭索引
POST /wql/_close
{
"acknowledged" : true,
"shards_acknowledged" : true,
"indices" : {
"wql" : {
"closed" : true
}
}
}
打开索引
POST /wql/_open
{
"acknowledged" : true,
"shards_acknowledged" : true
}
索引别名
POST /_aliases
{
"actions": [{
"add": {
"index": "reindex_old",
"alias": "index-alias"
}
},
{
"remove": {
"index": "reindex_new",
"alias": "index-alias"
}
}
]
}
映射操作
查看映射
GET /bank/_mapping
{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
扩展映射
POST /wql/_mapping
{
"properties": {
"address": {
"type": "keyword"
}
}
}
基本数据类型
text和keyword的区别
复杂数据类型
数组类型
# 增加标签字段
POST /hotel/_mapping
{
"properties":{
"tags": {
"type":"keyword"
}
}
}
# 插入数据
POST /hotel/_doc/1
{
"title": "文雅酒店",
"price": 300,
"full_room": true,
"create_time":"20211103",
"tags":["有车位", "免费"]
}
POST /hotel/_doc/2
{
"title": "文雅酒店",
"price": 300,
"full_room": true,
"create_time":"20211103",
"tags":["有车位", "免费WIFI"]
}
#查询数据
GET /hotel/_search
{
"query": {
"bool": {
"must": [
{"term": {
"tags": {
"value": "有车位"
}
}},
{"term": {
"tags": {
"value": "免费WIFI"
}
}}
]
}
}
}
# 结果
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.7587298,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.7587298,
"_source" : {
"title" : "文雅酒店",
"price" : 300,
"full_room" : true,
"create_time" : "20211103",
"tags" : [
"有车位",
"免费WIFI"
]
}
}
]
}
}
动态映射
生产禁用,因为映射生成的类型可能与用户实际的预期有差异
多字段
PUT /users
{
"mappings": {
"properties": {
"user_name":{
"type": "text",
"fields": {
"user_name_keyword":{
"type":"keyword"
}
}
}
}
}
}
GET /users/_mapping
{
"users" : {
"mappings" : {
"properties" : {
"user_name" : {
"type" : "text",
"fields" : {
"user_name_keyword" : {
"type" : "keyword"
}
}
}
}
}
}
}
# 这样我们就可以实现既可以按姓名进行搜索,又可以按姓名排序了
GET /users/_search
{
"query": {
"match": {
"user_name": "张三"
}
},
"sort": [
{
"user_name.user_name_keyword": {
"order": "desc"
}
}
]
}
文档操作
单条写入
POST /hotel/_doc/1
{
"title": "文雅酒店",
"price": 300,
"full_room": true,
"create_time":"20211103",
"tags":["有车位", "免费"]
}
批量写入
POST /_bulk
{"index":{"_index":"hotel", "_id":"2"}}
{"title": "文雅酒店","price": 300,"full_room": true,"create_time":"20211103","tags":["有车位", "免费"],"comment_info":{"favourable_commment":199,"negative_comment":68}}
{"index":{"_index":"users", "_id":"2"}}
{"user_name": "user_name"}
# 响应结果
{
"took" : 42,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "hotel",
"_type" : "_doc",
"_id" : "2",
"_version" : 10,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 17,
"_primary_term" : 1,
"status" : 200
}
},
{
"index" : {
"_index" : "users",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
}
]
}
单条更新
POST /hotel/_update/1
{
"doc": {
"title": "文雅酒店dag"
}
}
#更新结果
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "1",
"_version" : 12,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 21,
"_primary_term" : 1
}
批量更新
POST /_bulk
{"update":{"_index":"users", "_id":"2"}}
{"doc":{"user_name": "user_name"}}
根据条件更新
POST /users/_update_by_query
{
"query": {
"term": {
"user_name": {
"value": "user_name"
}
}
},
"script": {
"source": "ctx._source['user_name'] = 'wql'",
"lang": "painless"
}
}
单条删除
DELETE /hotel/_doc/1
批量删除
POST /_bulk
{"delete":{"_index":"users", "_id":"2"}}
{"delete":{"_index":"hotel", "_id":"2"}}
根据条件删除
POST /users/_delete_by_query
{
"query": {
"term": {
"user_name": {
"value": "wql"
}
}
}
}