【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

 

posted @   逆火狂飙  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需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
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示