# How to grab subset parts of a string, using its keys to map onto return value

**URL:** <https://discuss.elastic.co/t/how-to-grab-subset-parts-of-a-string-using-its-keys-to-map-onto-return-value/340855>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [August 15, 2023, 10:39pm UTC](https://discuss.elastic.co/t/how-to-grab-subset-parts-of-a-string-using-its-keys-to-map-onto-return-value/340855 "2023-08-15T22:39:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael\_c\_michael](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@michael\_c\_michael](https://discuss.elastic.co/u/michael_c_michael)\
**Post date:** [August 15, 2023, 10:39pm UTC](https://discuss.elastic.co/t/how-to-grab-subset-parts-of-a-string-using-its-keys-to-map-onto-return-value/340855/1 "2023-08-15T22:39:40Z")

</div>

Trying to do something which should be simple. Using **Painless** , take a value like "1.4" and another like "1.5", and using the first and second number, pop the name from a dictionary. I know there are methods like substr, but I don't know how to use them properly.

```auto
def dict = ['1.4': spaceship, '1.5': satelite]
def strct = doc['entityType.keyword'].value;
def first = strct.indexOf('.');
def second = first + 1 + strct.substring(first +1).indexOf('.');
def value = strct[0:first] + '-' +strct[first:second]
emit(dict[value].value)

```

---

<div class="post-metadata">

**Author:** ![jsanz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsanz/32/53734_2.png) [@jsanz](https://discuss.elastic.co/u/jsanz)\
**Post date:** [September 4, 2023, 3:35pm UTC](https://discuss.elastic.co/t/how-to-grab-subset-parts-of-a-string-using-its-keys-to-map-onto-return-value/340855/2 "2023-09-04T15:35:47Z")

</div>

You can do this in many ways, and I'm not sure about the content of your `entityType` field but in the Painless Lab application in the Dev Tools you can try easily to code different approaches that later you can adapt to your context.

For example this works for me:

```auto
def dict = ['1.4': 'spaceship', '1.5': 'satelite'];
def test_string = "A string with 1.5 on it";

for (key in dict.keySet()) {
    if ( test_string.contains(key) ) {
        return dict[key];
    }
}

return "Not found

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/a/3a6eb857d3ccfda8796bfb5a5c661a58b99311ce.png)

You could also use `indexOf` for sure.

```auto
def dict = ['1.4': 'spaceship', '1.5': 'satelite'];
def test_string = "A string with 1.5";

def from = test_string.indexOf('.');

if (from != -1){
    def key = test_string.substring(from - 1, from + 2 );
    def value = dict[key];
    if (value != null) {
        return value;
    }
} 

return "Not found"
``

Check all the methods available for strings at 

https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-template-java-lang.html#painless-api-reference-template-String
```

---

<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:** [October 2, 2023, 3:36pm UTC](https://discuss.elastic.co/t/how-to-grab-subset-parts-of-a-string-using-its-keys-to-map-onto-return-value/340855/3 "2023-10-02T15:36:04Z")

</div>

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