# Scripted fields for only documents with specific fields

**URL:** <https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304>\
**Category:** Kibana\
**Created:** [May 26, 2020, 10:13am UTC](https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304 "2020-05-26T10:13:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![overlord11](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@overlord11](https://discuss.elastic.co/u/overlord11)\
**Post date:** [May 26, 2020, 10:13am UTC](https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304/1 "2020-05-26T10:13:25Z")

</div>

Hello fellow users.

Recently if been trying use the scripted fields in kibana 7.\*. I have allot of different documents with each different fields. But if i use my scripted field it creates the field in all the documents, is it possible to only create it in the documents where specifc fields exist? This is my script for my scripted field, i expected this to give the result I needed but it does not.

```auto
    if (doc['field1'].size() >0 || doc['field2'].size() >0) {
        if (doc['field1'].value !=0) {
            return 1
        } else if (doc['field2'].value !=0) {
            return 0
        }
    }

```

I would appreciate the help allot.

---

<div class="post-metadata">

**Author:** ![overlord11](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@overlord11](https://discuss.elastic.co/u/overlord11)\
**Post date:** [May 26, 2020, 11:28am UTC](https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304/2 "2020-05-26T11:28:36Z")

</div>

I know it would be possible if i created new indexes for each type of document. But that does not seem to be a great solution since that would result in allot of indexes which would not be ideal. So there is surely a way to only add a scripted field to a document if a specific field existed.

---

<div class="post-metadata">

**Author:** ![myasonik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/myasonik/32/62369_2.png) [@myasonik](https://discuss.elastic.co/u/myasonik)\
**Post date:** [May 26, 2020, 6:51pm UTC](https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304/3 "2020-05-26T18:51:17Z")

</div>

Scripted fields are created for an index, not an individual document, so you can't do exactly what you're after.

Why do you want the field to not exist on certain documents? If it's causing troubles in some visualizations or averages, you could try giving documents that don't have your field, their own value that might be easier to filter out (for example, if it's numeric data, giving a value of "Unknown" might make it easier to deal with).

---

<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 23, 2020, 6:51pm UTC](https://discuss.elastic.co/t/scripted-fields-for-only-documents-with-specific-fields/234304/4 "2020-06-23T18:51:18Z")

</div>

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