# Query to get data related to only those field returned by aggregation

**URL:** <https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557>\
**Category:** Elasticsearch\
**Created:** [October 9, 2018, 6:32am UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557 "2018-10-09T06:32:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_Nambiar](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikhil\_Nambiar](https://discuss.elastic.co/u/Nikhil_Nambiar)\
**Post date:** [October 9, 2018, 6:32am UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557/1 "2018-10-09T06:32:52Z")

</div>

Hi,  
I need a help on the query where I want to search by phone numbers and get latest date amount charged for that phone number  
my mapping structure is as follows

```
    "mappings":{
	"phone_list":{
		"properties":{
			"phone":{
				"type":"text"
			}
			"amount":{
				"type":"integer"
			}
			"date_timestamp":{
				"type":"text"
			}
			
		}
	}
}

```

I have performed a query which gives the latest date\_timestamp of a particular phone number . As follows

```
   {
      "query":{
    "match":{
          "phone":"1234567890"
        }
      },
      "aggs":{
          "latest_call_date&time":{
        "max":{
              "field":"date_timestamp"
            }
          }
    }
    }

```

Now how can I achieve the amount related to the date\_timestamp returned by aggregation function max.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 10, 2018, 1:52pm UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557/2 "2018-10-10T13:52:26Z")

</div>

Do you really need to use an aggregation? I think it's simpler to just sort the search results on `date_timestamp` and ask Elasticsearch for 1 hit. That one hit will then tell you everything you want to know about the most recent transaction for a given phone number.

```auto
GET test/_search
{
  "size": 1,
  "query": {
    "match": {
      "phone": "1234567890"
    }
  },
  "sort": [
    {
      "date_timestamp": {
        "order": "desc"
      }
    }
  ]
}

```

(By the way, I think `date_timestamp` should probably be mapped as a `date`?)

---

<div class="post-metadata">

**Author:** ![Nikhil\_Nambiar](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikhil\_Nambiar](https://discuss.elastic.co/u/Nikhil_Nambiar)\
**Post date:** [October 10, 2018, 2:13pm UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557/3 "2018-10-10T14:13:20Z")

</div>

Hi Abdon,

I had resolved the query, the same way as you mentioned.

For sake of curiosity can we achieve the same by aggregation?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 10, 2018, 2:34pm UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557/4 "2018-10-10T14:34:18Z")

</div>

Maybe something with a [top hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) or a [max bucket](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-max-bucket-aggregation.html) aggregation could get you to what you need.

---

<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:** [November 7, 2018, 2:34pm UTC](https://discuss.elastic.co/t/query-to-get-data-related-to-only-those-field-returned-by-aggregation/151557/5 "2018-11-07T14:34:20Z")

</div>

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