世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。-侯氏工坊
文章来源地址https://www.toymoban.com/news/detail-533061.html
ip_range
from elasticsearch import Elasticsearch
import urllib3
urllib3.disable_warnings()
# 注意ip地址的类型为ip
# PUT es_ip_range
# {
# "mappings": {
# "properties": {
# "my_ip": {"type": "ip"}
# }
# }
# }
# POST es_ip_range/_bulk
# {"index": {"_id": 1}}
# {"my_ip": "127.0.0.1"}
# {"index": {"_id": 2}}
# {"my_ip": "127.0.0.8"}
# {"index": {"_id": 3}}
# {"my_ip": "127.0.0.10"}
# {"index": {"_id": 4}}
# {"my_ip": "127.0.0.6"}
# {"index": {"_id": 5}}
# {"my_ip": "127.0.0.5"}
# {"index": {"_id": 6}}
# {"my_ip": "127.0.0.18"}
# GET es_ip_range/_search
# {
# "size": 0,
# "aggs": {
# "my_ip_histogram": {
# "ip_range": {
# "field": "my_ip",
# "keyed": true,
# "ranges": [
# {"to": "127.0.0.5", "key": "first"},
# {"from": "127.0.0.1", "to": "127.0.0.10", "key": "second"},
# {"from": "127.0.0.10", "key": "third"}
# ]
# }
# }
# }
# }
# 创建es实例
es = Elasticsearch("https://192.168.2.64:9200",
verify_certs=False,
basic_auth=("elastic", "MuZkDqdW--VsfDjTcoex"),
request_timeout=60,
max_retries=3,
retry_on_timeout=True,
node_selector_class="round_robin")
# 刷新
es.indices.refresh(index="es_ip_range")
ip_range = {
"my_ip_histogram": {
"ip_range": {
"field": "my_ip",
"keyed": True,
"ranges": [
{"to": "127.0.0.5", "key": "first"},
{"from": "127.0.0.1", "to": "127.0.0.10", "key": "second"},
{"from": "127.0.0.10", "key": "third"}
]
}
}
}
resp = es.search(index="es_ip_range", size=0, aggregations=ip_range)
print(resp['aggregations']['my_ip_histogram']['buckets'])
文章来源:https://www.toymoban.com/news/detail-533061.html
到了这里,关于10. python-es-8.3.3-IP范围聚合ip_range的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!