# Can I use facet on script field?

**URL:** <https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533>\
**Category:** Elasticsearch\
**Created:** [January 30, 2012, 12:36am UTC](https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533 "2012-01-30T00:36:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivier](https://avatars.discourse-cdn.com/v4/letter/o/d2c977/32.png) [@Olivier](https://discuss.elastic.co/u/Olivier)\
**Post date:** [January 30, 2012, 12:36am UTC](https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533/1 "2012-01-30T00:36:15Z")

</div>

Hi,

Can I use facet on script field ?  
For instance, I would like to dynamically extract some field with  
regex and use it to specify field facet :  
{  
"query": {  
"match\_all": {}  
},  
"script\_fields": {  
"test1": {  
"lang": "groovy",  
"script": "(doc['some\_field'].value =~ '([a-z]).\*')[0][1]"  
}  
},  
"facets": {  
"tag": {  
"terms": {  
"field": "test1",  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 1, 2012, 9:01am UTC](https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533/2 "2012-02-01T09:01:50Z")

</div>

Thats a script field which controls what will be returned from the actual search top N hits you ask for (the size). Each facet specifically has some support for scripting, terms as well.

On Monday, January 30, 2012 at 2:36 AM, Olivier wrote:

> Hi,
> 
> Can I use facet on script field ?  
> For instance, I would like to dynamically extract some field with  
> regex and use it to specify field facet :  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "script\_fields": {  
> "test1": {  
> "lang": "groovy",  
> "script": "(doc['some\_field'].value =~ '([a-z]).\*')[0][1]"  
> }  
> },  
> "facets": {  
> "tag": {  
> "terms": {  
> "field": "test1",  
> }  
> }  
> }  
> }

---

<div class="post-metadata">

**Author:** ![Olivier](https://avatars.discourse-cdn.com/v4/letter/o/d2c977/32.png) [@Olivier](https://discuss.elastic.co/u/Olivier)\
**Post date:** [February 1, 2012, 2:42pm UTC](https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533/3 "2012-02-01T14:42:15Z")

</div>

Missed it.  
Thanks Shay

On 1 fév, 10:01, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Thats a script field which controls what will be returned from the actual search top N hits you ask for (the size). Each facet specifically has some support for scripting, terms as well.
> 
> On Monday, January 30, 2012 at 2:36 AM, Olivier wrote:
> 
> > Hi,
> 
> > Can I use facet on script field ?  
> > For instance, I would like to dynamically extract some field with  
> > regex and use it to specify field facet :  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "script\_fields": {  
> > "test1": {  
> > "lang": "groovy",  
> > "script": "(doc['some\_field'].value =~ '([a-z]).\*')[0][1]"  
> > }  
> > },  
> > "facets": {  
> > "tag": {  
> > "terms": {  
> > "field": "test1",  
> > }  
> > }  
> > }  
> > }

---

<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, 3:40am UTC](https://discuss.elastic.co/t/can-i-use-facet-on-script-field/6533/4 "2017-07-06T03:40:47Z")

</div>


