# Different results with fields vs script\_field using terms facet

**URL:** <https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409>\
**Category:** Elasticsearch\
**Created:** [November 14, 2013, 3:52pm UTC](https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409 "2013-11-14T15:52:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Hamed](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@Adam\_Hamed](https://discuss.elastic.co/u/Adam_Hamed)\
**Post date:** [November 14, 2013, 3:52pm UTC](https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409/1 "2013-11-14T15:52:34Z")

</div>

Shouldn't these two queries return the same result? That doesn't seem to  
be the case. The first query returns the correct result. But the second  
one does not. It seems that the second query does not properly handle the  
fact that there are multiple instances. The sum of counts from the first  
query correctly exceed the number of documents since there are multiple  
instances in each document. However, it seems the sum of counts in the  
second query equals the total number of documents so it isn't "flattening"  
out the instances.

Our documents have a map of entities, each entity has an array of  
instances, each instance has an attributes object.

Is this the intended behavior?

Thanks,  
Adam

POST /\_search  
{  
"size": 0,  
"facets": {  
"results": {  
"terms": {  
"field": "entities.B.instances.attributes.name"  
}  
}  
}  
}

{  
"size": 0,  
"facets": {  
"results": {  
"terms": {  
"script\_field":  
"doc['entities.B.instances.attributes.name'].value"  
}  
}  
}  
}

Build:  
1.0.0.Beta2-SNAPSHOT

--  
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:** ![Adam\_Hamed](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@Adam\_Hamed](https://discuss.elastic.co/u/Adam_Hamed)\
**Post date:** [November 18, 2013, 3:54pm UTC](https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409/2 "2013-11-18T15:54:16Z")

</div>

Anybody else see this? This is with 1.0.0.Beta2-SNAPSHOT so could be a  
new bug...

On Thursday, November 14, 2013 9:52:34 AM UTC-6, Adam H wrote:

> Shouldn't these two queries return the same result? That doesn't seem to  
> be the case. The first query returns the correct result. But the second  
> one does not. It seems that the second query does not properly handle the  
> fact that there are multiple instances. The sum of counts from the first  
> query correctly exceed the number of documents since there are multiple  
> instances in each document. However, it seems the sum of counts in the  
> second query equals the total number of documents so it isn't "flattening"  
> out the instances.
> 
> Our documents have a map of entities, each entity has an array of  
> instances, each instance has an attributes object.
> 
> Is this the intended behavior?
> 
> Thanks,  
> Adam
> 
> POST /\_search  
> {  
> "size": 0,  
> "facets": {  
> "results": {  
> "terms": {  
> "field": "entities.B.instances.attributes.name"  
> }  
> }  
> }  
> }
> 
> {  
> "size": 0,  
> "facets": {  
> "results": {  
> "terms": {  
> "script\_field": "doc['entities.B.instances.attributes.name  
> '].value"  
> }  
> }  
> }  
> }
> 
> Build:  
> 1.0.0.Beta2-SNAPSHOT

--  
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:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [November 18, 2013, 10:40pm UTC](https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409/3 "2013-11-18T22:40:19Z")

</div>

Hi Adam

The doc.value always returns the first value of a multi-valued field  
: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_document_fields)

If you use the doc.values which returns an array of all values, they  
should be identical.

Cheers,  
Boaz

On Monday, November 18, 2013 4:54:16 PM UTC+1, Adam H wrote:

> Anybody else see this? This is with 1.0.0.Beta2-SNAPSHOT so could be a  
> new bug...
> 
> On Thursday, November 14, 2013 9:52:34 AM UTC-6, Adam H wrote:
> 
> > Shouldn't these two queries return the same result? That doesn't seem to  
> > be the case. The first query returns the correct result. But the second  
> > one does not. It seems that the second query does not properly handle the  
> > fact that there are multiple instances. The sum of counts from the first  
> > query correctly exceed the number of documents since there are multiple  
> > instances in each document. However, it seems the sum of counts in the  
> > second query equals the total number of documents so it isn't "flattening"  
> > out the instances.
> > 
> > Our documents have a map of entities, each entity has an array of  
> > instances, each instance has an attributes object.
> > 
> > Is this the intended behavior?
> > 
> > Thanks,  
> > Adam
> > 
> > POST /\_search  
> > {  
> > "size": 0,  
> > "facets": {  
> > "results": {  
> > "terms": {  
> > "field": "entities.B.instances.attributes.name"  
> > }  
> > }  
> > }  
> > }
> > 
> > {  
> > "size": 0,  
> > "facets": {  
> > "results": {  
> > "terms": {  
> > "script\_field": "doc['entities.B.instances.attributes.name  
> > '].value"  
> > }  
> > }  
> > }  
> > }
> > 
> > Build:  
> > 1.0.0.Beta2-SNAPSHOT

--  
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:06am UTC](https://discuss.elastic.co/t/different-results-with-fields-vs-script-field-using-terms-facet/14409/4 "2017-07-06T02:06:22Z")

</div>


