# Access to a field of another type when doing query

**URL:** <https://discuss.elastic.co/t/access-to-a-field-of-another-type-when-doing-query/64526>\
**Category:** Elasticsearch\
**Created:** [November 1, 2016, 8:14am UTC](https://discuss.elastic.co/t/access-to-a-field-of-another-type-when-doing-query/64526 "2016-11-01T08:14:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saeed\_Zhiany](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saeed_zhiany/32/12865_2.png) [@Saeed\_Zhiany](https://discuss.elastic.co/u/Saeed_Zhiany)\
**Post date:** [November 1, 2016, 8:14am UTC](https://discuss.elastic.co/t/access-to-a-field-of-another-type-when-doing-query/64526/1 "2016-11-01T08:14:18Z")

</div>

relevant part of my mapping is here

```
{
  "mappings": {
    "message": {
      "properties": {
        "id": {
          "type": "string",
          "index": "not_analyzed"
        },
        "sourceId": {
          "type": "string",
          "index": "not_analyzed"
        },
        "sourceName": {
          "type": "string",
          "index": "analyzed"
        },
        "text": {
          "type": "string",
          "store": true
        },
        "creationTimestamp": {
          "type": "integer",
          "index": "not_analyzed"
        },
        "histories": {
          "type": "nested",
          "properties": {
            "views": {
              "type": "integer",
              "index": "not_analyzed"
            },
            "timestamp": {
              "type": "integer",
              "index": "not_analyzed"
            }
          }
        }
      }
    },
    "source": {
      "properties": {
        "id": {
          "type": "string",
          "index": "not_analyzed"
        },
        "name": {
          "type": "string",
          "store": true
        },
        "membersCount": {
          "type": "long",
          "index": "not_analyzed"
        }
      }
    }
  }
}

```

**about mapping**  
  
`sourceId` and `sourceName` in `message` is `id` and `name` in `source`.  
each source can have a lots of messages and most fields of data (my focus is `membersCount` field in here) has been updated periodically. so I think it's best mapping for my case for implementing relation between data (rather than parent-child approach)

what I want to achieve is sorting sources based on custom scoring by this formula:

> scoreOfSource = (sumOfViewsOfMessagesBelongToCurrentSourceInAPeriod) / membersCount

to achieve this, I created below query on `message` type

```
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "creationTimestamp": {
                  "gte": 1475325259,
                  "lte": 1476102877
                }
              }
            },
            {
              "match": {
                "text": {
                  "query": "car",
                  "fuzziness": 0
                }
              }
            }
          ]
        }
      },
      "script_score": {
        "script": "_source.histories[_source.histories.values.size()-1].views"
      },
      "boost_mode": "replace"
    }
  },
  "aggs": {
    "sources": {
      "terms": {
        "field": "sourceId",
        "order": {
          "myscore": "desc"
        },
        "size": 100
      },
      "aggs": {
        "myscore": {
          "sum": {
            "script": "_score"
          }
        }
      }
    }
  },
  "size": 0
}

```

by the aggregation I can get sum of total views of each source's messages in `myscore` but I don't know how can I access to `membersCount` of each source because for Division. it is a field of `source` type and I searching on messages. as I said, membersCount has been updated periodically. so it's not efficient to store the field in `message` type.

is that a way to access the field in aggregation? for example using a subquery on another type in within of aggregation or any other solution.

---

<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:** [July 5, 2017, 10:07pm UTC](https://discuss.elastic.co/t/access-to-a-field-of-another-type-when-doing-query/64526/2 "2017-07-05T22:07:57Z")

</div>

Closed on July 5, 2017, 10:07pm UTC


