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

  • 相关阅读:
    Raneto Docs(开源的知识库建站程序)
    Elasticsearch索引(company)_Centos下CURL增删改
    Elasticsearch配置详解、文档元数据
    Easticsearch通信方式_API
    全文检索学习历程目录结构(Lucene、ElasticSearch)
    Linux(CentOS 6.7)下配置Mono和Jexus并且部署ASP.NET MVC3、4、5和WebApi(跨平台)
    Apache Lucene(全文检索引擎)—分词器
    Apache Lucene(全文检索引擎)—搜索
    Apache Lucene(全文检索引擎)—创建索引
    Nginx主配置参数详解,Nginx配置网站
  • 原文地址:https://www.cnblogs.com/heyang78/p/16273223.html
Copyright © 2020-2023  润新知