# Populate field Static Lookup data programmatically

**URL:** <https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246>\
**Category:** Kibana\
**Tags:** elastic-stack-machine-learning\
**Created:** [March 28, 2019, 6:19am UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246 "2019-03-28T06:19:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomr/32/48260_2.png) [@tomr](https://discuss.elastic.co/u/tomr)\
**Post date:** [March 28, 2019, 6:19am UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246/1 "2019-03-28T06:19:54Z")

</div>

Using the Static Lookup field formatter ([https://github.com/elastic/kibana/pull/19637](https://github.com/elastic/kibana/pull/19637))..

Is there a recommended way to populate the static lookup field programmatically, with a largeish table? I want to convert `mlcategory` values in my ML into something more human readable.

---

<div class="post-metadata">

**Author:** ![tomr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomr/32/48260_2.png) [@tomr](https://discuss.elastic.co/u/tomr)\
**Post date:** [March 28, 2019, 6:47am UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246/2 "2019-03-28T06:47:31Z")

</div>

I've found where it's stored:

```auto
GET api/saved_objects/index-pattern/my-pattern \
 | jq -r '.attributes.fieldFormatMap' | jq '.'
{
  "myfield": {
    "id": "static_lookup",
    "params": {
      "lookupEntries": [
        {
          "key": "k1",
          "value": "key one"
        },
        {
          "key": "k2",
          "value": "key 2"
        }
      ]
    }
  }
}

```

I'm guessing I can use the [saved objects update api](https://www.elastic.co/guide/en/kibana/current/saved-objects-api-update.html) to create this field.

```auto
PUT api/saved_objects/index-pattern/my-pattern
{
  "attributes": {
    "fieldFormatMap": "{\"myfield\":{\"id\":\"static_lookup\",\"params\":{\"lookupEntries\":[{\"key\":\"k2\",\"value\":\"key 2\"},{\"key\":\"k2\",\"value\":\"key 2\"}]}}}"
  }
}

```

---

<div class="post-metadata">

**Author:** ![tomr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tomr/32/48260_2.png) [@tomr](https://discuss.elastic.co/u/tomr)\
**Post date:** [March 29, 2019, 8:31am UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246/3 "2019-03-29T08:31:49Z")

</div>

I ended up using this monster one-liner to:

- get ML categories from my job's custom index;
- use `jq` to convert those categories into a `fieldFormatMap`; and
- put those categories into my ML index-pattern's attributes in Kibana

`curl -XPUT -H "Content-type: application/json" -H "kbn-xsrf: kibana" localhost:5601/api/saved_objects/index-pattern/672e6eb0-4eee-11e9-894c-f5374ebb52f8 -d "$(echo "{}" | jq --arg fieldmap "$(curl -H 'Content-type: application/json' -s -XGET localhost:9200/.ml-anomalies-custom-log-analysis-2/_search -d '{"version":true,"size":500,"query":{"bool":{"must":[{"exists":{"field":"category_id"}}]}}}' | jq -c '{ mlcategory: { id: "static_lookup", params: { lookupEntries: [( .hits.hits[]._source | { key: .category_id } + { value: ( (.category_id|tostring) + ": " + .examples[0] )})]}}}' )" '{ attributes: { fieldFormatMap: $fieldmap }}')"`

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [March 29, 2019, 5:14pm UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246/4 "2019-03-29T17:14:56Z")

</div>

I bow to your 1-liner skills! This looks great!

I thought you might like a couple of items as food for thought:

- This method could also be used to create new index patterns dynamically, in case you need to preserve the pre-updated version. You'd just change the `PUT` to a `POST` and not provide an ID is the index pattern path
  - you can also just use your own made-up ID

- Elasticsearch is able to do partial updates and scripted updates to documents, which you might want to check out: [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html)

---

<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:** [April 26, 2019, 5:15pm UTC](https://discuss.elastic.co/t/populate-field-static-lookup-data-programmatically/174246/5 "2019-04-26T17:15:01Z")

</div>

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