# How to combine results from 2 elasticsearch indexes with different layouts

**URL:** <https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659>\
**Category:** Elasticsearch\
**Created:** [January 6, 2022, 3:00pm UTC](https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659 "2022-01-06T15:00:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan499](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan499/32/99917_2.png) [@jan499](https://discuss.elastic.co/u/jan499)\
**Post date:** [January 6, 2022, 3:00pm UTC](https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659/1 "2022-01-06T15:00:02Z")

</div>

We have an Elasticsearch with 2 different indexes. In one index there is a field called `physical_location` which contains a lat en lon subfield. in the other index there is a field called `program_run.geolocation` with the same nested lat and lon.

Now we want to plot documents from both indexes on a map in our client web application. we tried using a boolean query

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "geo_distance": {
          "distance": "10km",
          "physical_location": {
            "lat": 53.193078596551715,
            "lon": 6.783202751724139
          }
        }
        },
        
        {
          "geo_distance": {
          "distance": "10km",
          "program_run.geolocation": {
            "lat": 53.193078596551715,
            "lon": 6.783202751724139
          }
        }
      } 
        ]
    }
  }
}

```

but this does give an error, because elastic cant find the first field in the second index and the second field in the first index.

What is the best way of fixing this?

- Is there a server side way of combining queries for different indexes with different layouts without errors?
- or is it more handy to combine on the client? (and how to deal with aggregate counts in that case)
- or is the only way out reindexing and making the names the same in both indexes? (requires lots of refactor work).

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 6, 2022, 5:30pm UTC](https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659/2 "2022-01-06T17:30:20Z")

</div>

how about add alias field type to both index. It may not need reindexing.

> **[Alias field type | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/field-alias.html)**

This is an example:

```auto
PUT /my_locations1
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

PUT /my_locations1/_doc/1
{
  "pin": {
    "location": {
      "lat": 40.12,
      "lon": -71.34
    }
  }
}

PUT /my_locations2
{
  "mappings": {
    "properties": {
      "pin2": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

PUT /my_locations2/_doc/1
{
  "pin2": {
    "location": {
      "lat": 40.12,
      "lon": -71.34
    }
  }
}

PUT /my_locations1/_mapping
{
  "properties": {
    "location_for_search": {
      "type":"alias",
      "path": "pin.location"
    }
  }
}

PUT /my_locations2/_mapping
{
  "properties": {
    "location_for_search": {
      "type":"alias",
      "path": "pin2.location"
    }
  }
}

GET /my_locations*/_search
{
  "query":{
    "geo_distance":{
      "distance": "2000km",
      "location_for_search":{
        "lat": 40,
        "lon": -70
      }
    }
  }
}

```

```auto
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_locations1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "pin" : {
            "location" : {
              "lat" : 40.12,
              "lon" : -71.34
            }
          }
        }
      },
      {
        "_index" : "my_locations2",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "pin2" : {
            "location" : {
              "lat" : 40.12,
              "lon" : -71.34
            }
          }
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![jan499](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan499/32/99917_2.png) [@jan499](https://discuss.elastic.co/u/jan499)\
**Post date:** [January 7, 2022, 3:22pm UTC](https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659/3 "2022-01-07T15:22:29Z")

</div>

Thanks Tomo! This is a real time saver for us!

---

<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:** [February 4, 2022, 3:23pm UTC](https://discuss.elastic.co/t/how-to-combine-results-from-2-elasticsearch-indexes-with-different-layouts/293659/4 "2022-02-04T15:23:08Z")

</div>

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