# Limiting the Field Cache with Filters on Documents

**URL:** https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701
**Category:** Elasticsearch
**Created:** [February 11, 2013, 8:24pm UTC](https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701 "2013-02-11T20:24:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)
#### Post date: [February 11, 2013, 8:24pm UTC](https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701/1 "2013-02-11T20:24:46Z")

</div>

I understand that when you facet or sort on a field, it loads all of the  
possible values of that field into the field cache. This can get huge, and  
is usually the cause of a lot of heap OOM errors, especially on fields with  
high cardinality. My question is, are the values of the field that are put  
into the field cache limited by the number of records returned by the  
query, or does ElasticSearch load all unique values of a field into the  
field cache regardless of the filters? If so, which filters limit them?  
Are there only certain kinds of queries that limit these as well? I read  
another post that said only the constant\_score query limits the records  
used in the cache.

Example:  
{  
"query": {  
"filtered": {  
"query": { ...QUERY A... },  
"filter": { ... FILTER A...}  
}  
},  
"filter": { ...FILTER B... },  
"facets": {  
"hugeFacet": {  
"terms": {  
"field": "fieldWithGBofUniqueValues"  
},  
"facet\_filter": { ...FILTER C... }  
}  
}  
}

I know that QUERY A, FILTER A, FILTER B, and FILTER C will limit the  
records that my hugeFacet will use when returning results. However:

1. Will all 4 of those also limit what is added into the field cache?  
If not, which ones will and won't? I believe I read somewhere that  
facet\_filters (FILTER C) limit what is put into the field cache, but it  
never said anything about these other filters.
2. Can any query be used in QUERY A to decrease the number of field  
values that are looked at and added into the field cache, or is "constant\_score"  
the only one that will? If that's the case then does that actually mean  
nothing in my entire "filtered" query will limit what's put into the field  
cache (QUERY A and FILTER A)?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discuss.elastic.co/u/Mike)
#### Post date: [February 13, 2013, 3:50pm UTC](https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701/2 "2013-02-13T15:50:58Z")

</div>

Fount the  
post: [https://groups.google.com/d/topic/elasticsearch/ArOHQIKiMKE/discussion](https://groups.google.com/d/topic/elasticsearch/ArOHQIKiMKE/discussion)

"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."

Maybe I misunderstood. Any clarification on my 2 questions above would be  
greatly appreciated.

On Monday, February 11, 2013 3:24:46 PM UTC-5, Mike wrote:

> I understand that when you facet or sort on a field, it loads all of the  
> possible values of that field into the field cache. This can get huge, and  
> is usually the cause of a lot of heap OOM errors, especially on fields with  
> high cardinality. My question is, are the values of the field that are put  
> into the field cache limited by the number of records returned by the  
> query, or does Elasticsearch load all unique values of a field into the  
> field cache regardless of the filters? If so, which filters limit them?  
> Are there only certain kinds of queries that limit these as well? I read  
> another post that said only the constant\_score query limits the records  
> used in the cache.
> 
> Example:  
> {  
> "query": {  
> "filtered": {  
> "query": { ...QUERY A... },  
> "filter": { ... FILTER A...}  
> }  
> },  
> "filter": { ...FILTER B... },  
> "facets": {  
> "hugeFacet": {  
> "terms": {  
> "field": "fieldWithGBofUniqueValues"  
> },  
> "facet\_filter": { ...FILTER C... }  
> }  
> }  
> }
> 
> I know that QUERY A, FILTER A, FILTER B, and FILTER C will limit the  
> records that my hugeFacet will use when returning results. However:
> 
> 1. Will all 4 of those also limit what is added into the field cache?  
> If not, which ones will and won't? I believe I read somewhere that  
> facet\_filters (FILTER C) limit what is put into the field cache, but it  
> never said anything about these other filters.
> 2. Can any query be used in QUERY A to decrease the number of field  
> values that are looked at and added into the field cache, or is "constant\_score"  
> the only one that will? If that's the case then does that actually mean  
> nothing in my entire "filtered" query will limit what's put into the field  
> cache (QUERY A and FILTER A)?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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: [February 17, 2013, 12:21pm UTC](https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701/3 "2013-02-17T12:21:18Z")

</div>

Hello Mike,

For sorting, Elasticsearch sorts all the documents matching your query and  
filters, then gives you the top X items back. Any document that's filtered  
out by either your query or your filter won't bother caches.

For faceting, filters don't matter. Facets are done on query results. If  
you need to filter the results on which facets are done, you need to use  
facet\_filter. In this case, caches are used by all the documents on which  
faceting is done (query results, minus what facet\_filter takes out).

## Best regards, Radu

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

On Wed, Feb 13, 2013 at 5:50 PM, Mike [mnilsson2323@gmail.com](mailto:mnilsson2323@gmail.com) wrote:

> Fount the post:  
> [https://groups.google.com/d/topic/elasticsearch/ArOHQIKiMKE/discussion](https://groups.google.com/d/topic/elasticsearch/ArOHQIKiMKE/discussion)
> 
> "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."
> 
> Maybe I misunderstood. Any clarification on my 2 questions above would be  
> greatly appreciated.
> 
> On Monday, February 11, 2013 3:24:46 PM UTC-5, Mike wrote:
> 
> > I understand that when you facet or sort on a field, it loads all of the  
> > possible values of that field into the field cache. This can get huge, and  
> > is usually the cause of a lot of heap OOM errors, especially on fields with  
> > high cardinality. My question is, are the values of the field that are put  
> > into the field cache limited by the number of records returned by the  
> > query, or does Elasticsearch load all unique values of a field into the  
> > field cache regardless of the filters? If so, which filters limit them?  
> > Are there only certain kinds of queries that limit these as well? I read  
> > another post that said only the constant\_score query limits the records  
> > used in the cache.
> > 
> > Example:  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": { ...QUERY A... },  
> > "filter": { ... FILTER A...}  
> > }  
> > },  
> > "filter": { ...FILTER B... },  
> > "facets": {  
> > "hugeFacet": {  
> > "terms": {  
> > "field": "fieldWithGBofUniqueValues"  
> > },  
> > "facet\_filter": { ...FILTER C... }  
> > }  
> > }  
> > }
> > 
> > I know that QUERY A, FILTER A, FILTER B, and FILTER C will limit the  
> > records that my hugeFacet will use when returning results. However:
> > 
> > 1. Will all 4 of those also limit what is added into the field cache?  
> > If not, which ones will and won't? I believe I read somewhere that  
> > facet\_filters (FILTER C) limit what is put into the field cache, but it  
> > never said anything about these other filters.
> > 2. Can any query be used in QUERY A to decrease the number of field  
> > values that are looked at and added into the field cache, or is "constant\_score"  
> > the only one that will? If that's the case then does that actually mean  
> > nothing in my entire "filtered" query will limit what's put into the field  
> > cache (QUERY A and FILTER A)?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:51am UTC](https://discuss.elastic.co/t/limiting-the-field-cache-with-filters-on-documents/10701/4 "2017-07-06T02:51:09Z")

</div>


