# Unable to filter with scripted field even through Add a filter

**URL:** <https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676>\
**Category:** Elasticsearch\
**Created:** [January 20, 2021, 3:58pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676 "2021-01-20T15:58:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ras79](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@ras79](https://discuss.elastic.co/u/ras79)\
**Post date:** [January 20, 2021, 3:58pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/1 "2021-01-20T15:58:51Z")

</div>

I have a scripted field setup with the following code:

```auto
List list = new ArrayList();
          Map m = new HashMap();
          m.put("usColpo", "colpo");
          m.put("vp", "visualPap");
      
 
          for (int i = 0; i < doc['user.clinicalScenarios.keyword'].length; ++i) {
            def value = doc['user.clinicalScenarios.keyword'][i];
            if(m.get(value)!=null){
              list.add(m.get(value));
            } else {
              list.add(value);
            }
          }
          return list;

```

The original field clinicalScenario is a comma delimited string. It can have one or more values: usColpo, vp or usColpo, vp.  
When I go to the discover tab I can see all the values populated correctly but when I add the filter examType:colpo no results are returned. Is it because I return a list to populate the field? I would ultimately like to add a filter control for this field so that I can filter on it in a dashboard.

Thanks You

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [January 20, 2021, 5:31pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/2 "2021-01-20T17:31:46Z")

</div>

Can you post an example of what your scripted field result looks like?

---

<div class="post-metadata">

**Author:** ![ras79](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@ras79](https://discuss.elastic.co/u/ras79)\
**Post date:** [January 21, 2021, 8:23am UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/3 "2021-01-21T08:23:29Z")

</div>

![Screen Shot 2021-01-21 at 10.21.37](https://us1.discourse-cdn.com/elastic/original/3X/1/1/11ae2763ba96577772ad5296ed6931de0ca1a7fa.png) ![Screen Shot 2021-01-21 at 10.21.48](https://us1.discourse-cdn.com/elastic/original/3X/9/4/94723f52f5ca67828d0e3958a9180832e0492434.png)

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [January 21, 2021, 2:19pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/4 "2021-01-21T14:19:31Z")

</div>

What does your mapping look like?

I tested using the below data with the mapping shown and it works.

**Test Data**

```auto
PUT test

POST test/_doc
{
  "examType": "colpo",
  "message": "1"
}

POST test/_doc
{
  "examType": "sane",
  "message": "2"
}

POST test/_doc
{
  "examType": "colpo, via",
  "message": "3"
}

```

**Mapping**

```auto
        "examType" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },

```

---

<div class="post-metadata">

**Author:** ![ras79](https://avatars.discourse-cdn.com/v4/letter/r/d6d6ee/32.png) [@ras79](https://discuss.elastic.co/u/ras79)\
**Post date:** [January 21, 2021, 3:17pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/5 "2021-01-21T15:17:33Z")

</div>

I think I found the issue. It is because they are scripted array fields. This link seems to be my issue: [https://github.com/elastic/kibana/issues/61597](https://github.com/elastic/kibana/issues/61597)

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [January 21, 2021, 3:31pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/6 "2021-01-21T15:31:21Z")

</div>

That could be it, yes.

My test as you see didn't reproduce the scripted field so that's why it worked for me. 🙂

Have you tested this in a Control Visualization vs Filter Query? That could possibly work still.

---

<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:** [February 18, 2021, 3:31pm UTC](https://discuss.elastic.co/t/unable-to-filter-with-scripted-field-even-through-add-a-filter/261676/7 "2021-02-18T15:31:32Z")

</div>

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