您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
elasticSearch桶聚合-ip范围与IP类型字段存储方式
发布时间:2021-09-03 15:45:58编辑:雪饮阅读()
桶聚合的ip范围查询需要依赖于字段类型为ip字段的文档。
那么首先就是要创建具有ip字段的文档,那么假定这些文档创建在schools索引下
那么schools索引下必须定义ip字段类型所映射到的我们文档中的ip字段。
那么创建ip字段映射如:
请求正文:
{
"properties": {
"name": {
"type": "text",
"fielddata": true
},
"ipAddr":{"type": "ip"}
}
}
响应正文:
{
"acknowledged": true
}
这里ipAddr就是接下来我们要创建的文档中的ip地址字段了。
那么接下来创建包含有ip地址的文档如:
请求正文:
{
"name":"Central School", "description":"ICSE", "street":"West End", "city":"Meerut",
"state":"UP", "zip":"250002", "location":[28.9926174, 77.692485], "fees":3500,"ipAddr":"192.168.0.20",
"tags":["fully computerized"], "rating":"4.5"
}
响应正文:
{
"_index": "schools",
"_type": "school",
"_id": "20",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 33,
"_primary_term": 3
}
为了实现ip范围桶聚合运算,所以我们可以创建一些用于测试的ip地址范围,比如这里我创建了192.168.0.10-192.168.0.20:
那么接下来就是ip范围的桶聚合运算了
请求正文:
{
"size": 10,
"aggs": {
"ipAddr_ranges": {
"ip_range": {
"field": "ipAddr",
"ranges": [
{ "to": "192.168.0.15" },
{ "from": "192.168.0.15" }
]
}
}
}
}
响应正文:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 20,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"ipAddr_ranges": {
"buckets": [
{
"key": "*-192.168.0.15",
"to": "192.168.0.15",
"doc_count": 5
},
{
"key": "192.168.0.15-*",
"from": "192.168.0.15",
"doc_count": 6
}
]
}
}
}
这里分别统计了从xxx.xxx.xxx.xxx-192.168.0.15范围的ip地址个数和192.168.0.15-xxx.xxx.xxx.xxx范围的ip地址个数。
关键字词:elasticSearch,桶聚合,ip范围,ip,存储