# How does Elasticsearch use Lucene for aggregations?

**URL:** <https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319>\
**Category:** Elasticsearch\
**Created:** [September 21, 2017, 10:59am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319 "2017-09-21T10:59:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wilqor](https://avatars.discourse-cdn.com/v4/letter/w/9de0a6/32.png) [@wilqor](https://discuss.elastic.co/u/wilqor)\
**Post date:** [September 21, 2017, 10:59am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319/1 "2017-09-21T10:59:01Z")

</div>

Hello,

I could not find this information while reading either Elasticsearch Definitive Guide or Elasticsearch Reference, which led me to trying out the Community.

I would like to learn how does Elasticsearch perform aggregations - particularly calculating top N most frequently occurring terms - with the knowledge that its segments are Lucene indexes. Does it make use of Lucene Facets API when creating Lucene documents? What Lucene queries are performed by Elasticsearch when calculating aggregations?

I have a local Elasticsearch node running with debugger attached using IntelliJ, so any hints on where to look in the source code would be beneficial. All kind of explanation is also welcome.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 21, 2017, 3:47pm UTC](https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319/2 "2017-09-21T15:47:23Z")

</div>

Lucene does have join queries since version 4.x [1] and Elasticsearch  
abstracts the details of using those queries with the various joining  
queries such as Nested and Parent/Child.

Aggregations are done using custom code within Elasticsearch and does not  
use the Facets API. I believe that Solr, like Elasticsearch, also does not  
use the Facets API, but my knowledge of Solr is several versions old. I  
have not explored the aggregations code in detail, but I am assuming the  
Elasticsearch leverages the Lucene doc\_values/fielddata APIs and does not  
use queries to calculate the aggregations.

[1]  
[https://lucene.apache.org/core/6\_0\_0/join/org/apache/lucene/search/join/package-summary.html](https://lucene.apache.org/core/6_0_0/join/org/apache/lucene/search/join/package-summary.html)

---

<div class="post-metadata">

**Author:** ![wilqor](https://avatars.discourse-cdn.com/v4/letter/w/9de0a6/32.png) [@wilqor](https://discuss.elastic.co/u/wilqor)\
**Post date:** [September 22, 2017, 6:07am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319/3 "2017-09-22T06:07:03Z")

</div>

Thanks Ivan!

I have not heard yet of the Lucene join queries, which is an interesting topic for me to investigate on its own.  
From what I have recently found there are no taxonomy directories created for Lucene indexes, which sounds like a proof that Facets API is not used by Elastic.

---

<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:** [October 20, 2017, 6:07am UTC](https://discuss.elastic.co/t/how-does-elasticsearch-use-lucene-for-aggregations/101319/4 "2017-10-20T06:07:08Z")

</div>

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