# Add new fields using ingest pipeline script processor

**URL:** <https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537>\
**Category:** Elasticsearch\
**Created:** [June 6, 2019, 9:20am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537 "2019-06-06T09:20:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 6, 2019, 9:20am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/1 "2019-06-06T09:20:58Z")

</div>

Hi there!

I'd like to add an ingest pipeline to my index in order to add some further info to incoming documents.

Let's say these docs have two fields, USERNAME and CITY. I'd like to add also latitude and longitude according to the field city.

I tried with an ingest pipeline like the following:

```
PUT _ingest/pipeline/my_index
{
    "processors": [
      {
        "script": {
          "source": """
            if (ctx.place == params.key_rome) {
              ctx.lat = params.rome_lat;
              ctx.long = params.rome_long;
            }
            if (ctx.place == params.key_ny) {
              ctx.lat = params.ny_lat;
              ctx.long = params.ny_long;
            }
            if (ctx.place == params.key_london) {
              ctx.lat = params.london_lat;
              ctx.long = params.london_long;
            }
          """,
          "params": {
            "key_rome": "Rome",
            "rome_lat": "X",
            "rome_long": "Y",
            "key_ny": "NewYork",
            "ny_lat": "Z",
            "ny_long": "T",
            "key_london": "London",
            "london_lat": "V",
            "london_long": "W"
          }
        }
      }
    ]
}

```

It didn't give any error but it simply did nothing.  
I also tried adding a useless line after the last 'if block' simply to copy the field "place" into another field `ctx.new_place = ctx.place` and it didn't work either.

What am I doing wrong?

Thank you!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 7, 2019, 12:47pm UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/2 "2019-06-07T12:47:09Z")

</div>

Hey

can you share the output of

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [ 
    { "_source" : { "place":"London" } }, 
    { "_source" : { "place":"Rome" } }, 
    { "_source" : { "place":"NewYork" } }, 
    { "_source" : { "place":"Berlin" } } 
    ],
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
            if (ctx.place == params.key_rome) {
              ctx.lat = params.rome_lat;
              ctx.long = params.rome_long;
            }
            if (ctx.place == params.key_ny) {
              ctx.lat = params.ny_lat;
              ctx.long = params.ny_long;
            }
            if (ctx.place == params.key_london) {
              ctx.lat = params.london_lat;
              ctx.long = params.london_long;
            }
""",
          "params": {
            "key_rome": "Rome",
            "rome_lat": "X",
            "rome_long": "Y",
            "key_ny": "NewYork",
            "ny_lat": "Z",
            "ny_long": "T",
            "key_london": "London",
            "london_lat": "V",
            "london_long": "W"
          }
        }
      }
    ]
  }
}

```

This example works for me and enriches three out of those four cities. Have you specified the pipeline when indexing a document? Please provide an exact reproduction and the elasticsearch version if it still does not work as expected.

--Alex

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 10, 2019, 9:06am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/3 "2019-06-10T09:06:41Z")

</div>

Ok I guess I found my problem. I only manage to edit stored data and not incoming ones, since I basically apply the ingest pipeline to my index via an update\_by\_query call.

How can I set my ingest pipeline as the default one for that specific index?

Thanks!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 10, 2019, 9:20am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/4 "2019-06-10T09:20:41Z")

</div>

See `index.default_pipeline` in [https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html)

---

<div class="post-metadata">

**Author:** ![Fabio-sama](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@Fabio-sama](https://discuss.elastic.co/u/Fabio-sama)\
**Post date:** [June 10, 2019, 9:40am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/5 "2019-06-10T09:40:12Z")

</div>

Great!

What I needed was to do:

```
PUT /my_index/_settings
{
    "index" : {
        "default_pipeline" : "my_pipe"
    }
}

```

Thank you so much for the tip!

---

<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:** [July 8, 2019, 9:50am UTC](https://discuss.elastic.co/t/add-new-fields-using-ingest-pipeline-script-processor/184537/6 "2019-07-08T09:50:01Z")

</div>

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