ElasticSearch6(一) restful风格 基本操作CURD

 

一 索引创建

1.1 通过head插件去创建索引

1.2 通过 es http api 去创建

PUT http://192.168.239.128:9200/testindex/  Content-Type: application/json

{
  "settings": {
    "number_of_replicas": 0
  },
  "mappings": {
    "article": {
      "properties":{
        "id":{
          "type":"long"
        },
        "author":{
          "type":"keyword"
        },
        "title":{
          "type":"keyword"
        },
        "content":{
          "type":"text"
        },
        "publishDate":{
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        },
        "tags":{
          "type":"text"
        }
      }  
    }
  }
}

注意创建索引时,索引名称必须是小写,而且在6.0以上一个index只能建一个type

二 插入数据

2.1 指定id插入 (注意指定id和请求参数的id是不一样的)

{
  "id":2,
  "author":"test2",
  "title":"java",
  "content":"java 入门必选",
  "publishDate":"2018-08-03",
  "tags":[
  	"java"
  ]
}

2.2 不指定id,让es自动生成

POST http://192.168.239.128:9200/testindex/article Content-Type: application/json 

{
  "id":3,
  "author":"test3",
  "title":"php",
  "content":"php 入门必选",
  "publishDate":"2018-08-03",
  "tags":[
  	"PHP"
  ]
}

三 更新数据

3.1 不通过脚本更新

POST http://192.168.239.128:9200/testindex/article/1/_update 

{
  "doc":{
    "id":1,
    "author":"test",
    "title":"elasticsearch 入门",
    "content":"elasticsearch curd 入门必选 update",
    "publishDate":"2018-07-03",
    "tags":[
      "java","elasticsearch","update"
    ]
  }
}

注意返回报文,version发生变化 

3.2 通过脚本更新 

POST http://192.168.239.128:9200/testindex/article/1/_update 

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.id += 10"
	}
}

 意思是把doc中id 加上10

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.author = params.author",
		"params":{
			"author":"test update"
		}
	}
}

更新 author 字段

4 删除数据

4.1 指定id删除

DELETE http://192.168.239.128:9200/testindex/article/1 

4.2 根据条件删除

POST http://192.168.239.128:9200/testindex/article/_delete_by_query 

{
  "query":{
    "term":{
      "author":"test2"
    }
  }
}

五 简单查询

5.1 不带任何条件查询

POST http://192.168.239.128:9200/testindex/article/_search

5.2 带条件查询

{
  "query":{
    "term":{
      "author":"test3"
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值