【ES】排序输出
下文分四部分:
一.清除旧有数据
二.增添测试数据
三.设置对name进行优化
四.将查询结果按name升序输出
一.清除旧有数据
#删除所有旧有数据并删除索引结构
命令:
curl -u elastic:123456 -XDELETE 'localhost:9200/apple'
返回结果:
{"acknowledged":true}
二.增添测试数据
#插入数据 Andy
命令:
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/apple/emp/1?pretty' -d' {"name":"andy","age":"18","from":"China"}'
返回结果:
{ "_index" : "apple", "_type" : "emp", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
#插入数据 Bill
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/apple/emp/2?pretty' -d' {"name":"bill","age":"28","from":"England"}'
返回结果:
{ "_index" : "apple", "_type" : "emp", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
#插入数据 Cindy
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/apple/emp/3?pretty' -d' {"name":"Cindy","age":"38","from":"France"}'
返回结果:
{ "_index" : "apple", "_type" : "emp", "_id" : "3", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 2, "_primary_term" : 1 }
#插入数据 Douglas
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/apple/emp/4?pretty' -d' {"name":"Douglas","age":"48","from":"USA"}'
返回结果:
{ "_index" : "apple", "_type" : "emp", "_id" : "4", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1 }
#查询全体
curl -H "Content-Type: application/json" -XGET 'localhost:9200/apple/emp/_search?pretty' -d' {"query":{"match_all":{}}}'
返回结果:
{ "took" : 523, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "apple", "_type" : "emp", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "andy", "age" : "18", "from" : "China" } }, { "_index" : "apple", "_type" : "emp", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "bill", "age" : "28", "from" : "England" } }, { "_index" : "apple", "_type" : "emp", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "Cindy", "age" : "38", "from" : "France" } }, { "_index" : "apple", "_type" : "emp", "_id" : "4", "_score" : 1.0, "_source" : { "name" : "Douglas", "age" : "48", "from" : "USA" } } ] } }
三.设置对name进行优化
#优化emp的name字段
curl -X PUT "localhost:9200/apple/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "name": { "type": "text", "fielddata": true } } } '
返回结果:
{ "acknowledged" : true }
四.将查询结果按name升序输出
#按name升序查询全体
curl -H "Content-Type: application/json" -XGET 'localhost:9200/apple/emp/_search?pretty' -d' { "query":{"match_all":{} }, "sort":[{"name":{"order":"asc"}}] }'
返回结果:
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "apple", "_type" : "emp", "_id" : "1", "_score" : null, "_source" : { "name" : "andy", "age" : "18", "from" : "China" }, "sort" : [ "andy" ] }, { "_index" : "apple", "_type" : "emp", "_id" : "2", "_score" : null, "_source" : { "name" : "bill", "age" : "28", "from" : "England" }, "sort" : [ "bill" ] }, { "_index" : "apple", "_type" : "emp", "_id" : "3", "_score" : null, "_source" : { "name" : "Cindy", "age" : "38", "from" : "France" }, "sort" : [ "cindy" ] }, { "_index" : "apple", "_type" : "emp", "_id" : "4", "_score" : null, "_source" : { "name" : "Douglas", "age" : "48", "from" : "USA" }, "sort" : [ "douglas" ] } ] } }
END