# How add field on index according to another value in same index

**URL:** <https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752>\
**Category:** Elasticsearch\
**Created:** [October 25, 2023, 4:48pm UTC](https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752 "2023-10-25T16:48:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVD\_MNC](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@DVD\_MNC](https://discuss.elastic.co/u/DVD_MNC)\
**Post date:** [October 25, 2023, 4:48pm UTC](https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752/1 "2023-10-25T16:48:34Z")

</div>

Hello, everyone,  
I am working on an elastic index.  
I would like to add a 'status' field that is equal to 'success' if the 'code' field is equal to 0, or the 'status' field is 'fail' if the 'code' field is different from 0.

I would like to do this automatically with the arrival of my logs. I tried with a processor in ingest pipeline but failed.

Could someone give me a hand?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 26, 2023, 4:02am UTC](https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752/2 "2023-10-26T04:02:26Z")

</div>

Hi @DVD_MNC

Something like this

```auto
PUT _ingest/pipeline/discuss_test
{
  "processors": [
    {
      "set": {
        "if": "ctx.code == 0",
        "field": "status",
        "value": "success"
      }
    },
    {
      "set": {
        "if": "ctx.code != 0",
        "field": "status",
        "value": "fail"
      }
    }
  ]
}

POST _ingest/pipeline/discuss_test/_simulate
{
  "docs": [
    {
      "_source": {
        "code": 0
      }
    },
    {
      "_source": {
        "code": 1
      }
    },
    {
      "_source": {
        "code": 3
      }
    },
    {
      "_source": {
        "code": -1
      }
    }
  ]
}

# Result
{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "code": 0,
          "status": "success"
        },
        "_ingest": {
          "timestamp": "2023-10-26T04:01:33.030899373Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "code": 1,
          "status": "fail"
        },
        "_ingest": {
          "timestamp": "2023-10-26T04:01:33.030925393Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "code": 3,
          "status": "fail"
        },
        "_ingest": {
          "timestamp": "2023-10-26T04:01:33.030933593Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_version": "-3",
        "_source": {
          "code": -1,
          "status": "fail"
        },
        "_ingest": {
          "timestamp": "2023-10-26T04:01:33.030939827Z"
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![DVD\_MNC](https://avatars.discourse-cdn.com/v4/letter/d/f08c70/32.png) [@DVD\_MNC](https://discuss.elastic.co/u/DVD_MNC)\
**Post date:** [October 27, 2023, 2:16pm UTC](https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752/3 "2023-10-27T14:16:27Z")

</div>

Thanks for response. I solved. Thank you so much!

---

<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:** [November 24, 2023, 2:17pm UTC](https://discuss.elastic.co/t/how-add-field-on-index-according-to-another-value-in-same-index/345752/4 "2023-11-24T14:17:09Z")

</div>

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