# Aggregation on substring of a specific field

**URL:** <https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412>\
**Category:** Elasticsearch\
**Created:** [April 7, 2021, 7:03am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412 "2021-04-07T07:03:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![111457](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111457/32/86675_2.png) [@111457](https://discuss.elastic.co/u/111457)\
**Post date:** [April 7, 2021, 7:03am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/1 "2021-04-07T07:03:45Z")

</div>

Hi. I'm trying to aggregate on part of strings of a given field.

For example, I have 5 documents in an index "Petroglyph".  
Each documents includes a field called "Site".  
In "Site", input values follow pattern like 'site1\<site2\<site3\<site4',

```auto
doc1 - "Site": "Asia<South Korea<Ulsan<Bangudae"
doc2 - "Site": "Europe<France<Dordogne<Grotte de Cussac"
doc3 - "Site": "Europe<France<Dordogne<Lascaux"
doc4 - "Site": "Asia<South Korea<Ulsan<Cheon-Jeon-Ri"
doc5 - "Site": "Africa<Namibia<Kunene<Twyfelfontein"

```

and I wish to do aggregation on 'site2\<site3' regarding its order, which should look like:

```auto
buckets: [
  {
    key: 'South Korea<Ulsan',
    doc_count: 2
  },
  {
    key: 'France<Dordogne',
    doc_count: 2
  },
  {
    key: 'Namibia<Kunene',
    doc_count: 1
  }
]

```

I have read and tried [node.js - Elasticsearch aggregation on part of string, not full string - Stack Overflow](https://stackoverflow.com/questions/30948805/elasticsearch-aggregation-on-part-of-string-not-full-string#) that seems very similar to my question, however, it did not work which I believe it was written in consideration of version less than 7.0.

Also, I have given a thought of splitting value of Site with '\<', save the split result in an array, and aggregate on result 'array[1]\<array[2]'.  
But it seems to be creating complexity and not elasticsearch-likely according to my limited experience.

What would be the way to solve this problem?

Thank you.

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [April 7, 2021, 7:51am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/2 "2021-04-07T07:51:59Z")

</div>

You could create a runtime field that is doing the substring calculation and use the result field for your aggregation.

---

<div class="post-metadata">

**Author:** ![111457](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111457/32/86675_2.png) [@111457](https://discuss.elastic.co/u/111457)\
**Post date:** [April 8, 2021, 5:11am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/3 "2021-04-08T05:11:31Z")

</div>

Actually I couldn't make your suggestion of using runtime field work due to the elasticsearch version difference. But thank you for the reply.

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [April 8, 2021, 6:38am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/4 "2021-04-08T06:38:52Z")

</div>

Okay in older versions you have to create the new field on Index time. Which version are you running on?

---

<div class="post-metadata">

**Author:** ![111457](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111457/32/86675_2.png) [@111457](https://discuss.elastic.co/u/111457)\
**Post date:** [April 8, 2021, 6:49am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/5 "2021-04-08T06:49:01Z")

</div>

On v7.2.0, elasticsearch and kibana both.

---

<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:** [May 6, 2021, 6:49am UTC](https://discuss.elastic.co/t/aggregation-on-substring-of-a-specific-field/269412/6 "2021-05-06T06:49:33Z")

</div>

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