# Size of Field Cache built/loaded into memory with respect to query executed

**URL:** https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223
**Category:** Elasticsearch
**Created:** [January 3, 2013, 8:09pm UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223 "2013-01-03T20:09:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![revdev](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@revdev](https://discuss.elastic.co/u/revdev)
#### Post date: [January 3, 2013, 8:09pm UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/1 "2013-01-03T20:09:56Z")

</div>

Hi,  
I am trying to understand how and when field cache is built and loaded into  
memory.  
Does the field cache is built and loaded as soon as any single query is  
performed?

In the following queries, how much field cache is built and loaded into  
memory? In other words, does es loads a subset of field cache in memory  
depending upon what matches query or all at the same time?

1. Query with match\_all set and with no filters.
2. Query with match\_all set and with few filters.
3. Constant score Query with few filters. something like "query" : {  
"constant\_score" : { "filter" : { bunch of filters here} } }
4. Query which hits a subset of documents and few filters.

Thanks!  
Vinay

--

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [January 4, 2013, 7:45am UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/2 "2013-01-04T07:45:51Z")

</div>

Hello Vinay,

AFAIK, field caches are used when sorting or faceting. And none of the  
scenarios you mentioned seems to use that.

Instead, I see you have some filters there. Are you interested in filter  
caches?

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Thu, Jan 3, 2013 at 10:09 PM, revdev [clickingcam@gmail.com](mailto:clickingcam@gmail.com) wrote:

> Hi,  
> I am trying to understand how and when field cache is built and loaded  
> into memory.  
> Does the field cache is built and loaded as soon as any single query is  
> performed?
> 
> In the following queries, how much field cache is built and loaded into  
> memory? In other words, does es loads a subset of field cache in memory  
> depending upon what matches query or all at the same time?
> 
> 1. Query with match\_all set and with no filters.
> 2. Query with match\_all set and with few filters.
> 3. Constant score Query with few filters. something like "query" : {  
> "constant\_score" : { "filter" : { bunch of filters here} } }
> 4. Query which hits a subset of documents and few filters.
> 
> Thanks!  
> Vinay
> 
> --

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [January 4, 2013, 1:30pm UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/3 "2013-01-04T13:30:13Z")

</div>

Yes, field caches are loaded for sorting, faceting and when a script is  
accessing a document field using constructs such as doc['field\_name'].

On Friday, January 4, 2013 2:45:51 AM UTC-5, Radu Gheorghe wrote:

> Hello Vinay,
> 
> AFAIK, field caches are used when sorting or faceting. And none of the  
> scenarios you mentioned seems to use that.
> 
> Instead, I see you have some filters there. Are you interested in filter  
> caches?
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Thu, Jan 3, 2013 at 10:09 PM, revdev \<[click...@gmail.com](mailto:click...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi,  
> > I am trying to understand how and when field cache is built and loaded  
> > into memory.  
> > Does the field cache is built and loaded as soon as any single query is  
> > performed?
> > 
> > In the following queries, how much field cache is built and loaded into  
> > memory? In other words, does es loads a subset of field cache in memory  
> > depending upon what matches query or all at the same time?
> > 
> > 1. Query with match\_all set and with no filters.
> > 2. Query with match\_all set and with few filters.
> > 3. Constant score Query with few filters. something like "query" : {  
> > "constant\_score" : { "filter" : { bunch of filters here} } }
> > 4. Query which hits a subset of documents and few filters.
> > 
> > Thanks!  
> > Vinay
> > 
> > --

--

---

<div class="post-metadata">

### Author: ![revdev](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@revdev](https://discuss.elastic.co/u/revdev)
#### Post date: [January 4, 2013, 5:46pm UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/4 "2013-01-04T17:46:51Z")

</div>

Radu, Igor, Thanks for the reply.  
Sorry, I was not clear with my query (pun intended).  
In the scenarios I described, I am using those queries/filters to get  
facets on various fields.  
So, my question is about when is field cache built and does it depend upon  
the query or filters used? or is it built completely in one shot for all  
documents and all fields?

Secondly, to calculate facets I am using a constant score query with filter  
( "query" : { "constant\_score" : { "filter" : { bunch of filters here} }  
}). I am wondering if this approach is less/more efficient speed-wise as  
compared to using a mix of query and filter where query hits a subset of  
documents and filter is run on that subset.

Thanks again!  
Vinay

On Friday, January 4, 2013 5:30:13 AM UTC-8, Igor Motov wrote:

> Yes, field caches are loaded for sorting, faceting and when a script is  
> accessing a document field using constructs such as doc['field\_name'].
> 
> On Friday, January 4, 2013 2:45:51 AM UTC-5, Radu Gheorghe wrote:
> 
> > Hello Vinay,
> > 
> > AFAIK, field caches are used when sorting or faceting. And none of the  
> > scenarios you mentioned seems to use that.
> > 
> > Instead, I see you have some filters there. Are you interested in filter  
> > caches?
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Thu, Jan 3, 2013 at 10:09 PM, revdev [click...@gmail.com](mailto:click...@gmail.com) wrote:
> > 
> > > Hi,  
> > > I am trying to understand how and when field cache is built and loaded  
> > > into memory.  
> > > Does the field cache is built and loaded as soon as any single query is  
> > > performed?
> > > 
> > > In the following queries, how much field cache is built and loaded into  
> > > memory? In other words, does es loads a subset of field cache in memory  
> > > depending upon what matches query or all at the same time?
> > > 
> > > 1. Query with match\_all set and with no filters.
> > > 2. Query with match\_all set and with few filters.
> > > 3. Constant score Query with few filters. something like "query" : {  
> > > "constant\_score" : { "filter" : { bunch of filters here} } }
> > > 4. Query which hits a subset of documents and few filters.
> > > 
> > > Thanks!  
> > > Vinay
> > > 
> > > --

--

---

<div class="post-metadata">

### Author: ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)
#### Post date: [January 5, 2013, 3:53pm UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/5 "2013-01-05T15:53:32Z")

</div>

Hello Vinay,

On Fri, Jan 4, 2013 at 7:46 PM, revdev [clickingcam@gmail.com](mailto:clickingcam@gmail.com) wrote:

> Radu, Igor, Thanks for the reply.  
> Sorry, I was not clear with my query (pun intended).  
> In the scenarios I described, I am using those queries/filters to get  
> facets on various fields.  
> So, my question is about when is field cache built and does it depend upon  
> the query or filters used? or is it built completely in one shot for all  
> documents and all fields?

My understanding is that when ES does faceting, it loads the relevant  
fields (of the relevant documents) in the field cache. So if different  
queries return different sets of results - the load on your field cache  
should be different.

> Secondly, to calculate facets I am using a constant score query with  
> filter ( "query" : { "constant\_score" : { "filter" : { bunch of filters  
> here} } }). I am wondering if this approach is less/more efficient  
> speed-wise as compared to using a mix of query and filter where query hits  
> a subset of documents and filter is run on that subset.

Filters on their own benefit from filter caches, so they should be faster.  
But please note that facets are run on query results and ignore the  
filters. So if you need to account for filters when you run facets, you  
need to either wrap them in the constant\_score query or use facet\_filter.

As far as I know, facet filters are the faster of the two. But again, they  
would only apply to facets, not to search results. So if you want the same  
filter to be applied on both, you'd have to stick with constant\_score.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

--

---

<div class="post-metadata">

### Author: ![Vinay\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinay_2/32/2344_2.png) [@Vinay\_2](https://discuss.elastic.co/u/Vinay_2)
#### Post date: [January 6, 2013, 1:43am UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/6 "2013-01-06T01:43:51Z")

</div>

Thanks a lot Radu! That answers my questions!

On Sat, Jan 5, 2013 at 7:53 AM, Radu Gheorghe [radu.gheorghe@sematext.com](mailto:radu.gheorghe@sematext.com)wrote:

> constant\_score

--

---

<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: [July 6, 2017, 2:57am UTC](https://discuss.elastic.co/t/size-of-field-cache-built-loaded-into-memory-with-respect-to-query-executed/10223/7 "2017-07-06T02:57:45Z")

</div>


