# Top 10 IPs visiting a website

**URL:** https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133
**Category:** Elasticsearch
**Created:** [August 7, 2017, 3:41pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133 "2017-08-07T15:41:38Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![hding](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hding](https://discuss.elastic.co/u/hding)
#### Post date: [August 7, 2017, 3:41pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/1 "2017-08-07T15:41:38Z")

</div>

I have indexed some network logs. How do I search for the top 10 IP addresses that have visited a given website within a time range?

Do I use aggregations? Do I need to enable "doc\_values", and also set "index\_options" to at least "freqs"?

Thanks!

---

<div class="post-metadata">

### Author: ![Ranjith\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ranjith_m/32/19272_2.png) [@Ranjith\_M](https://discuss.elastic.co/u/Ranjith_M)
#### Post date: [August 7, 2017, 4:16pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/2 "2017-08-07T16:16:56Z")

</div>

If you can add more information about the challenges you are facing that will be easy to answer.  
But per my understanding you can use aggs and limit the size per your requirement.

Adding reference link from documentation,  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html)

---

<div class="post-metadata">

### Author: ![hding](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hding](https://discuss.elastic.co/u/hding)
#### Post date: [August 7, 2017, 4:51pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/3 "2017-08-07T16:51:35Z")

</div>

Hi Ranjith,

Thank you for replying.

For example, I have the following log lines:

```
[17/May/2015:09:40:34] 83.149.9.216 GET www.site1.com
[17/May/2015:09:45:16] 83.149.9.216 GET www.site1.com
[17/May/2015:10:05:04] 83.149.9.216 GET www.site1.com
[17/May/2015:10:06:02] 83.149.9.216 GET www.site222.com
[17/May/2015:10:07:35] 123.125.71.35 GET www.site1.com
[17/May/2015:10:08:08] 123.125.71.35 GET www.site1.com
[17/May/2015:10:09:14] 123.125.71.35 GET www.site1.com
[17/May/2015:10:09:24] 123.125.71.35 GET www.site333.com
[17/May/2015:10:10:01] 200.49.190.101 GET www.site1.com
[17/May/2015:10:10:26] 200.49.190.101 GET www.site1.com
[17/May/2015:11:10:43] 200.49.190.101 GET www.site1.com
[17/May/2015:11:12:23] 200.49.190.101 GET www.site1.com

```

Given **[www.site1.com](http://www.site1.com)** and a time range **[17/May/2015][10am-11am]**, 83.149.9.216 visited it **once** , 123.125.71.35 visited it **3 times** , 200.49.190.101 visited it **twice**.

If I want to know what are the top 2 IP addresses that visited **[www.site1.com](http://www.site1.com)** within time range **[17/May/2015][10am-11am]**, it should return something like this:

123.125.71.35 --- 3  
200.49.190.101 --- 2

How do I achieve this?

Thanks!

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [August 7, 2017, 4:55pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/4 "2017-08-07T16:55:34Z")

</div>

> [@hding](#):
>
> Do I use aggregations?

Yes, a `terms` aggregation.

> [@hding](#):
>
> Do I need to enable “doc\_values”

This should be on by default, so you should rather make sure to not disable them.

> [@hding](#):
>
> and also set “index\_options” to at least “freqs”

This is not necessary.

---

<div class="post-metadata">

### Author: ![hding](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hding](https://discuss.elastic.co/u/hding)
#### Post date: [August 7, 2017, 5:08pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/5 "2017-08-07T17:08:04Z")

</div>

Hi Adrien,

Thank you for replying.

For my example above, do I enable `doc_values` for the field of **83.149.9.216** , or the field of **[www.site1.com](http://www.site1.com)**? Our disk space is very limited, so we want to keep the index data size to its minimum.

Given a website name and time range, we first extract all matched log line, then sum by IP address, then do a sort on the sums. Does something like this work: `terms aggs` + `sort aggs`. But I imagine that the search query should be nested...

---

<div class="post-metadata">

### Author: ![hding](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@hding](https://discuss.elastic.co/u/hding)
#### Post date: [August 7, 2017, 5:25pm UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/6 "2017-08-07T17:25:33Z")

</div>

Will something like this work?

```auto
POST /_search
{
    "query": {
        "constant_score": {
            "filter": {
                "bool": {
                    "filter": [ 
                        "range": {
                            "log_time": {
                                "gte": 17/May/2015:10:00:00,
                                "lte": 17/May/2015:11:00:00
                            }
                        },
                        "terms": {
                            "site_name": "www.site1.com"
                        }
                    ]
                }
            },
        }
    },
    "aggs": {
        "top_ips_visiting_a_site": {
            "terms": {
                "field": "ip_address"
            },
            "aggs": {
                "top_ips_hits": {
                    "sum_hits" : { 
                        "sum" : { 
                            "field" : "ip_address" 
                        } 
                    },
                    "sort_hits": {
                        "sort": {
                            "date": {
                                "order": "desc"
                            }
                        }
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [August 8, 2017, 9:29am UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/7 "2017-08-08T09:29:02Z")

</div>

> [@hding](#):
>
> For my example above, do I enable doc\_values for the field of 83.149.9.216, or the field of [www.site1.com](http://www.site1.com)? Our disk space is very limited, so we want to keep the index data size to its minimum.

I would recommend buying more disk space. Disabling doc values does not save that much disk space, and will prevent you from aggregating this field in the future.

Your aggregation looks wrong, I believe you want to do something like this:

```auto
POST /_search
{
    "query": {
        "constant_score": {
            "filter": {
                "bool": {
                    "filter": [ 
                        "range": {
                            "log_time": {
                                "gte": 17/May/2015:10:00:00,
                                "lte": 17/May/2015:11:00:00
                            }
                        },
                        "terms": {
                            "site_name": "www.site1.com"
                        }
                    ]
                }
            },
        }
    },
    "aggs": {
        "top_ips_visiting_a_site": {
            "terms": {
                "field": "ip_address"
            },
            "aggs": {
                "top_ips_hits": {
                    "top_hits": {
                        "sort": {
                            "log_time": {
                                "order": "desc"
                            }
                        }
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 5, 2017, 9:29am UTC](https://discuss.elastic.co/t/top-10-ips-visiting-a-website/96133/8 "2017-09-05T09:29:05Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
