How to combine results from 2 elasticsearch indexes with different layouts

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

This is an example:

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
      }
    }
  }
}
{
  "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
            }
          }
        }
      }
    ]
  }
}

3 Likes