elasticsearch学习3--索引与文档的CURD

CURD代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)操作。

一、索引的CURD

1.创建

{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "dynamic": false,
    "properties": {
      "id": {
        "type": "integer"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "createAt": {
        "type": "date"
      }
    }
  }
}

number_of_shards:设置分片数量,默认为5
number_of_replicas:设置副本数量,默认为1
dynamic:动态映射配置

通过dynamic参数来控制字段的新增:
true:默认,允许自动新增字段,但是mapping不显示,查询返回JSON有
false:不允许自动新增字段,但是文档可以正常写入,但无法对新增字段进行查询等操作
strict:文档不能写入,报错
dynamic mapping详细说明

type:表示字段类型
analyzer:存储时的分词器,将text类型的字段做分词然后插入倒排索引
search_analyzer:查询时的分词器,先对要查询的text类型的输入做分词,再去倒排索引搜索

创建成功返回的信息:

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_008"
}

acknowledged: 表示在集群中创建索引是否成功
shards_acknowledged: 表示在集群中创建索引分片是否成功

2.读取(查询)

查询单个索引

GET /test_008

查询多个或全部索引

GET /test_007,test_008
GET /*    

查询test_008结果:

{
  "test_008": {
    "aliases": {},
    "mappings": {
      "dynamic": "false",
      "properties": {
        "createAt": {
          "type": "date"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1603433624241",
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "uuid": "DY_Hs_DMSq6ppp-Hm6GjoA",
        "version": {
          "created": "7030299"
        },
        "provided_name": "test_008"
      }
    }
  }
}

3.更新

常用的更新index的操作:

新增字段、修改副本数、修改分片刷新时间

#新增字段
PUT /test_008/_mapping/_doc 
{
  "properties": {
    "age": {
      "type": "integer"
    }
  }
}
#修改副本数
PUT /test_008/_settings
{
    "index" : {
        "number_of_replicas" : 2
    }
}
#修改分片刷新时间,默认为1s
PUT /test_008/_settings
{
    "index" : {
        "refresh_interval" : "2s"
    }
}

修改之后:

{
  "test_008": {
    "aliases": {},
    "mappings": {
      "dynamic": "false",
      "properties": {
        "age": {
          "type": "integer"
        },
        "createAt": {
          "type": "date"
        },
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        }
      }
    },
    "settings": {
      "index": {
        "refresh_interval": "2s",
        "number_of_shards": "1",
        "provided_name": "test_008",
        "creation_date": "1603433624241",
        "number_of_replicas": "2",
        "uuid": "DY_Hs_DMSq6ppp-Hm6GjoA",
        "version": {
          "created": "7030299"
        }
      }
    }
  }
}

4.删除

#删除索引
DELETE /test_008
#验证索引是否存在
GET test_008

{
  "error": {
    "root_cause": [
      {
        "type": "index_not_found_exception",
        "reason": "no such index [test_008]",
        "resource.type": "index_or_alias",
        "resource.id": "test_008",
        "index_uuid": "_na_",
        "index": "test_008"
      }
    ],
    "type": "index_not_found_exception",
    "reason": "no such index [test_008]",
    "resource.type": "index_or_alias",
    "resource.id": "test_008",
    "index_uuid": "_na_",
    "index": "test_008"
  },
  "status": 404
}

二、文档(数据)的CURD

1.创建

# 新增单条数据,并指定es的id  1
PUT /test_001/_doc/1?pretty
{
  "name": "cape"
}
# 新增单条数据,使用ES自动生成id
POST /test_001/_doc?pretty
{
  "name": "cape"
}
# 查询结果
GET /test_001/_doc/_search
{
	"_index": "test_001",
	"_type": "_doc",
	"_id": "1",
	"_score": 1,
	"_source": {
		"name": "cape"
	}
},
{
	"_index": "test_001",
	"_type": "_doc",
	"_id": "bV2gq3UBAbeCckVd4Mms",
	"_score": 1,
	"_source": {
		"name": "cape"
	}
}

2.读取(查询)

# 1、根据id,获取单个数据
GET /test_001/_doc/1
结果:
{
	"_index": "test_001",
	"_type": "_doc",
	"_id": "1",
	"_version": 1,
	"_seq_no": 0,
	"_primary_term": 1,
	"found": true,
	"_source": {
		"name": "cape"
	}
}
# 2、获取索引下的所有数据
GET /test_001/_doc/_search
结果:
{
	"took": 0,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [{
			"_index": "test_001",
			"_type": "_doc",
			"_id": "1",
			"_score": 1,
			"_source": {
				"name": "cape"
			}
		},
		{
			"_index": "test_001",
			"_type": "_doc",
			"_id": "bV2gq3UBAbeCckVd4Mms",
			"_score": 1,
			"_source": {
				"name": "cape"
			}
		}]
	}
}
# 3、条件查询(下一节详细介绍)
POST /test_001/_doc/_search
{
  "query": {
    "match": {
      "name": "cape2"
    }
  }
}
结果:
{
	"took": 0,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 0.9808292,
		"hits": [{
			"_index": "test_001",
			"_type": "_doc",
			"_id": "gF6zq3UBAbeCckVd_JVA",
			"_score": 0.9808292,
			"_source": {
				"name": "cape2"
			}
		}]
	}
}

3.更新

# 根据id,修改单条数据
(ps:修改语句和新增语句相同,可以理解为根据ID,存在则更新;不存在则新增)
PUT /test_001/_doc/1?pretty
{
  "name": "cape3"
}
# 根据查询条件id=1,修改name="更新后的name"
(版本冲突而不会导致_update_by_query 中止)
POST test_001/_update_by_query
{
  "script": {
    "source": "ctx._source.name = params.name",
    "lang": "painless",
    "params":{
      "name":"更新后的name"
    }
  },
  "query": {
    "term": {
      "id": "1"
    }
  }
}

4.删除

# 1、根据id,删除单个数据
DELETE /test_001/_doc/1
# 2、delete by query
POST test_001/_delete_by_query
{
  "query": { 
    "match": {
     "name": "2"
    }
  }
}

三、批量操作

# 批量操作
POST _bulk
{ "index" : { "_index" : "test_001", "_type" : "_doc", "_id" : "1" } }
{ "this_is_field1" : "this_is_index_value" }
{ "delete" : { "_index" : "test_001", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test_001", "_type" : "_doc", "_id" : "3" } }
{ "this_is_field3" : "this_is_create_value" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test_001"} }
{ "doc" : {"this_is_field2" : "this_is_update_value"} }
# 查询所有数据
GET /test_001/_doc/_search
结果:
{
  "took": 33,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "test_001",
        "_type": "_doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "this_is_field1": "this_is_index_value",
          "this_is_field2": "this_is_update_value"
        }
      },
      {
        "_index": "test_001",
        "_type": "_doc",
        "_id": "3",
        "_score": 1,
        "_source": {
          "this_is_field3": "this_is_create_value"
        }
      }
    ]
  }
}

导入或者更新时,使用元数据来完成数据的批量导入,每导入一条数据,由两行构成,一条是元信息,另一条是数据行

POST _bulk进行的操作
1、若索引“test_001”不存在,则创建一个名为“test_001”的index,同时若id = 1 的文档存在,则更新;不存在则插入一条 id=1 的文档;
2、删除 id=2 的文档;
3、插入 id=3 的文档;若文档已存在,则报异常;
4、更新 id = 1 的文档。

ps:批量操作在实践中使用是比较多的,因为减少了IO,提高了效率!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值