'Elasticsearch - split nested objects from an index into a new index

I have an index user-stats in Elastic that has documents as follows:

{
  "userId": 1285641,
  "pageVisits": [
    {
      "pageUrl": "/profile",
      "pageName": "My Profile",
      "responseTime": 25,
      "exception": null
    },
    {
      "pageUrl": "/admin",
      "pageName": "Admin Console",
      "responseTime": 12,
      "exception": {
         "code": 401,
         "message": "You do not have permission"
      }
    }
  ]
}

I would like that each time I save a document to the user-stats index, I do some sort of a pipeline that would publish to another index page-visits

{
  "userId": 1285641,
  "pageUrl": "/profile",
  "pageName": "My Profile",
  "responseTime": 25,
  "exception": null
}
{
  "userId": 1285641,
  "pageUrl": "/admin",
  "pageName": "Admin Console",
  "responseTime": 12,
  "exception": {
  "code": 401,
    "message": "You do not have permission"
  }
}

Since I can't change the app that publishes to the first index, I would like to know if I can do something to automatically create my second index, and also update it at each save?

Thank you in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source