基本操作
新增数据
{
"name" : "yhq",
"age" : 26,
"sex" : "男"
}
结果
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": { "total": 2, "successful": 1, "failed": 0 },
"_seq_no": 0,
"_primary_term": 1,
}
查询数据
GET yhq/user/1
结果
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": { "name": "yhq", "age": 26, "sex": "男" },
}
GET /yhq/user/_search?q=name:yhq
结果
{
"took": 46,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 1, "relation": "eq" },
"max_score": 0.6931471,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_score": 0.6931471,
"_source": { "name": "yhq-qhh", "age": 28, "sex": "男" },
},
],
},
}
更新数据
PUT /yhq/user/1
{
"name" : "yhq",
"age" : 28,
"sex" : "男"
}
结果
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": { "total": 2, "successful": 1, "failed": 0 },
"_seq_no": 2,
"_primary_term": 1,
}
POST /yhq/user/1/_update
{
"doc" : {
"name" : "yhq-qhh"
}
}
结果
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_version": 3,
"result": "updated",
"_shards": { "total": 2, "successful": 1, "failed": 0 },
"_seq_no": 3,
"_primary_term": 1,
}
复杂操作
查询
GET /yhq/user/_search
{
"query": {
"match": {
"name": "yhq"
}
},
"_source": ["name","age"],
"sort": [
{
"age":{
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
结果
{
"took": 5,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 2, "relation": "eq" },
"max_score": null,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "2",
"_score": null,
"_source": { "name": "yhq", "age": 26 },
"sort": [26],
},
],
},
}
GET /yhq/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "yhq"
}
},
{
"match": {
"age": 26
}
}
]
}
}
}
结果
{
"took": 6,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 1, "relation": "eq" },
"max_score": 1.5619608,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "2",
"_score": 1.5619608,
"_source": { "name": "yhq", "age": 26, "sex": "男" },
},
],
},
}
GET /yhq/user/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "yhq"
}
},
{
"match": {
"age": 26
}
}
]
}
}
}
结果
{
"took": 4,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 2, "relation": "eq" },
"max_score": 1.5619608,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "2",
"_score": 1.5619608,
"_source": { "name": "yhq", "age": 26, "sex": "男" },
},
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_score": 0.43445712,
"_source": { "name": "yhq-qhh", "age": 28, "sex": "男" },
},
],
},
}
GET /yhq/user/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "yhq"
}
}
]
}
}
}
结果
{
"took": 2,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 1, "relation": "eq" },
"max_score": 0.0,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "12",
"_score": 0.0,
"_source": { "name": "张三", "age": 27, "sex": "男" },
},
],
},
}
高亮
GET /yhq/user/_search
{
"query": {
"match": {
"name": "yhq"
}
},
"highlight": {
"fields": {
"name" :{}
},
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>"
}
}
结果
{
"took": 4,
"timed_out": false,
"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },
"hits":
{
"total": { "value": 2, "relation": "eq" },
"max_score": 0.5619608,
"hits":
[
{
"_index": "yhq",
"_type": "user",
"_id": "2",
"_score": 0.5619608,
"_source": { "name": "yhq", "age": 26, "sex": "男" },
"highlight":
{ "name": ["<p class='key' style='color:red'>yhq</p>"] },
},
{
"_index": "yhq",
"_type": "user",
"_id": "1",
"_score": 0.43445712,
"_source": { "name": "yhq-qhh", "age": 28, "sex": "男" },
"highlight":
{ "name": ["<p class='key' style='color:red'>yhq</p>-qhh"] },
},
],
},
}