Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Server \ Elasticsearch 2.20 Beginners: aggregation     - Linux crontab commands and detailed usage examples (Linux)

- Use dump restore mode fast backup and recovery system FreeBSD (Linux)

- Linux initialization init system - UpStart (Linux)

- Stucts2 values on the page and how the attribute values corresponding to the Action (Programming)

- Linux file time Comments ctime mtime atime (Linux)

- Linux operating system log system (Linux)

- JavaScript function definition mode (Programming)

- jdbc Oracle database connection string writing pluggable (Database)

- Linux operating system Study Notes (Linux)

- Ora-00439: feature not enabled: managed standby (Database)

- Httpclient4.4 of principle (Http execution context) (Programming)

- Use SecureCRT to transfer files between Linux and Windows (Linux)

- Install Mac OS X 10.9 systems VMware10 (Linux)

- Use exp exported EXP-00091 error (Database)

- Linux system started to learn: Teaches you install Ubuntu 15.04 on VirtualBox (Linux)

- The Linux kernel and AVL tree in red-black tree (Programming)

- To install the mail client terminal Evolution 3.13.2 under Ubuntu 14.04 (Linux)

- Linux kernel update error, update-initramfs: failed Solution (Linux)

- Configuring the PXE server in Ubuntu 14.04 (Server)

- CentOS 6.6 compile and install phpMyAdmin configuration PostgreSQL9.4.1 (Database)

 
         
  Elasticsearch 2.20 Beginners: aggregation
     
  Add Date : 2018-11-21      
         
         
         
  Polymerization (Aggregations) provides the ability to group and statistical documents. Similar polymerization relational database group by grouping functions in Elasticsearch in which the primary aggregate query can get the specific results of the polymerization are polymerized Again, this is a very useful feature. You can get the results many times by one operation of the polymerization, thus avoiding repeated requests, to reduce the burden on the network and servers.

Polymerization (Aggregations) provides the ability to group and statistical documents. Similar polymerization relational database group by grouping functions in Elasticsearch in which the primary aggregate query can get the specific results of the polymerization are polymerized Again, this is a very useful feature. You can get the results many times by one operation of the polymerization, thus avoiding repeated requests, to reduce the burden on the network and servers.

Data Preparation: We insert a few data:

Request: POST localhost:9200/customer/external/?pretty

parameter:

{ "Name": "secisland", "age": 25, "state": "open", "gender": "woman", "balance": 87}

{ "Name": "zhangsan", "age": 32, "state": "close", "gender": "man", "balance": 95}

{ "Name": "zhangsan1", "age": 33, "state": "close", "gender": "man", "balance": 91}

{ "Name": "lisi", "age": 34, "state": "open", "gender": "woman", "balance": 99}

{ "Name": "wangwu", "age": 46, "state": "close", "gender": "woman", "balance": 78}

Wherein the insert 5 as the test data.

Once we have data aggregation test:

Example: all customers grouped by status, then return to the top 10 (the default), press statistics (also the default) Sort:

Request: POST http: // localhost: 9200 / customer / _search pretty?

parameter:

{
  "Size": 0,
  "Aggs": {
    "Group_by_state": {
      "Terms": {
        "Field": "state"
      }
    }
  }
}

This condition is similar to the query in a relational database group by:
SELECT state, COUNT (*) FROM customer GROUP BY state ORDER BY COUNT (*) DESC

Return:

  {
  "Took": 1,
  "Timed_out": false,
  "_shards": {
    "Total": 5,
    "Successful": 5,
    "Failed": 0
  },
  "Hits": {
    "Total": 5,
    "Max_score": 0.0,
    "Hits": []
  },
  "Aggregations": {
    "Group_by_state": {
      "Doc_count_error_upper_bound": 0,
      "Sum_other_doc_count": 0,
      "Buckets": [{
        "Key": "close",
        "Doc_count": 3
      }, {
        "Key": "open",
        "Doc_count": 2
      }]
    }
  }
}

We can be seen, there are two close states customers, users two open states.

Here we are again on the basis of the above increase is a function of the state, while the statistical average balance is calculated for each state.

Request and just the same, but the parameters have changed, consider the following parameters:

{
  "Size": 0,
  "Aggs": {
    "Group_by_state": {
      "Terms": {
        "Field": "state"
      },
      "Aggs": {
        "Average_balance": {
          "Avg": {
            "Field": "balance"
          }
        }
      }
    }
  }
}

Results obtained are as follows:

{
  "Took": 16,
  "Timed_out": false,
  "_shards": {
    "Total": 5,
    "Successful": 5,
    "Failed": 0
  },
  "Hits": {
    "Total": 5,
    "Max_score": 0.0,
    "Hits": []
  },
  "Aggregations": {
    "Group_by_state": {
      "Doc_count_error_upper_bound": 0,
      "Sum_other_doc_count": 0,
      "Buckets": [{
        "Key": "close",
        "Doc_count": 3,
        "Average_balance": {
          "Value": 88.0
        }
      }, {
        "Key": "open",
        "Doc_count": 2,
        "Average_balance": {
          "Value": 93.0
        }
      }]
    }
  }
}

Look carefully at how the nested average_balance gathered in group_by_state ACCUMULATION. This is a common pattern polymerization. Any field can be aggregated to get the results we want again after polymerization.

Look at the following example, we obtain the results of the above-average amount of accounts again in descending order:

Request the same as before:

parameter:

{
  "Size": 0,
  "Aggs": {
    "Group_by_state": {
      "Terms": {
        "Field": "state",
        "Order": {
          "Average_balance": "desc"
        }
      },
      "Aggs": {
        "Average_balance": {
          "Avg": {
            "Field": "balance"
          }
        }
      }
    }
  }
}

Results obtained:

{
  "Took": 1,
  "Timed_out": false,
  "_shards": {
    "Total": 5,
    "Successful": 5,
    "Failed": 0
  },
  "Hits": {
    "Total": 5,
    "Max_score": 0.0,
    "Hits": []
  },
  "Aggregations": {
    "Group_by_state": {
      "Doc_count_error_upper_bound": 0,
      "Sum_other_doc_count": 0,
      "Buckets": [{
        "Key": "open",
        "Doc_count": 2,
        "Average_balance": {
          "Value": 93.0
        }
      }, {
        "Key": "close",
        "Doc_count": 3,
        "Average_balance": {
          "Value": 88.0
        }
      }]
    }
  }
}

This article by the Mosaic Rand (secisland) original, reproduced, please indicate the author and source.

The following example is more complex: demonstrates how age group (ages 20-29, 30-39, 40-49), and then through sex, and finally get every age group, the average account balance each sex:

{
  "Size": 0,
  "Aggs": {
    "Group_by_age": {
      "Range": {
        "Field": "age",
        "Ranges": [
          {
            "From": 20,
            "To": 30
          },
          {
            "From": 30,
            "To": 40
          },
          {
            "From": 40,
            "To": 50
          }
        ]
      },
      "Aggs": {
        "Group_by_gender": {
          "Terms": {
            "Field": "gender"
          },
          "Aggs": {
            "Average_balance": {
              "Avg": {
                "Field": "balance"
              }
            }
          }
        }
      }
    }
  }
}

Check out the return:

{
  "Took": 15,
  "Timed_out": false,
  "_shards": {
    "Total": 5,
    "Successful": 5,
    "Failed": 0
  },
  "Hits": {
    "Total": 5,
    "Max_score": 0.0,
    "Hits": []
  },
  "Aggregations": {
    "Group_by_age": {
      "Buckets": [{
        "Key": "20.0-30.0",
        "From": 20.0,
        "From_as_string": "20.0",
        "To": 30.0,
        "To_as_string": "30.0",
        "Doc_count": 1,
        "Group_by_gender": {
          "Doc_count_error_upper_bound": 0,
          "Sum_other_doc_count": 0,
          "Buckets": [{
            "Key": "woman",
            "Doc_count": 1,
            "Average_balance": {
              "Value": 87.0
            }
          }]
        }
      }, {
        "Key": "30.0-40.0",
        "From": 30.0,
        "From_as_string": "30.0",
        "To": 40.0,
        "To_as_string": "40.0",
        "Doc_count": 3,
        "Group_by_gender": {
          "Doc_count_error_upper_bound": 0,
          "Sum_other_doc_count": 0,
          "Buckets": [{
            "Key": "man",
            "Doc_count": 2,
            "Average_balance": {
              "Value": 93.0
            }
          }, {
            "Key": "woman",
            "Doc_count": 1,
            "Average_balance": {
              "Value": 99.0
            }
          }]
        }
      }, {
        "Key": "40.0-50.0",
        "From": 40.0,
        "From_as_string": "40.0",
        "To": 50.0,
        "To_as_string": "50.0",
        "Doc_count": 1,
        "Group_by_gender": {
          "Doc_count_error_upper_bound": 0,
          "Sum_other_doc_count": 0,
          "Buckets": [{
            "Key": "woman",
            "Doc_count": 1,
            "Average_balance": {
              "Value": 78.0
            }
          }]
        }
      }]
    }
  }
}

As can be seen from the above examples, Elasticsearch aggregation capability is very powerful.
     
         
         
         
  More:      
 
- Stunning exclamation point at the Linux command line (Linux)
- Depth understanding of JavaScript new mechanism (Programming)
- ASP.NET 5 (vNext) Linux deployment (Server)
- Java precision four operations (Programming)
- Debian 7.8 system installation and configuration process (Linux)
- Installation under Linux to deploy Java (Linux)
- 10 practical Java programming technology (Programming)
- Java-- get the reflection object information (Programming)
- HTML5 Application Cache (Programming)
- How to use Aptik to backup and restore Apps/PPAs under ubuntu (Linux)
- MySQL High Availability plan several options (Database)
- Linux cut Command Study Notes (Linux)
- Memcached source installation and configuration under CentOS 6.6 (Server)
- Servlet 3.0 interfaces of AsyncListener (Programming)
- CentOS 7 source code to compile and install PHP5.6 Nginx1.7.9 and MySQL (LNMP build environment) (Server)
- Sort search algorithm Java - application examples with recursive dichotomy (Programming)
- Understand the profound discussion of some of the options for wireless encryption (Linux)
- Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)
- Linux firewall settings instance (Linux)
- The temporary operation of the document mkstemp on Linux (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.