新建文档(动态创建)
系统会自动帮助创建"表结构",缺点:创建出来的数据类型不够严谨,比如时间字段"createTime"会自动创建为字符型
//格式 [PUT] http://host:port/索引名称/文档类型/文档Id 文档id,可省略,当省略时,系统自动生成 //例如 [PUT] http://host:port/news/news_info/1 //参数 { "newsType ":"sport", "title":"中国国奥队无缘东京奥运会", "author":true, "readNumber":1, "createTime":"2020-1-14 17:16:22", "content":"1月12日,中国国奥队球员胡靖航(左)在比赛中带球突破。 当日..." } //返回 { "_index": "news", "_type": "news_info", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 3 }
新建文档(静态创建)
有点类似于数据库中的创建表结构
//格式 [PUT] http://host:port/索引名称 文档id,可省略,当省略时,系统自动生成 //例如 [PUT] http://host:port/news //参数 { "mappings":{ "news_info":{ "_all":{"enabled":false}, "properties":{ "id":{"type":"text"}, "title":{"type":"text"}, "author":{"type":"text"}, "createTime":{ "type": "date","format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"}, "newsType":{"type":"keyword"}, "readNumber":{"type":"long"} } } } }
获取文档信息
//格式
[GET] http://host:port/索引名称/文档类型/文档Id
//例如
[GET] http://host:port/news/news_info/1
//返回
{
"_index": "news",
"_type": "news_info",
"_id": "1",
"_version": 1,
"_seq_no": 1,
"_primary_term": 3,
"found": true,
"_source": {
"newsType ": "sport",
"title": "中国国奥队无缘东京奥运会",
"author": true,
"read_number": 1,
"create_time": "2020-1-14 17:16:22",
"content": "1月12日,中国国奥队球员胡靖航(左)在比赛中带球突破。 当日,在..."
}
}
更新文档内容
//格式
[POST] http://host:port/索引名称/文档类型/文档Id/_update
//例如
[POST] http://host:port/news/news_info/1/_update
//参数
①普通方式
{
"doc":{
"title":"中国国奥队无缘东京奥运会UPDATE-TEST"
}
}
②脚本方式
{
"script":{
"inline":"ctx._source.title = params.title",
"params":{
"title":"中国国奥队无缘东京奥运会UPDATE"
}
}
}
//返回
{
"_index": "news",
"_type": "news_info",
"_id": "1",
"_version": 8,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 7,
"_primary_term": 3
}
查询文档
//格式
[GET] http://host:port/索引名称/文档类型/_search
//例如
[GET] http://host:port/news/news_info/_search
//返回
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": [
{
"_index": "news",
"_type": "news_info",
"_id": "2",
"_score": 1.0,
"_source": {
"newsType ": "sport",
"title": "中国国奥队无缘东京奥运会",
"author": true,
"read_number": 1,
"create_time": "2020-1-14 17:16:22",
"content": "1月12日,中国国奥队球员胡靖航..."
}
},
{
"_index": "news",
"_type": "news_info",
"_id": "1",
"_score": 1.0,
"_source": {
"newsType ": "sport",
"title": "中国国奥队无缘东京奥运会UPDATE-TEST",
"author": true,
"read_number": 1,
"create_time": "2020-1-14 17:16:22",
"content": "1月12日,中国国奥队球员胡靖航..."
}
}
]
}
}
说明
{
"took": "整个搜索用了所少毫秒",
"timed_out": "查询是否超时",
"_shards": {
"total": "查询中参与分片的总数",
"successful": "查询成功的分片数",
"skipped": "没有参与的分片数",
"failed": "查询失败的分片数"
},
"hits": {
"total": 2,
"max_score": 1.0,
"hits": []
}
}
删除文档
//格式
[DELETE] http://host:port/索引名称/文档类型/文档Id
//例如
[DELETE] http://host:port/news/news_info/2
//返回
{
"_index": "news",
"_type": "news_info",
"_id": "2",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 3
}
。