zoukankan      html  css  js  c++  java
  • ES基础(三十九)作用范围与排序

     

     

     

     

     

    DELETE /employees
    PUT /employees/
    {
      "mappings" : {
          "properties" : {
            "age" : {
              "type" : "integer"
            },
            "gender" : {
              "type" : "keyword"
            },
            "job" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 50
                }
              }
            },
            "name" : {
              "type" : "keyword"
            },
            "salary" : {
              "type" : "integer"
            }
          }
        }
    }
    
    PUT /employees/_bulk
    { "index" : {  "_id" : "1" } }
    { "name" : "Emma","age":32,"job":"Product Manager","gender":"female","salary":35000 }
    { "index" : {  "_id" : "2" } }
    { "name" : "Underwood","age":41,"job":"Dev Manager","gender":"male","salary": 50000}
    { "index" : {  "_id" : "3" } }
    { "name" : "Tran","age":25,"job":"Web Designer","gender":"male","salary":18000 }
    { "index" : {  "_id" : "4" } }
    { "name" : "Rivera","age":26,"job":"Web Designer","gender":"female","salary": 22000}
    { "index" : {  "_id" : "5" } }
    { "name" : "Rose","age":25,"job":"QA","gender":"female","salary":18000 }
    { "index" : {  "_id" : "6" } }
    { "name" : "Lucy","age":31,"job":"QA","gender":"female","salary": 25000}
    { "index" : {  "_id" : "7" } }
    { "name" : "Byrd","age":27,"job":"QA","gender":"male","salary":20000 }
    { "index" : {  "_id" : "8" } }
    { "name" : "Foster","age":27,"job":"Java Programmer","gender":"male","salary": 20000}
    { "index" : {  "_id" : "9" } }
    { "name" : "Gregory","age":32,"job":"Java Programmer","gender":"male","salary":22000 }
    { "index" : {  "_id" : "10" } }
    { "name" : "Bryant","age":20,"job":"Java Programmer","gender":"male","salary": 9000}
    { "index" : {  "_id" : "11" } }
    { "name" : "Jenny","age":36,"job":"Java Programmer","gender":"female","salary":38000 }
    { "index" : {  "_id" : "12" } }
    { "name" : "Mcdonald","age":31,"job":"Java Programmer","gender":"male","salary": 32000}
    { "index" : {  "_id" : "13" } }
    { "name" : "Jonthna","age":30,"job":"Java Programmer","gender":"female","salary":30000 }
    { "index" : {  "_id" : "14" } }
    { "name" : "Marshall","age":32,"job":"Javascript Programmer","gender":"male","salary": 25000}
    { "index" : {  "_id" : "15" } }
    { "name" : "King","age":33,"job":"Java Programmer","gender":"male","salary":28000 }
    { "index" : {  "_id" : "16" } }
    { "name" : "Mccarthy","age":21,"job":"Javascript Programmer","gender":"male","salary": 16000}
    { "index" : {  "_id" : "17" } }
    { "name" : "Goodwin","age":25,"job":"Javascript Programmer","gender":"male","salary": 16000}
    { "index" : {  "_id" : "18" } }
    { "name" : "Catherine","age":29,"job":"Javascript Programmer","gender":"female","salary": 20000}
    { "index" : {  "_id" : "19" } }
    { "name" : "Boone","age":30,"job":"DBA","gender":"male","salary": 30000}
    { "index" : {  "_id" : "20" } }
    { "name" : "Kathy","age":29,"job":"DBA","gender":"female","salary": 20000}
    
    
    
    # Query
    POST employees/_search
    {
      "size": 0,
      "query": {
        "range": {
          "age": {
            "gte": 20
          }
        }
      },
      "aggs": {
        "jobs": {
          "terms": {
            "field":"job.keyword"
            
          }
        }
      }
    }
    
    
    #Filter
    POST employees/_search
    {
      "size": 0,
      "aggs": {
        "older_person": {
          "filter":{
            "range":{
              "age":{
                "from":35
              }
            }
          },
          "aggs":{
             "jobs":{
               "terms": {
            "field":"job.keyword"
          }
          }
        }},
        "all_jobs": {
          "terms": {
            "field":"job.keyword"
            
          }
        }
      }
    }
    
    
    
    #Post field. 一条语句,找出所有的job类型。还能找到聚合后符合条件的结果
    POST employees/_search
    {
      "aggs": {
        "jobs": {
          "terms": {
            "field": "job.keyword"
          }
        }
      },
      "post_filter": {
        "match": {
          "job.keyword": "Dev Manager"
        }
      }
    }
    
    
    #global
    POST employees/_search
    {
      "size": 0,
      "query": {
        "range": {
          "age": {
            "gte": 40
          }
        }
      },
      "aggs": {
        "jobs": {
          "terms": {
            "field":"job.keyword"
            
          }
        },
        
        "all":{
          "global":{},
          "aggs":{
            "salary_avg":{
              "avg":{
                "field":"salary"
              }
            }
          }
        }
      }
    }
    
    
    #排序 order
    #count and key
    POST employees/_search
    {
      "size": 0,
      "query": {
        "range": {
          "age": {
            "gte": 20
          }
        }
      },
      "aggs": {
        "jobs": {
          "terms": {
            "field":"job.keyword",
            "order":[
              {"_count":"asc"},
              {"_key":"desc"}
              ]
            
          }
        }
      }
    }
    
    
    #排序 order
    #count and key
    POST employees/_search
    {
      "size": 0,
      "aggs": {
        "jobs": {
          "terms": {
            "field":"job.keyword",
            "order":[  {
                "avg_salary":"desc"
              }]
            
            
          },
        "aggs": {
          "avg_salary": {
            "avg": {
              "field":"salary"
            }
          }
        }
        }
      }
    }
    
    
    #排序 order
    #count and key
    POST employees/_search
    {
      "size": 0,
      "aggs": {
        "jobs": {
          "terms": {
            "field":"job.keyword",
            "order":[  {
                "stats_salary.min":"desc"
              }]
            
            
          },
        "aggs": {
          "stats_salary": {
            "stats": {
              "field":"salary"
            }
          }
        }
        }
      }
    }

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/14197787.html

  • 相关阅读:
    changing a pointer rather than erasing memory cells
    验证码识别 edge enhancement 轮廓增强 region finding 区域查找
    Manipulating Data Structures
    passing parameters by value is inefficient when the parameters represent large blocks of data
    Aliasing 走样
    Artificial Intelligence Research Methodologies 人工智能研究方法
    Thread safety
    include pointers as a primitive data type
    flat file
    functional cohesion
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14197787.html
Copyright © 2011-2022 走看看