Scripted fields for only documents with specific fields

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.

    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.

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.

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).

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