Merge two index to create third index using logstash

I think logstash might not be the right tool, it's conceptually a mapper, what you need is a reducer. In practice you want to group documents, in your case you want to group docs with the same code field. You need aggregations for that.

Long story short, please have a look at the very similar ask: Merging documents based on matched fields values

As you explicitly say that you want an index as output, transform sounds like the right tool to me.