Is it posible to project _source ? many fields?

for example I have 2 indicies

  1. countries
  2. states

PUT /countries/_mapping

{
  "dynamic" : "strict",
  "properties" : {
    "id" : {
      "type" : "keyword"
    },
    "geo" : {
      "type" : "geo_point"
    },
    "name" : {
      "type" : "text"
    },
    "numberISO" : {
      "type" : "integer"
    }
  }
}

PUT /states/_mapping

{
  "dynamic" : "strict",
  "properties" : {
    "countryID" : {
      "type" : "keyword"
    },
    "id" : {
      "type" : "keyword"
    },
    "geo" : {
      "type" : "geo_point"
    },
    "name" : {
      "type" : "text"
    },
    "numberISO" : {
      "type" : "integer"
    }
  }
}

JOIN on [“id”, “countryID”]

as a result I want to get a set of included documents, like this:

"hits" : [
    {
      "_index" : "countries",
      "_type" : "_doc",
      "_id" : "1",
      "_score" : 1.0,
      "_source" : {
        id: '1',
        ...
      },
      "fields" : {
        "states" : [
          {
            "countryID": '1',
            "id": '2',
            "numberISO": 123
          },
          {
            "countryID": '1',
            "id": '3',
            "numberISO": 345
          },
          ...
        ]
      }
    }
  ]

At the moment you can only project fields, see Query domain-specific language (DSL) :: SIREN DOCS
However, this is not enough for you because the values of each document would get mixed.

yes it’s definitely not what I’m looking for, but could it be possible to do it with some scripts tricks ?

let me show you the simplest one

"query" : {
        "join" : {
          "indices" : ["states"],
          "on" : ["id", "countryID"],
          "request": {
            "project" : [
              {
                "script_field" : {
                  "name" : "_states",
                  "type" : "keyword",
                  "script" : {
                    "source" : "return 'id@@' +  doc.id + '##countryID@@' +  doc.countryID + '##geo@@' + doc.geo",
                    "lang" : "painless"
                  }
                }
              }
            ]
            }
          }
        }
      },
      "script_fields": {
        "states": {
          "script": {
            "source": "HashSet s = new HashSet(); for (String x: doc._states) { HashMap hm = new HashMap(); for (String y: /##/.split(x)) { String[] v = /@@/.split(y); hm.put(v[0], v[1]); } s.add(hm); } return s;",
            "lang": "painless"
          }
        }
      }

The main problem that here we’re losing fields types, all fields values are string no matter is it number or something else