ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

elasticsearch简单增删改查

2022-01-25 23:32:55  阅读:121  来源: 互联网

标签:index name people luoye 改查 elasticsearch 增删 type id


创建索引

put http://localhost:9200/luoye/   
Content-Type  application/json
{
	"settings": {
		"index": {
			"number_of_shards": "2",   --分片数
			"number_of_replicas": "0"  --副本
		}
	}
}
-----------------------------------------------------------------------
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "indsex": "luoye"
}

删除索引

Delete  http://localhost:9200/luoye/   
-----------------------------------------------------------------------
{
    "acknowledged": true
}

插入数据

1.指定了id,方法用put和post都可以

put http://localhost:9200/luoye/people/01    

{
	 "name”":"kettle",
     "age":25,
     "wetchat":"1732650",
     "sex":"男",
      "Height":175.5

}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",   
    "_version": 1,        --版本
    "result": "created",  --创建,如果再执行会变成updated, vsesion 会变成2 
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

2.未指定id 方法必须用post

post  http://localhost:9200/luoye/people 

{
	 "name”":"落叶",
     "age":25,
     "wetchat":"17326504*",
     "sex":"男",
      "Height":175.5

}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "cMQ1kX4BOCtS3EjrIwsn",     --随机生成的
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

3.批量插入数据

post http://localhost:9200/luoye/people/_bulk

 {"create" :{ "_index":"luoye","_type":"people","_id":"2022"}}
 {"id":"2022","name”":"大哥哥","age": 25 ,"address":"深圳","wetchat":"17326","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2023"}}
 {"id":"2023","name”":"Henry","age":25  ,"address":"汕头","wetchat":"1732650","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2024"}}
 {"id":"2024","name”":"说好的幸福","age":25  ,"address":"深圳","wetchat":"17326504","sex":"男"}
  {"create" :{ "_index":"luoye","_type":"people","_id":"2025"}}
 {"id":"2025","name”":"痛怎么伪装","age":25  ,"address":"珠海","wetchat":"173265","sex":"男"}    --这个后面要加\n
                                                                                              --的换行符
                                                                                              
 -----------------------------------------------------------------------
 {
    "took": 69,
    "errors": false,
    "items": [
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2022",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 5,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2023",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 19,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2024",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 20,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "create": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2025",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 6,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}
 


更新数据

1.全局更新,会直接覆盖之前的,方法可用put或者post

 post/put http://localhost:9200/luoye/people/01
 
 {
	 "name”":"kettle",
     "age":25,
     "wetchat":"1732650",
     "sex":"男",
     "address":"深圳"    -Height换成了address,后面再查询查不到Height,相当于删掉之前的,再换成现在的,只是id不变
}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 3,            --版本变化
    "result": "updated",      ---更新
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

2.部分更新,方法 post/put都可以

post/put http://localhost:9200/luoye/people/01
{
  "doc":{
    "name":"落叶",
      "age":25
  } 
}
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 5,              --版本
    "result": "updated",        --更新
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 1
}

删除数据

1.单条数据删除

Delete  http://localhost:9200/luoye/people/02
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "02",
    "_version": 2,
    "result": "deleted",         --成功为deleted,不成功为not_found
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

2.批量删除

 post http://localhost:9200/luoye/people/_bulk

{"delete" :{ "_index":"luoye","_type":"people","_id":"2022"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2023"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2024"}}
{"delete" :{ "_index":"luoye","_type":"people","_id":"2025"}}      --这里同样要有个换行符\n
-----------------------------------------------------------------------

{
    "took": 5,
    "errors": false,
    "items": [
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2022",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 7,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2023",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 21,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2024",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 22,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "delete": {
                "_index": "luoye",
                "_type": "people",
                "_id": "2025",
                "_version": 2,
                "result": "deleted",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 8,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}
 

查询数据

1.直接查询


Get   http://localhost:9200/luoye/people/01
-----------------------------------------------------------------------
{
    "_index": "luoye",
    "_type": "people",
    "_id": "01",
    "_version": 1,
    "_seq_no": 8,
    "_primary_term": 1,
    "found": true,              ---存在
    "_source": {
        "name”": "落叶",
        "age": 25,
        "wetchat": "17326504*",
        "sex": "男",
        "Height": 175.5
    }
}
-----------------------------------------------------------------------
{
    "_index": "luoye",       
    "_type": "people",
    "_id": "011",               
    "found": false              --- 不存在
}

2.获取全部数据

  Get http://localhost:9200/luoye/people/_search
-----------------------------------------------------------------------
  {
    "took": 831,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "b8QykX4BOCtS3EjrcwuQ",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650",
                    "sex": "男",
                    "Height": 175.5
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "cMQ1kX4BOCtS3EjrIwsn",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650",
                    "sex": "男",
                    "Height": 175.5
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.0,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*",
                    "sex": "男",
                    "Height": 175.5
                }
            }
        ]
    }
}

3.瘦身查询,有时我们不需要那么多字段,那么就可以指出只展现哪些字段就行

Get  http://localhost:9200/luoye/people/_search?_source=name”,age,wetchat
-----------------------------------------------------------------------
{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "b8QykX4BOCtS3EjrcwuQ",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "cMQ1kX4BOCtS3EjrIwsn",
                "_score": 1.0,
                "_source": {
                    "name”": "kettle",
                    "age": 25,
                    "wetchat": "1732650"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.0,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*"
                }
            },
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "_search ",
                "_score": 1.0,
                "_source": {}
            }
        ]
    }
}

4.多个数据进行查询,某个数据不存在,不影响查询

post  http://localhost:9200/luoye/people/_mget

{
  "ids":["01","cMQ1kX4BOCtS3EjrIwsn","02"]    --02数据就不存在,不影响

}
-----------------------------------------------------------------------
{
    "docs": [
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "01",
            "_version": 1,
            "_seq_no": 8,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name”": "落叶",
                "age": 25,
                "wetchat": "17326504*",
                "sex": "男",
                "Height": 175.5
            }
        },
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "cMQ1kX4BOCtS3EjrIwsn",
            "_version": 1,
            "_seq_no": 1,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name”": "kettle",
                "age": 25,
                "wetchat": "1732650",
                "sex": "男",
                "Height": 175.5
            }
        },
        {
            "_index": "luoye",
            "_type": "people",
            "_id": "02",
            "found": false
        }
    ]
}

3.关键字搜索

方法1:

  get  http://localhost:9200/luoye/people/_search?q=name\”:落叶     --name“字段里面打多了一个中文的“所以加转义
  -----------------------------------------------------------------------
  {
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.2199391,
        "hits": [
            {
                "_index": "luoye",
                "_type": "people",
                "_id": "01",
                "_score": 1.2199391,
                "_source": {
                    "name”": "落叶",
                    "age": 25,
                    "wetchat": "17326504*",
                    "sex": "男",
                    "Height": 175.5
                }
            }
        ]
    }
}

标签:index,name,people,luoye,改查,elasticsearch,增删,type,id
来源: https://blog.csdn.net/yu240956419/article/details/122693793

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有