# Nested mapping or comparing fields from different indices \[SQL\]

**URL:** <https://discuss.elastic.co/t/nested-mapping-or-comparing-fields-from-different-indices-sql/291327>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 9, 2021, 12:44pm UTC](https://discuss.elastic.co/t/nested-mapping-or-comparing-fields-from-different-indices-sql/291327 "2021-12-09T12:44:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Katgust](https://avatars.discourse-cdn.com/v4/letter/k/c4cdca/32.png) [@Katgust](https://discuss.elastic.co/u/Katgust)\
**Post date:** [December 9, 2021, 12:44pm UTC](https://discuss.elastic.co/t/nested-mapping-or-comparing-fields-from-different-indices-sql/291327/1 "2021-12-09T12:44:36Z")

</div>

I'm aiming to replace our existing search functionality with Elasticsearch and I'm taking small steps towards that goal. Right now I'm trying to update a specific method but I would like some guidance.

We have in our system Groups and Units and every Unit belongs to a Group. Right now I've indexed these separate into index groups and index units respectively but my problem is that I've got a some pretty complicated sql queries in some places that I need to translate (I'm in .net using Nest) and I'm starting to wonder if it would be better to index these tables as one nested index instead.

The query I'm trying to translate right now looks like this

```auto
            const string selectSql = @"
SELECT 
	groups.GroupId, 
    groups.GroupName,
    units.UnitId, 
    units.UnitName
	FROM Groups groups
LEFT JOIN Units units ON groups.GroupId = units.GroupId
WHERE groups.GroupId IN 
	(SELECT groups.GroupId FROM Groups 
	LEFT JOIN Units units ON groups.GroupId = units.GroupId
	/ **where** /
	GROUP BY
	groups.GroupId, 
    groups.GroupName
	/ **orderby** /
	OFFSET @Skip ROWS 
	FETCH NEXT @Take ROWS ONLY)
GROUP BY 
	groups.GroupId, 
    groups.GroupName,
    units.UnitId, 
    units.UnitName
/ **orderby** / , units.UnitName
";

```

So my question is, is there a smart enough way to translate the left join and compare fields from different indices or would it be best to give nested mapping a go making a new, combined index? And if so would that apply to bigger, more nested examples (with more tables, joins and data) too?

---

<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:** [January 6, 2022, 12:45pm UTC](https://discuss.elastic.co/t/nested-mapping-or-comparing-fields-from-different-indices-sql/291327/2 "2022-01-06T12:45:27Z")

</div>

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