索引
创建索引库
ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。
创建索引库和mapping的DSL语法如下:
PUT /索引库名称
{
"mappings":{
"properties":{
"字段名1":{
"type":"text",
"analyzer":"ik_smart"
},
"字段名2":{
"type":"keyword",
"index":"false"
},
"字段名3":{
"properties":{
"子字段":{
"type":"keyword"
}
}
},
// ...略
}
}
}
实例
PUT /es_test
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": "false"
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
}
}
}
}
执行结果
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "es_test"
}
查询索引库
语法:
GET /索引库名
实例
GET /es_test
执行结果
{
"es_test" : {
"aliases" : { },
"mappings" : {
"properties" : {
"email" : {
"type" : "keyword",
"index" : false
},
"info" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"name" : {
"properties" : {
"firstName" : {
"type" : "keyword"
},
"lastName" : {
"type" : "keyword"
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1687487996291",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "cQ_LgHSOQn-1KdWTR0YL0A",
"version" : {
"created" : "7060199"
},
"provided_name" : "es_test"
}
}
}
}
删除索引库
语法:
DELETE /索引库名
实例:
DELETE /es_test1
执行结果
{
"acknowledged" : true
}
修改索引库
es中索引库不允许修改,修改会导致倒排索引失效
但允许向索引库中添加新的字段,语法如下:
PUT /索引库名/_mapping
{
"properties":{
"新字段名":{
"type":"integer"
}
}
}
字段名必须是全新的,否则以为是修改原有的,会报错
实例
PUT /es_test/_mapping
{
"properties": {
"age":{
"type":"integer"
}
}
}
执行结果
{
"acknowledged" : true
}
再次修改type为long类型
PUT /es_test/_mapping
{
"properties": {
"age":{
"type":"long"
}
}
}
报错
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "mapper [age] cannot be changed from type [integer] to [long]"
}
],
"type" : "illegal_argument_exception",
"reason" : "mapper [age] cannot be changed from type [integer] to [long]"
},
"status" : 400
}
Reference
黑马程序员