文档
创建文档第一种方式
使用_doc
请求协议
POST/PUT
请求路径
http://ip:port/${index}/_doc
http://ip:port/${index}/_doc/${id}
请求body体
{
"msgid":0,
"from":1,
"to":2,
"content":"123123"
}
返回结果
如果请求路径传入${id}则_id使用传入的,否则使用随机生成的
{
"_index": "chat",
"_type": "_doc",
"_id": "OIK6bHsBeMYl6XLQxKXj",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
创建文档第二种方式
使用_create
请求协议
POST/PUT
请求路径
http://ip:port/${index}/_create/${id}
请求body体
{
"msgid":8,
"from":1,
"to":2,
"content":"123123"
}
返回结果
{
"_index": "chat",
"_type": "_doc",
"_id": "8",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 20,
"_primary_term": 1
}
查询文档
请求协议
GET
请求路径
http://ip:port/${index}/_doc/${id}
请求body体
无
返回结果
found==true?找到了:没有找到
{
"_index": "chat",
"_type": "_doc",
"_id": "12",
"_version": 1,
"_seq_no": 26,
"_primary_term": 1,
"found": true,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
}
查询index下的所有文档
请求协议
GET
请求路径
http://ip:port/${index}/_search
请求body体
无
返回结果
{
"took": 930,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 13,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "chat",
"_type": "_doc",
"_id": "OIK6bHsBeMYl6XLQxKXj",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "0",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "OYLHbHsBeMYl6XLQUKUE",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"msgid": 1,
"from": 1,
"to": 2,
"content": "456456"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "4",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "5",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "6",
"_score": 1.0,
"_source": {
"msgid": 6,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "7",
"_score": 1.0,
"_source": {
"msgid": 6,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "8",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
},
{
"_index": "chat",
"_type": "_doc",
"_id": "11",
"_score": 1.0,
"_source": {
"msgid": 0,
"from": 1,
"to": 2,
"content": "123123"
}
}
]
}
}
条件查询文档
请求协议
GET
请求路径
http://ip:port/${index}/_search
请求body体
{
"query":{
"match":{
"from":"1"
}
},
"from":0,
"size":3,
"_source":[
"msgid",
"from"
],
"sort":{
"msgid":{
"order":"desc"
}
}
}
query 为查询条件
from为起始位置,size为查询的条数
_source为需要的查询字段
sort为排序
返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 11,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "chat",
"_type": "_doc",
"_id": "PYLTbHsBeMYl6XLQxqUJ",
"_score": null,
"_source": {
"msgid": 13,
"from": 1
},
"sort": [
13
]
},
{
"_index": "chat",
"_type": "_doc",
"_id": "6",
"_score": null,
"_source": {
"msgid": 6,
"from": 1
},
"sort": [
6
]
},
{
"_index": "chat",
"_type": "_doc",
"_id": "7",
"_score": null,
"_source": {
"msgid": 6,
"from": 1
},
"sort": [
6
]
}
]
}
}
多条件查询文档
请求协议
GET
请求路径
http://ip:port/${index}/_search
请求body体
{
"query":{
"bool":{
"should":[
{
"match_phrase":{
"from":1
}
},
{
"match":{
"form":2
}
}
],
"filter":{
"range":{
"msgid":{
"gt":2
}
}
}
}
},
"highlight":{
"fields":{
"msgid":{
}
}
}
}
bool 为多个条件查询
must为多个条件都成立相当于and,如果想用or改为should
如果想要进行范围查找使用filter加range
from为起始位置,size为查询的条数
_source为需要的查询字段
sort为排序
match为全文检索匹配(会将内容进行分词),match_phrase完全匹配
highlight为高亮显示
返回结果
多条件查询文档
请求协议
GET
请求路径
http://ip:port/${index}/_search
请求body体
{
"aggs":{ // 聚合操作
"from_group":{ // 聚合后的名称,类似于MySql中的as
"terms":{ //分组操作
"field":"from" //分组字段
}
}
},
"size":0
}
aggs为聚合操作
from_group为聚合后的名称,类似于MySql中的as
terms:分组操作,avg:平均值
field:分组字段
size:0:屏蔽原始数据
返回结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 12,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"from_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 8
},
{
"key": 2,
"doc_count": 4
}
]
}
}
}
修改文档
请求协议
POST
请求路径
http://ip:port/${index}/_update/${id}
请求body体
{
"doc":{
"content":"修改内容"
}
}
content为修改的文档的字段
返回结果
{
"_index":"chat",
"_type":"_doc",
"_id":"1",
"_version":14,
"result":"updated",
"_shards":{
"total":2,
"successful":1,
"failed":0
},
"_seq_no":28,
"_primary_term":1
}
删除文档
请求协议
DELETE
请求路径
http://ip:port/${index}/_doc/${id}
请求body体
无
返回结果
{
"_index": "chat",
"_type": "_doc",
"_id": "1",
"_version": 16,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 31,
"_primary_term": 1
}