Skip to content

Commit

Permalink
CONSOLEify ip-range bucket agg docs
Browse files Browse the repository at this point in the history
Related #18160
  • Loading branch information
polyfractal committed Aug 3, 2017
1 parent e7eda5e commit 829f7cb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 13 deletions.
40 changes: 39 additions & 1 deletion docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ apply plugin: 'elasticsearch.docs-test'
* only remove entries from this list. When it is empty we'll remove it
* entirely and have a party! There will be cake and everything.... */
buildRestTests.expectedUnconvertedCandidates = [
'reference/aggregations/bucket/iprange-aggregation.asciidoc',
'reference/aggregations/bucket/nested-aggregation.asciidoc',
'reference/aggregations/bucket/range-aggregation.asciidoc',
'reference/aggregations/bucket/reverse-nested-aggregation.asciidoc',
Expand Down Expand Up @@ -505,3 +504,42 @@ for (int i = 0; i < 100; i++) {
{"index":{}}
{"load_time": "$value"}"""
}

// Used by iprange agg
buildRestTests.setups['iprange'] = '''
- do:
indices.create:
index: ip_addresses
body:
settings:
number_of_shards: 1
number_of_replicas: 1
mappings:
data:
properties:
ip:
type: ip
- do:
bulk:
index: ip_addresses
type: data
refresh: true
body: |'''


for (int i = 0; i < 255; i++) {
buildRestTests.setups['iprange'] += """
{"index":{}}
{"ip": "10.0.0.$i"}"""
}
for (int i = 0; i < 5; i++) {
buildRestTests.setups['iprange'] += """
{"index":{}}
{"ip": "9.0.0.$i"}"""
buildRestTests.setups['iprange'] += """
{"index":{}}
{"ip": "11.0.0.$i"}"""
buildRestTests.setups['iprange'] += """
{"index":{}}
{"ip": "12.0.0.$i"}"""
}
46 changes: 34 additions & 12 deletions docs/reference/aggregations/bucket/iprange-aggregation.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Example:

[source,js]
--------------------------------------------------
GET /ip_addresses/data/_search
{
"size": 10,
"aggs" : {
"ip_ranges" : {
"ip_range" : {
Expand All @@ -21,6 +23,8 @@ Example:
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:iprange]

Response:

Expand All @@ -34,23 +38,26 @@ Response:
"buckets" : [
{
"to": "10.0.0.5",
"doc_count": 4
"doc_count": 10
},
{
"from": "10.0.0.5",
"doc_count": 6
"doc_count": 260
}
]
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]

IP ranges can also be defined as CIDR masks:

[source,js]
--------------------------------------------------
GET /ip_addresses/data/_search
{
"size": 0,
"aggs" : {
"ip_ranges" : {
"ip_range" : {
Expand All @@ -64,44 +71,51 @@ IP ranges can also be defined as CIDR masks:
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:iprange]

Response:

[source,js]
--------------------------------------------------
{
...
"aggregations": {
"ip_ranges": {
"buckets": [
{
"key": "10.0.0.0/25",
"from": "10.0.0.0",
"to": "10.0.0.127",
"doc_count": 127
"to": "10.0.0.128",
"doc_count": 128
},
{
"key": "10.0.0.127/25",
"from": "10.0.0.0",
"to": "10.0.0.127",
"doc_count": 127
"to": "10.0.0.128",
"doc_count": 128
}
]
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]

==== Keyed Response

Setting the `keyed` flag to `true` will associate a unique string key with each bucket and return the ranges as a hash rather than an array:

[source,js]
--------------------------------------------------
GET /ip_addresses/data/_search
{
"size": 0,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "remote_ip",
"field": "ip",
"ranges": [
{ "to" : "10.0.0.5" },
{ "from" : "10.0.0.5" }
Expand All @@ -112,6 +126,8 @@ Setting the `keyed` flag to `true` will associate a unique string key with each
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:iprange]

Response:

Expand All @@ -125,27 +141,30 @@ Response:
"buckets": {
"*-10.0.0.5": {
"to": "10.0.0.5",
"doc_count": 1462
"doc_count": 10
},
"10.0.0.5-*": {
"from": "10.0.0.5",
"doc_count": 50000
"doc_count": 260
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]

It is also possible to customize the key for each range:

[source,js]
--------------------------------------------------
GET /ip_addresses/data/_search
{
"size": 0,
"aggs": {
"ip_ranges": {
"ip_range": {
"field": "remote_ip",
"field": "ip",
"ranges": [
{ "key": "infinity", "to" : "10.0.0.5" },
{ "key": "and-beyond", "from" : "10.0.0.5" }
Expand All @@ -156,6 +175,8 @@ It is also possible to customize the key for each range:
}
}
--------------------------------------------------
// CONSOLE
// TEST[setup:iprange]

Response:

Expand All @@ -169,14 +190,15 @@ Response:
"buckets": {
"infinity": {
"to": "10.0.0.5",
"doc_count": 1462
"doc_count": 10
},
"and-beyond": {
"from": "10.0.0.5",
"doc_count": 50000
"doc_count": 260
}
}
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]

0 comments on commit 829f7cb

Please sign in to comment.