# Group by on Derived string field

**URL:** <https://discuss.elastic.co/t/group-by-on-derived-string-field/95940>\
**Category:** Elasticsearch\
**Created:** [August 4, 2017, 6:00pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940 "2017-08-04T18:00:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EuclideanSearch](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@EuclideanSearch](https://discuss.elastic.co/u/EuclideanSearch)\
**Post date:** [August 4, 2017, 6:00pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/1 "2017-08-04T18:00:07Z")

</div>

Hi. I need to take the first K characters of a string field (which is not analyzed) and aggregate on that. I know how to aggregate, but is it possible to aggregate on a derived field in ES?

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [August 4, 2017, 8:39pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/2 "2017-08-04T20:39:42Z")

</div>

Maybe you can use a script aggregation ?

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html)

What do you mean by :

```
a derived field

```

?

---

<div class="post-metadata">

**Author:** ![EuclideanSearch](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@EuclideanSearch](https://discuss.elastic.co/u/EuclideanSearch)\
**Post date:** [August 4, 2017, 8:45pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/3 "2017-08-04T20:45:56Z")

</div>

Hi. By 'derived field' I mean something like (using SQL) LEFT(my\_field, 10) as the\_value. In sql I would be doing:  
select the\_value, count(\*) from (  
select LEFT(my\_field, 10) as the\_value from table  
) aaa group by the\_value  
In this case the\_value is a 'derived' field.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [August 4, 2017, 8:52pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/4 "2017-08-04T20:52:37Z")

</div>

OK, in this case the script aggregation is possible. the code should be something like that:

```
"script": "my_field == null ? null : my_field.substring(0, (my_field.length() > 10 ? 10 : my_field.length()))"

```

(not tested 😉 )

---

<div class="post-metadata">

**Author:** ![EuclideanSearch](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@EuclideanSearch](https://discuss.elastic.co/u/EuclideanSearch)\
**Post date:** [August 4, 2017, 9:22pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/5 "2017-08-04T21:22:32Z")

</div>

Thank you xavierfacq!

---

<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:** [September 1, 2017, 9:22pm UTC](https://discuss.elastic.co/t/group-by-on-derived-string-field/95940/6 "2017-09-01T21:22:38Z")

</div>

Closed on September 1, 2017, 9:22pm UTC

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