elasticsearch简单增删改查

创建索引

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
}

更新数据

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
}

删除数据

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
}

查询数据

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.关键字搜索

方法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
                }
            }
        ]
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落叶@Henry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值