# Generating an aggregate field during search query

**URL:** <https://discuss.elastic.co/t/generating-an-aggregate-field-during-search-query/117371>\
**Category:** Elasticsearch\
**Created:** [January 28, 2018, 2:47pm UTC](https://discuss.elastic.co/t/generating-an-aggregate-field-during-search-query/117371 "2018-01-28T14:47:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyt](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lyt](https://discuss.elastic.co/u/lyt)\
**Post date:** [January 28, 2018, 2:47pm UTC](https://discuss.elastic.co/t/generating-an-aggregate-field-during-search-query/117371/1 "2018-01-28T14:47:05Z")

</div>

Hi all, new to ES here. I'm trying to implement a search engine from a source with the following schema in 1 index:

index:paper  
{  
"title": string,  
"author": string,  
"id": string,  
"references": [another\_paper.id, another\_paper.id, ...],  
"pubDate": date  
}

Let's say I want to perform a search for papers with the author "A. Smith" between the dates 2017-01-09 to 2017-01-30, and the number of times that each paper are referenced. How would I craft my search query to get results with a generated a field that says how many times each returned document is referenced by other documents under the "references" field? Is this even possible in ES? Execution speed is not important and I can tolerate relatively slow execution speed, but I do not want to update existing documents when I upload new documents.

Thank you

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [January 29, 2018, 8:36am UTC](https://discuss.elastic.co/t/generating-an-aggregate-field-during-search-query/117371/2 "2018-01-29T08:36:14Z")

</div>

Hey,

I think you want to aggregate on the `references`, but you want to use a cardinality aggregation in there to get the number of distinct references. See [https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-cardinality-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-cardinality-aggregation.html)

--Alex

---

<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:** [February 26, 2018, 8:36am UTC](https://discuss.elastic.co/t/generating-an-aggregate-field-during-search-query/117371/3 "2018-02-26T08:36:19Z")

</div>

Closed on February 26, 2018, 8:36am UTC

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