ElasticSearch- polymerization bucket learning

DELETE cars
PUT cars
{
  "mappings": {
    "transactions": {
      "properties": {
        "price": {
          "type":"long"
        },
        "color": {
          "type":"keyword"
        },
        "make": {
          "type":"keyword"
        },
        "sold": {
          "type":"date"
        }
      }
    }
  }
}
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

----------Filter Aggregation-------

The number of red cars

POST /cars/transactions/_search?size=0
{
    "aggs" : {
        "red_cars" : {
            "filter" : { "term": { "color": "red" } }
        }
    }
}

----------Filters Aggregation-------

Statistical red car, blue car each number

POST /cars/transactions/_search
{
  "size": 0,
  "aggs" : {
    "cars" : {
      "filters" : {
        "filters" : {
          "red_cars" :   { "match" : { "color" : "red"   }},
          "blue_cars" : { "match" : { "color" : "blue" }}
        }
      }
    }
  }
}

Statistical red car, blue car each number, and the average price of the car in two colors

POST /cars/transactions/_search
{
  "size": 0,
  "aggs" : {
    "cars" : {
      "filters" : {
        "filters" : {
          "red_cars" :   { "match" : { "color" : "red"   }},
          "blue_cars" : { "match" : { "color" : "blue" }}
        }
      },
      "aggs" : {
          "avg_price" : { "avg" : { "field" : "price" } }
      }
    }
  }
}

----------Date Histogram Aggregation-------

How many monthly car sales

interval parameters: year, Quarter, month The, Week, Day, hour, minute, SECOND,
interval parameters: You can also write a specific time, such as 24h, 90m

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "sales_over_time": {
         "date_histogram": {
            "field": "sold",
            "interval": "month", 
            "format": "yyyy-MM-dd" 
         }
      }
   }
}

Specified period of 90 minutes

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "sales_over_time": {
         "date_histogram": {
            "field": "sold",
            "interval": "90m", 
            "format": "yyyy-MM-dd HH:mm:ss" 
         }
      }
   }
}

Add keyed parameters to return buckets not returned as an array

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs": {
      "sales_over_time": {
         "date_histogram": {
            "field": "sold",
            "interval": "month", 
            "format": "yyyy-MM-dd",
            "keyed":true
         }
      }
   }
}

----------Date Range Aggregation-------

According to statistics the number of vehicle sale date range

GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "range": {
      "date_range": {
        "field": "sold",
        "format": "yyyy-MM-dd",
        "ranges": [
          {"from": "now-36M/M"},
          {"to": "now-24M/M"},
          {"from": "now-36M/M","to": "now-12M/M"}
        ]
      }
    }
  }
}

Add keyed parameters to return buckets not returned as an array, and the specified key value

GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "range": {
      "date_range": {
        "field": "sold",
        "format": "yyyy-MM-dd",
        "ranges": [
          {"from": "now-36M/M","key":"36months"},
          {"to": "now-24M/M","key":"2years_ago"},
          {"from": "now-36M/M","to": "now-12M/M"}
        ],
        "keyed":true
      }
    }
  }
}

According to statistics the number of vehicle sale date range, and the average selling price in the period

GET /cars/transactions/_search
{
  "size": 0,
  "aggs": {
    "range": {
      "date_range": {
        "field": "sold",
        "format": "yyyy-MM-dd",
        "ranges": [
          {"from": "now-36M/M","key":"36months"},
          {"to": "now-24M/M","key":"2years_ago"},
          {"from": "now-36M/M","to": "now-12M/M"}
        ],
        "keyed":true
      },
      "aggs" : {
          "avg_price" : { "avg" : { "field" : "price" } }
      }
    }
  }
}

----------Histogram Aggregation-------

Histogram, 20,000 were in accordance with sub-section barrels

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "histogram" : {
              "field" : "price",
              "interval": 20000
            }
        }
    }
}

min_doc_count parameters, limiting it to display at least a few barrel

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "histogram" : {
              "field" : "price",
              "interval": 20000,
              "min_doc_count": 1
            }
        }
    }
}

extended_bounds parameters, extended display range

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "histogram" : {
              "field" : "price",
              "interval": 20000,
              "extended_bounds": {
                    "min" : 0,
                    "max" : 200000
              }
            }
        }
    }
}

Increase Sort, in descending order according to the name of a barrel

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "histogram" : {
              "field" : "price",
              "interval": 20000,
              "order" : { "_key" : "desc" }
            }
        }
    }
}

Increase Sort, sorted by the number of statistics

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "histogram" : {
              "field" : "price",
              "interval": 20000,
              "order" : { "_count" : "desc" }
            }
        }
    }
}

Histogram, according to 20,000 barrels is divided into sections, and aggregated

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs":{
      "price":{
         "histogram":{ 
            "field": "price",
            "interval": 20000
         },
         "aggs":{
            "price_sum": {
               "sum": { 
                 "field" : "price"
               }
             }
         }
      }
   }
}

Histogram, according to 20,000 barrels is divided into sections, and aggregated

Are sorted in the sub-index polymeric

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs":{
      "price":{
         "histogram":{ 
            "field": "price",
            "interval": 20000,
            "order":{ "price_sum.value" : "desc" }
         },
         "aggs":{
            "price_sum": {
               "sum": { 
                 "field" : "price"
               }
             }
         }
      }
   }
}

Increase keyed parameters

GET /cars/transactions/_search
{
   "size" : 0,
   "aggs":{
      "price":{
         "histogram":{ 
            "field": "price",
            "interval": 20000,
            "order":{ "price_sum.value" : "desc" }
            ,"keyed":true
         },
         "aggs":{
            "price_sum": {
               "sum": { 
                 "field" : "price"
               }
             }
         }
      }
   }
}

----------Terms Aggregation-------

It is divided barrel according to terms of a field

To get the most amount of the first few entries on each tile, then the overall secondary rearrangement, so there may be errors

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make"
            }
        }
    }
}

Entries sorted alphabetically

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make",
                "order" : { "_term" : "asc" }
            }
        }
    }
}

min_doc_count: used to limit the number of times only to extract appeared many times greater than the entry

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make",
                "min_doc_count": 3
            }
        }
    }
}

Using a script, modify the field content

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "script" : {
                    "inline": "'make:'+doc['make'].value",
                    "lang": "painless"
                }
            }
        }
    }
}

Ditto

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make",
                "script" : {
                    "inline" : "'make: ' +_value",
                    "lang" : "painless"
                }
            }
        }
    }
}

Using regular expressions filter entry

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make",
                "include" : ".*o.*",
                "exclude" : "f.*"
            }
        }
    }
}

Use precisely specified entry is divided barrel

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "make_terms" : {
            "terms" : {
                "field" : "make",
                 "include" : ["mazda", "honda"]
            }
        }
    }
}

----------Range Aggregation-------

The specified range of segment points tub, and counting the number

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 20000 },
                    { "from" : 20000, "to" : 50000 },
                    { "from" : 50000 }
                ]
            }
        }
    }
}

The script is used to specify field

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "script" : {
                    "lang": "painless",
                    "inline": "doc['price'].value"
                },
                "ranges" : [
                    { "to" : 20000 },
                    { "from" : 20000, "to" : 50000 },
                    { "from" : 50000 }
                ]
            }
        }
    }
}

In the minutes before the barrel, by changing the value of the script

GET /cars/transactions/_search
{
    "size": 0, 
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" :"price",
                "script" : {
                    "lang": "painless",
                    "inline": "_value * params.rate",
                    "params" : {
                        "rate" : 2.5
                    }
                },
                "ranges" : [
                    { "to" : 20000 },
                    { "from" : 20000, "to" : 50000 },
                    { "from" : 50000 }
                ]
            }
        }
    }
}

----------Global Aggregation-------

Calculated using global all documents

GET /cars/transactions/_search?size=0
{
    "query" : {
        "match" : { "make" : "honda" }
    },
    "aggs" : {
        "all_makes" : {
            "global" : {}, 
            "aggs" : { 
                "avg_price" : { "avg" : { "field" : "price" } }
            }
        },
        "honda_make": { "avg" : { "field" : "price" } }
    }
}

Verify global calculation is correct

GET /cars/transactions/_search?size=0
{
    "query" : {
        "match_all" : { }
    },
    "aggs" : {
        "all_make": { "avg" : { "field" : "price" } }
    }
}

----------IP Range Aggregation-------

DELETE ips
PUT ips
{
  "mappings": {
    "transactions": {
      "properties": {
        "ip": {
          "type":"ip"
        }
      }
    }
  }
}
POST /ips/doc/_bulk
{ "index": {}}
{ "ip" : "192.168.1.1"}
{ "index": {}}
{ "ip" : "192.168.1.10"}
{ "index": {}}
{ "ip" : "192.168.1.102"}
{ "index": {}}
{ "ip" : "192.168.1.150"}
{ "index": {}}
{ "ip" : "192.168.1.160"}
{ "index": {}}
{ "ip" : "192.168.1.250"}

Divided barrel according to the specified ip range and quantity statistics

GET /ips/doc/_search
{
    "size": 0, 
    "aggs" : {
        "ip_ranges" : {
            "ip_range" : {
                "field" : "ip",
                "ranges" : [
                    {"from" : "192.168.1.1" },
                    {"to" : "192.168.2.1" },
                    {"from" : "192.168.1.1","to" : "192.168.3.200" }
                ]
            }
        }
    }
}

Sub-barrel range by the subnet mask

192.168.1.0/24 represents: 192.168.1.1 to 192.168.1.254

192.168.2.0/25:192.168.2.1 to 192.168.2.126

GET /ips/doc/_search
{
    "size": 0, 
    "aggs" : {
        "ip_ranges" : {
            "ip_range" : {
                "field" : "ip",
                "ranges" : [
                    { "mask" : "192.168.1.0/24" },
                    { "mask" : "192.168.2.0/25" }
                ]
            }
        }
    }
}

Join keyed parameters

GET /ips/doc/_search
{
    "size": 0, 
    "aggs" : {
        "ip_ranges" : {
            "ip_range" : {
                "field" : "ip",
                "ranges" : [
                    { "mask" : "192.168.1.0/24" },
                    { "mask" : "192.168.2.0/25" }
                ],
                "keyed": true
            }
        }
    }
}

----------Geo Distance Aggregation-------

DELETE /museums
PUT /museums
{
    "mappings": {
        "doc": {
            "properties": {
                "location": {
                    "type": "geo_point"
                }
            }
        }
    }
}
POST /museums/doc/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}

Specify the coordinate points how many points the barrel of a document within a distance range, default units: m (meter)

POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ]
            }
        }
    }
}

Designated units to kilometers

Can be used: mi (miles miles), in (inches inches), yd (yards codescale), km (kilometers), cm (centimeters), mm (millimeters).

POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "unit" : "km",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ]
            }
        }
    }
}

Specifies the distance mode

distance_type: arc curvature (default, high precision, accurate calculation), Plane (better performance, faster, but somewhat less accuracy)

POST /museums/_search?size=0
{
    "aggs" : {
        "rings" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "unit" : "km",
                "distance_type" : "plane",
                "ranges" : [
                    { "to" : 100 },
                    { "from" : 100, "to" : 300 },
                    { "from" : 300 }
                ]
            }
        }
    }
}

Use keyed

POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ],
                "keyed": true
            }
        }
    }
}


Author: One point zero
link: https: //www.jianshu.com/p/f79309adb63b
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/cdchencw/p/12470746.html