# Searching on Scripted field fails with shard errors

**URL:** https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213
**Category:** Kibana
**Created:** [April 28, 2020, 4:06pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213 "2020-04-28T16:06:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lmit](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@lmit](https://discuss.elastic.co/u/lmit)
#### Post date: [April 28, 2020, 4:06pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/1 "2020-04-28T16:06:45Z")

</div>

Trying to filter the following scripted field **Status** column to show me only GOOD

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

When I enter a filter for this field, I get shard failures

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/4/74b4c11bfa53ad016f188fbf63b57b3d7a818f06.png)

A sample error seems to indicates that my script does not return a value in some of the exit conditions

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

But when I examine my script, there is a return statement for all paths

```json
def good = new ArrayList();
def common_error_warning = new ArrayList();
def out = new ArrayList();

if(params._source.vehicle.sensors != null && params._source.vehicle.sensors.size() > 0) {
    for(int i = 0; i<params._source.vehicle.sensors.size(); i++ )
    {
      def go = params._source.vehicle.sensors[i];
      if(go.status.summary == 'Good'){
         if(good.contains(go.status.short_desc)){
                good.set(0, go.status.short_desc);
          }else {
                good.add(go.status.short_desc);
          }
      }
      else if(go.status.summary == 'Error'){
          if(common_error_warning.contains(go.status.short_desc)){
                common_error_warning.set(0, go.status.short_desc);
          }else {
                common_error_warning.add(go.status.short_desc);
          }
      }
      else if(go.status.summary == 'Warning'){
          if(common_error_warning.contains(go.status.short_desc)){
                common_error_warning.set(0, go.status.short_desc);
          }else {
                common_error_warning.add(go.status.short_desc);
          }
      }else {
          out.add( go.status.short_desc );
      }
    }
    if( common_error_warning.size() > 0 ) {
        return common_error_warning.toArray();
    }
    else if(good.size() > 0 && common_error_warning.size() == 0)
    {
        return good.toArray();
    }else
    {
        return out.toArray();
    }
}
return out.toArray();

```

and the error point seems to be outside of my script

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

Can anyone illuminate?

Thank you

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [May 4, 2020, 4:58pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/2 "2020-05-04T16:58:32Z")

</div>

Are you checking for "exists" in the script as well? That's the most common issue that causes this error.

---

<div class="post-metadata">

### Author: ![lmit](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@lmit](https://discuss.elastic.co/u/lmit)
#### Post date: [May 5, 2020, 1:33pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/3 "2020-05-05T13:33:47Z")

</div>

Thank you for responding Marius,

You are correct,  
Apparently the failure was due to **params.\_source** being **null**  
Which was resolved by checking the whole path

```auto
if(params._source != null && 
   params._source.vehicle != null && 
   params._source.vehicle.sensors != null && 
   params._source.vehicle.sensors.size() > 0) 
{
....
}

```

So no longer get **"Shard Failures"** when adding a **GOOD** filter on the **"Status"** field...  
Hurrayyy!

BUT I do get **an unexpected "No results match your search criteria"** , which makes me feel that i have a type mismatch occurring on the return data.  
Any Suggestions on strategies for resolving / diagnosing output of scripted fields?

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

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [May 5, 2020, 1:46pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/4 "2020-05-05T13:46:42Z")

</div>

Hmm, can you try removing the space before "GOOD"?

---

<div class="post-metadata">

### Author: ![lmit](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@lmit](https://discuss.elastic.co/u/lmit)
#### Post date: [May 5, 2020, 2:15pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/5 "2020-05-05T14:15:43Z")

</div>

No Change / Difference

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/6/263c440edb7e12595bd267036dfef70ac67dc538.png)

But if I remove/disable the filter, here is what i get

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

---

<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: [June 2, 2020, 2:15pm UTC](https://discuss.elastic.co/t/searching-on-scripted-field-fails-with-shard-errors/230213/6 "2020-06-02T14:15:52Z")

</div>

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