ElasticSearch 创建索引、修改索引、删除索引、数据操作
文章目录# 索引操作1、创建简单索引2、创建带有分片、映射信息的索引3、修改索引3.1 修改索引副本数3.2 增加索引映射字段4、删除索引5、查看索引信息# 数据操作1、填充数据1.1 不指定数据ID1.2 指定数据ID2、删除数据3、更新数据4、查看数据# 数据查询环境:ElasticSearch版本:7.x工具:Postman# 索引操作1、创建简单索引curl -X PUT "127.0.0
·
文章目录
环境:
ElasticSearch版本:7.x
工具:Postman
# 索引操作
1、创建简单索引
curl -X PUT "127.0.0.1:9200/user"
索引创建成功会返回一下信息:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "user"
}
2、创建带有分片、映射信息的索引
curl -X PUT "127.0.0.1:9200/user"
参数:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"isteacher": {
"type": "boolean"
},
"createdate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
创建了一个分片数为 3,副本数为 2 的索引,同时,里面包含了 4 个字段,类型各不相同。
用 Postman 工具来一次性创建带有映射的索引:
3、修改索引
3.1 修改索引副本数
curl -X PUT "127.0.0.1:9200/user/settings"
参数:
{
"number_of_replicas": 3
}
3.2 增加索引映射字段
curl -X PUT "127.0.0.1:9200/user/_mapping"
参数:
{
"properties": {
"name2": {
"type": "text"
},
"age2": {
"type": "integer"
}
}
}
4、删除索引
curl -X DELETE '127.0.0.1:9200/user'
5、查看索引信息
curl -X GET '127.0.0.1:9200/user'
# 数据操作
1、填充数据
1.1 不指定数据ID
下面的示例不指定id
,则返回的数据中id
是随机的字符串;
不指定id
,则要使用POST
请求;
curl -X POST '127.0.0.1:9200/user/_doc'
参数:
{
"name": "jack",
"age": 16,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
1.2 指定数据ID
下面的示例,是将数据的id
指定为2,如果存在id
为2的数据,则会被覆盖;
curl -X PUT '127.0.0.1:9200/user/_doc/2'
参数:
{
"name": "jack",
"age": 16,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
2、删除数据
下面的示例为删除id
为2的数据;
curl -X DELETE '127.0.0.1:9200/accounts/user/_doc/2'
3、更新数据
下面的示例为更新id
为2的数据,如同指定id
添加数据;
curl -X PUT '127.0.0.1:9200/user/_doc/2'
参数:
{
"name": "jack",
"age": 16,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
返回:
{
"_index": "user",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 3,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
可以看到,记录的 Id
没变,但是版本(version
)从1变成2,操作类型(result
)从created
变成updated
。
4、查看数据
下面的示例为查看id
为2的数据:
curl -X GET '127.0.0.1:9200/user/_doc/2'
返回:
{
"_index": "user",
"_type": "_doc",
"_id": "2",
"_version": 2,
"_seq_no": 2,
"_primary_term": 1,
"found": true,
"_source": {
"name": "jack",
"age": 20,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
}
# 数据查询
1、返回所有记录
使用 GET 方法,直接请求/Index/_search
,就会返回所有记录。
curl -X GET '127.0.0.1:9200/user/_search'
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "dkddPnoBQYnp-UDGKlgC",
"_score": 1.0,
"_source": {
"name": "jack",
"age": 16,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "jack",
"age": 20,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
}
]
}
}
返回结果的 took
字段表示该操作的耗时(单位为毫秒),timed_out
字段表示是否超时,hits
字段表示命中的记录,里面子字段的含义如下。
total:返回记录数,本例是2条。
max_score:最高的匹配程度,本例是1.0。
hits:返回的记录组成的数组。
2、搜索
curl -X GET '127.0.0.1:9200/user/_search'
{
"query": {
"match" : {
"name" : "lily"
}
}
}
{
"took": 490,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.7549127,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "3",
"_score": 0.7549127,
"_source": {
"name": "lily",
"age": 10,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "4",
"_score": 0.55654144,
"_source": {
"name": "jack lily",
"age": 10,
"isteacher": false,
"createdate": "2021-06-24 20:50:32"
}
}
]
}
}
更多推荐
已为社区贡献7条内容
所有评论(0)