# Search For a string in scripted index

**URL:** <https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [February 17, 2021, 5:44am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506 "2021-02-17T05:44:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 17, 2021, 5:44am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/1 "2021-02-17T05:44:41Z")

</div>

Hi, I need to search for a string in a field of an index using a scripted filed, below is how it looks like.

build\_info.causes {  
"cause\_class": "some vaue",  
"short\_description": "Triggered by Gerrit: some value ."  
}

I need to implement a scripted field where i search for the "short\_description" and if it is Gerrit do some action.

Thanks

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [February 18, 2021, 2:26am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/2 "2021-02-18T02:26:47Z")

</div>

Try a scripted field

```auto
String str = doc['short_description.keyword'].value;
if (str.contains("Gerrit")) {
return true;
}
return false;

```

[Another discussion](https://discuss.elastic.co/t/create-scripted-field-using-substring-before/238096/10)

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 18, 2021, 4:50am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/3 "2021-02-18T04:50:42Z")

</div>

> [@AClerk](#):
>
> `short_description.keyword`

Hey,  
Thanks for your reply. I have tried your code but unfortunately it is not working and failing with error  
**"No field found for [short\_description.keyword] in mapping with types []"**  
I have also tried build\_info.causes.short\_description.keyword instead of short\_description.keyword but same error.  
The mapping is something like below  
"build\_info" : {  
"properties" : {  
"built\_on" : {  
"type" : "text",  
"fields" : {  
"raw" : {  
"type" : "keyword"  
}  
}  
},  
"causes" : {  
"type" : "nested",  
"properties" : {  
"cause\_class" : {  
"type" : "text",  
"fields" : {  
"raw" : {  
"type" : "keyword"  
}  
}  
},  
"short\_description" : {  
"type" : "text"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [February 18, 2021, 6:13am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/4 "2021-02-18T06:13:19Z")

</div>

Validate there is a value. Seems like you have empty/null values.  
e.g.

```auto
if(!doc['short_description'].empty)
{
...
}

```

Cheers!

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 18, 2021, 7:14am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/5 "2021-02-18T07:14:10Z")

</div>

I have tried this but still the same error.  
**"No field found for [short\_description] in mapping with types []"**

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [February 18, 2021, 7:34am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/6 "2021-02-18T07:34:56Z")

</div>

> [@yash\_gupta](#):
>
> build\_info.causes

Maybe it is `build_info.causes.short_description`  
Not sure what your data looks like.  
Cheers!

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 18, 2021, 11:12pm UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/7 "2021-02-18T23:12:05Z")

</div>

Hey, Thanks much for reply but as i said it is not working with both and as for reference y mapping is as below:  
The mapping is something like below  
"build\_info" : {  
"properties" : {  
"built\_on" : {  
"type" : "text",  
"fields" : {  
"raw" : {  
"type" : "keyword"  
}  
}  
},  
"causes" : {  
"type" : "nested",  
"properties" : {  
"cause\_class" : {  
"type" : "text",  
"fields" : {  
"raw" : {  
"type" : "keyword"  
}  
}  
},  
"short\_description" : {  
"type" : "text"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [February 18, 2021, 11:33pm UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/8 "2021-02-18T23:33:40Z")

</div>

It is a nested field.  
Read how to fetch nested fields values.

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 19, 2021, 3:38am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/9 "2021-02-19T03:38:21Z")

</div>

Hi, I am able to run the code in scripted field and test it there but when i come back to discover tab and select respective index i see a red box with error message  
**script**  
String str = params.\_source.build\_info.causes[0]['short\_description'];

if(str.contains("Gerrit")){  
return str;  
}  
**Error**  
{"type":"script\_exception","reason":"runtime error","script\_stack":["str = params.\_source.build\_info.causes[0]['short\_description'];\r\n\r\n"," ^---- HERE"],"script":"String str = params.\_source.build\_info.causes[0]['short\_description'];\r\n\r\nif(str.contains("Gerrit")){\r\nreturn str;\r\n}","lang":"painless","caused\_by":{"type":"null\_pointer\_exception","reason":null}}}]},"status":400}

---

<div class="post-metadata">

**Author:** ![AClerk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aclerk/32/55297_2.png) [@AClerk](https://discuss.elastic.co/u/AClerk)\
**Post date:** [February 19, 2021, 4:17am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/10 "2021-02-19T04:17:50Z")

</div>

You may have null/empty values.  
Check if empty and return another string. For example "N/A".

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 19, 2021, 4:37am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/11 "2021-02-19T04:37:28Z")

</div>

Yes I did tried this  
if(!(params.\_source.build\_info.causes[0]['short\_description']).empty){  
return params.\_source.build\_info.causes[0]['short\_description'];  
}  
else{  
return "Not Found"  
}

But still same error.  
It seems that some document dose not have build\_info.causes so I tried  
if(doc.containsKey('build\_info.causes')){  
params['\_source']['build\_info']['causes'][0]['short\_description']  
}  
but this is giving null for all, even if there is value in causes.

---

<div class="post-metadata">

**Author:** ![yash\_gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yash_gupta/32/84010_2.png) [@yash\_gupta](https://discuss.elastic.co/u/yash_gupta)\
**Post date:** [February 22, 2021, 1:19am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/12 "2021-02-22T01:19:25Z")

</div>

if (params['\_source'].containsKey('build\_info'))  
{  
if(params['\_source']['build\_info'].containsKey('causes'))  
{  
....  
{ ...}  
else{ ...}  
}  
}  
else  
{  
....  
}

---

<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:** [March 22, 2021, 1:19am UTC](https://discuss.elastic.co/t/search-for-a-string-in-scripted-index/264506/13 "2021-03-22T01:19:32Z")

</div>

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