# Aggregate elasticsearch index by nested filed values

**URL:** <https://discuss.elastic.co/t/aggregate-elasticsearch-index-by-nested-filed-values/69292>\
**Category:** Elasticsearch\
**Created:** [December 16, 2016, 1:23pm UTC](https://discuss.elastic.co/t/aggregate-elasticsearch-index-by-nested-filed-values/69292 "2016-12-16T13:23:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hershs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hershs/32/10019_2.png) [@hershs](https://discuss.elastic.co/u/hershs)\
**Post date:** [December 16, 2016, 1:23pm UTC](https://discuss.elastic.co/t/aggregate-elasticsearch-index-by-nested-filed-values/69292/1 "2016-12-16T13:23:18Z")

</div>

I need to model web site with users and articles where each user can interact (read, open e.t.c) with any article many times. I want to do it one elasticsearch index by following nested mapping:

```
{
    "mappings": {
        "user": {
            "properties": {
                "user_id": {"type": "string"},
                "interactions": {
                    "type": "nested",
                    "properties": {
                        "article_id": {"type": "string"},
                        "interact_date": {"type": "date"}
                    }
                }
            }
        }
    }
}

```

example of indexed document:

```
{
    "user_id": 20,
    "interactions": [
        {"article_id": "111", "interact_date": "2015-01-01"},
        {"article_id": "111", "interact_date": "2015-01-02"},
        {"article_id": "222", "interact_date": "2015-01-01"}
     ]
}

```

I need to do the following aggregations on the data:

1. Total number of interactions per day. The following query looks OK for me:

2. Number of unique users interactions per day. If specific user interacted with several articles at same date range the user should be counted only once. In postgres it's simple query: for table with 3 columns [user\_id, article\_id, interact\_date]

3. How to add interactions by \_update without re-indexing whole document?

4. How to filter users by specific articles - count user once in aggregation by date only if he interacted with one of specified articles?

Thank you

---

<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:** [January 13, 2017, 1:23pm UTC](https://discuss.elastic.co/t/aggregate-elasticsearch-index-by-nested-filed-values/69292/2 "2017-01-13T13:23:18Z")

</div>

Closed on January 13, 2017, 1:23pm UTC

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