本文主要包含elasticsearch等服务器相关知识,黎杰希望可以进行参考
- 1.添加一个学生的成绩(ip: port/库/表/id)
curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}'
- 2.通过浏览器用id查询学生成绩
http://192.168.80.123:9200/school/jsj/1
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
- 3.在linux中通过curl的方式用id查询学生成绩
curl -XGET 'http://192.168.80.123:9200/school/jsj/1'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
- 4.添加另外一个学生的成绩
curl -XPUT 'http://192.168.80.123:9200/school/jsj/2' -d '{
"subject": "math",
"name": {
"first": "zhang",
"last": "san"
},
"create_time": "2017-07-01",
"score": "59"
}'
- 5.通过_source获取指定的字段
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"subject": "math"
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source=subject,score'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"score": "98",
"subject": "math"
}
}
curl -XGET 'http://192.168.80.123:9200/school/jsj/1?_source'
结果:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"class": "软件工程",
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-10",
"score": "98"
}
}
- 6.可以通过覆盖的方式更新
curl -XPUT 'http://192.168.80.123:9200/school/jsj/1' -d '{
"subject": "math",
"name": {
"first": "li",
"last": "jie"
},
"create_time": "2017-08-11",
"score": "100"
}'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
再查看就变成100分了.
- 7.通过_updateAPI的方式单独更新你想要更新的
curl -XPOST 'http://192.168.80.123:9200/school/jsj/1/_update' -d '{
"doc": {
"score": "666"
}
}'
返回:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 4,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再查看就变成666分了.
- 8.删除一个文档
curl -XDELETE 'http://192.168.80.123:9200/school/jsj/1'
返回:
{
"found": true,
"_index": "school",
"_type": "jsj",
"_id": "1",
"_version": 5,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再查看id为1的返回数据为:
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"found": false
}
- 9.查询所有
curl -XGET 'http://192.168.80.123:9200/school/jsj/_search' -d '{
"query": { "match_all": {} }
}'
返回:
{
"took": 10, //执行搜索的时间(以毫秒为单位)
"timed_out": false, //是否超时
"_shards": { //搜索分片,成功和失败的分片
"total": 5, //总搜索分片
"successful": 5, //成功搜索分片
"failed": 0 //失败搜索分片
},
"hits": {
"total": 2, //符合我们的搜索条件的文档总数
"max_score": 1.0, //最高分数
"hits": [{ //搜索结果的实际数组(默认为前10个文档)
"_index": "school",
"_type": "jsj",
"_id": "2",
"_score": 1.0, //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
"_source": {
"subject": "math",
"name": {
"first": "zhang",
"last": "san"
},
"create_time": "2017-07-01",
"score": "59"
}
},
{
"_index": "school",
"_type": "jsj",
"_id": "1",
"_score": 1.0, //是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。
&quo

