• 【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

  • 相关阅读:
    【转载】中文分词整理
    【转载】浅谈事件冒泡与事件捕获
    【转载】SpringCloud-Eurek 心跳阈值说明
    【转载】Linux下查看CPU、内存占用率
    Linux内存、性能诊断中vmstat命令的详解
    【转载】springboot四 全局异常处理
    【转载】linux系统时间自动同步:ntp
    springboot整合三 共享session,集成springsession
    git把一个分支上的某个提交合并到另一个分支
    VS Code打开新的文件会覆盖窗口中的,怎么改
  • 原文地址:https://www.cnblogs.com/heyang78/p/16273223.html
Copyright © 2020-2023  润新知