栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticSearch核心概念的介绍(三):文档的增删改查

elasticSearch核心概念的介绍(三):文档的增删改查

文档的增删改查

在上一章介绍了映射的使用,有兴趣的朋友可以参考一下
elasticSearch核心概念的介绍(二):映射的介绍和使用
这一章我们来简单介绍 一下 文档的使用,在es中文档也就是指的数据,如同mysql中的表数据。

新增文档

请求

curl -X PUT "http://172.25.45.150:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
    "name":"哈登",
    "team_name":"火箭",
    "position":"得分后卫",
    "play_year":"10",
    "jerse_no":"13"
}
'

http://172.25.45.150:9200/nba/_doc/1 1:为id 我们可以自行指定,我们也可以不指定id进行创建

http://172.25.45.150:9200/nba/_doc 不指定id的话 为POST请求

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

获取文档信息

请求

curl -X GET "http://172.25.45.150:9200/nba/_doc/1" 

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "哈登",
        "team_name": "火箭",
        "position": "得分后卫",
        "play_year": "10",
        "jerse_no": "13"
    }
}

自动创建索引

查看auto_create_index 开关状态

请求

curl -X GET "http://172.25.45.150:9200/_cluster/settings"

响应:

{
    "persistent": {
        "action": {
            "destructive_requires_name": "true"
        }
    },
    "transient": {}
}

当索引不存在并且auto_create_index为true的时候,新增文档的时候如果不存在该索引就会自动创建索引

修改auto_create_index状态

请求

curl -X PUT "http://172.25.45.150:9200/_cluster/settings" -H 'Content-Type:application/json' -d '
	{
		"persistent":{
			"action.auto_create_index":false
		}
	}
'

响应

{
    "acknowledged": true,
    "persistent": {
        "action": {
            "auto_create_index": "false"
        }
    },
    "transient": {}
}

修改之后再次进行 curl -X GET “http://172.25.45.150:9200/_cluster/settings” 会发现多了一条auto_create_index=false的字段。

当auto_create_index=false时新增一个不存在索引的文档。

请求

curl -X PUT "http://172.25.45.150:9200/wnba/_doc/1" -H 'Content-Type:application/json' -d '

{
    "name":"杨超越",
    "team_name":"梦之队",
    "position":"组织后卫",
    "play_year":"0",
    "jerse_no":"18"
}

'

响应

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

将auto_create_index设置为true,我们再来试试创建文档

响应

{
    "_index": "wnba",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

会发现wnba的索引会帮我们创建,同时mapping也会根据文档信息帮我们自动创建 ,我们来试一下

请求

curl -X GET "http://172.25.45.150:9200/wnba/_mapping"

响应

{
    "wnba": {
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "play_year": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "position": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "team_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

查看多个文档

请求一

curl -X POST "http://172.25.45.150:9200/_mget" -H 'Content-Type:application/json' -d '
{
    "docs":[
        {
            "_index":"nba",
            "_type":"_doc",
            "_id":"1"
        },
        {
            "_index":"nba",
            "_type":"_doc",
            "_id":"2"
        }
    ]
}
'

请求二

curl -X POST "http://172.25.45.150:9200/nba/_mget" -H 'Content-Type:application/json' -d '
{
    "docs":[
        {
            "_type":"_doc",
            "_id":"1"
        },
        { 
            "_type":"_doc",
            "_id":"2"
        }
    ]
}
'

请求三

curl -X POST "http://172.25.45.150:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
{
    "docs":[
        {
            "_id":"1"
        },
        {  
            "_id":"2"
        }
    ]
}
'

请求四

curl -X POST "http://172.25.45.150:9200/nba/_doc/_mget" -H 'Content-Type:application/json' -d '
{
    "ids":["1","2"]
}
'

响应

{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 1,
            "_seq_no": 0,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "哈登",
                "team_name": "火箭",
                "position": "得分后卫",
                "play_year": "10",
                "jerse_no": "13"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

修改文档

根据提供的文档片段更新数据

请求

curl -X POST "http://172.25.45.150:9200/nba/_update/1" -H 'Content-Type:application' -d '
{
    "doc":{
    "name":"哈登",
    "team_name":"火箭",
    "position":"双能卫",
    "play_year":"10",
    "jerse_no":"13"
	}
}
'

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

删除文档

请求

curl -X DELETE "http://172.25.45.150:9200/nba/_doc/22"

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "22",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 10,
    "_primary_term": 1
}

_source 字段

在上面请求文档数据的时候我们会发现,我们需要的数据存放在_source的字段下面,_source他是作为一个载体,我们如果需要修改一些字段的信息,也可以通过_srouce去操作

新增一个字段(age)

请求

curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
	{
        "script" : "ctx._source.age=18"
    }
'

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

新增成功之后,再通过

curl -X GET “http://172.25.45.150:9200/nba/_doc/1”

就会发现_srouce下面多了一个age字段

删除一个字段

请求

curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
	{
        "script" : "ctx._source.remove("age")"
    }
'

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

修改字段

请求

curl -X POST "http://172.25.450.156:9200/nba/_update/1" -H 'Content-Type:application/json' -d '
	{
        "script":{
        	"source":"ctx._source.age += params.age",
        	 "params":{
        		"age":4
        	  }
        }
   '

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 7,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

修改不存在的文档字段

请求

curl -X POST "http://172.25.450.156:9200/nba/_update/22" -H 'Content-Type:application/json' -d '
	{
        "script":{
        	"source":"ctx._source.age += params.allstart",
        	 "params":{
        		"allstart":4
        	  }
        },
        "upsert":{
        	"allstart":1
        }
       }
   '

请求说明:

upsert: 当指定的文档不存在时,upsert参数包含的内容将会被插入到索引中,作为一个新文档;如果指定的文档存在,ElasticSearch引擎将会执行指定的更新逻辑(script),通过在创建该文档的数据同事,会更新同步mapping的对应属性。

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "22",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 9,
    "_primary_term": 1
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/741871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号