# How to enrich/auto-generate fields before index?

**URL:** <https://discuss.elastic.co/t/how-to-enrich-auto-generate-fields-before-index/226252>\
**Category:** Elasticsearch\
**Created:** [April 2, 2020, 4:47pm UTC](https://discuss.elastic.co/t/how-to-enrich-auto-generate-fields-before-index/226252 "2020-04-02T16:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![redec](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@redec](https://discuss.elastic.co/u/redec)\
**Post date:** [April 2, 2020, 4:47pm UTC](https://discuss.elastic.co/t/how-to-enrich-auto-generate-fields-before-index/226252/1 "2020-04-02T16:47:28Z")

</div>

I'm fairly new to elasticsearch, so apologies if this is a dumb question...Is there a way to automatically generate a field based on other fields, prior to the document being indexed? I would like to generate a string based on a couple other fields in the document, which will be used for ordering records with equal scores. Here's a simple example of what I'm trying to accomplish, and hoping is possible, somehow:

```auto
{
  file_name: { type: 'text' },
  directory: { type: 'nested', properties: { name: 'text' } },
  full_path: { type: 'keyword', template: '${directory.name.join("/") + "/" + file_name}` }
}

```

So, I know this is a rough example but I'm hoping it gets my point across. In this example the "directory" field contains multiple values - 1 value for each parent directory, so an example document which would represent the file "/home/redec/foo.log" would look like this:

```auto
{
  file_name: 'foo.log',
  directory: [{ name: 'home' }, { name: 'redec' }],
  full_path: '/home/redec/foo.log'
}

```

Obviously I realize I can generate the full\_path value before inserting the document, but I would like to do it on the elasticsearch side so that I can do a targeted update to update the name of a parent directory and have the full\_path get automatically updated (so I don't have to individually update every document one by one)

I realize sorting/ordering by a string field is not exactly in elasticsearch's core competency, and does not seem to be a common thing to do so I'm kinda in fringe territory here, but I'm hoping there's some mechanism that can be used to accomplish this. Any help/advice is appreciated! Thanks! 🙂

---

<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 30, 2020, 4:47pm UTC](https://discuss.elastic.co/t/how-to-enrich-auto-generate-fields-before-index/226252/2 "2020-04-30T16:47:38Z")

</div>

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