【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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-05-15 SQL Builder 1.04
2018-05-15 【Python】由host得到IP
2018-05-15 【Python】使用geopy由经纬度找地理信息
2018-05-15 【Python】使用geopy由地址找经纬度等信息
2018-05-15 【Python】安装geopy
2018-05-15 【Python】使用geocoder找出本机IP所在经纬度和城市
2018-05-15 【Python】安装geocoder