# Effective way to pull a unique field value from Elasticsearch 2.4?

**URL:** <https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261>\
**Category:** Elasticsearch\
**Created:** [June 25, 2019, 8:26am UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261 "2019-06-25T08:26:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yashasvi\_Raj\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yashasvi_raj_pant/32/35022_2.png) [@Yashasvi\_Raj\_Pant](https://discuss.elastic.co/u/Yashasvi_Raj_Pant)\
**Post date:** [June 25, 2019, 8:26am UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/1 "2019-06-25T08:26:03Z")

</div>

I have a field called memberInfoId in Elasticsearch, whose value is unique in each document. I have thousands of documents in my Elasticsearch, so if I have to pull all the value of **memberInfoId** , **terms aggregation** seems to be very heavy. Any alternative to it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 25, 2019, 8:48am UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/2 "2019-06-25T08:48:29Z")

</div>

If the value is unique for each document, you probably don't need to aggregate as it will only produce one count per term.  
Instead just use the scroll API to fetch all the documents.

---

<div class="post-metadata">

**Author:** ![Yashasvi\_Raj\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yashasvi_raj_pant/32/35022_2.png) [@Yashasvi\_Raj\_Pant](https://discuss.elastic.co/u/Yashasvi_Raj_Pant)\
**Post date:** [June 25, 2019, 9:56am UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/3 "2019-06-25T09:56:57Z")

</div>

But scrolling needs multiple hits and I just want value from a field memberInfoId, not the whole document. So, do you think it's effective than terms aggregation?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 25, 2019, 10:30am UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/4 "2019-06-25T10:30:30Z")

</div>

As you won't be able to get back like 1m terms, yes probably it is more efficient.  
You can just fetch the field you want (if you stored it explicitly in the mapping) instead of getting the `_source` field.

---

<div class="post-metadata">

**Author:** ![Yashasvi\_Raj\_Pant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yashasvi_raj_pant/32/35022_2.png) [@Yashasvi\_Raj\_Pant](https://discuss.elastic.co/u/Yashasvi_Raj_Pant)\
**Post date:** [June 25, 2019, 8:42pm UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/5 "2019-06-25T20:42:17Z")

</div>

Please suggest me the ideal **size** I should use in the scroll API as I have thousands of documents. Do you mean to use **field** instead of **\_source**?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 25, 2019, 9:05pm UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/6 "2019-06-25T21:05:43Z")

</div>

No idea. You need to test it and find what is the sweet spot for you.

I meant to use [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-stored-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-stored-fields.html) or [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html)

---

<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 23, 2019, 9:05pm UTC](https://discuss.elastic.co/t/effective-way-to-pull-a-unique-field-value-from-elasticsearch-2-4/187261/7 "2019-07-23T21:05:45Z")

</div>

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