一 索引创建
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"
}
}
}