# How to get all the distinct values of field in sorted order using ES Java API

**URL:** https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649
**Category:** Elasticsearch
**Created:** [October 20, 2011, 1:18pm UTC](https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649 "2011-10-20T13:18:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kranti\_vns](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kranti\_vns](https://discuss.elastic.co/u/kranti_vns)
#### Post date: [October 20, 2011, 1:18pm UTC](https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649/1 "2011-10-20T13:18:49Z")

</div>

- deleted -

---

<div class="post-metadata">

### Author: ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)
#### Post date: [October 21, 2011, 10:57am UTC](https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649/2 "2011-10-21T10:57:45Z")

</div>

probably you need the scan query type?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On 20 Okt., 15:18, "kranti.vns" [kranti....@gmail.com](mailto:kranti....@gmail.com) wrote:

> Hi Team,
> 
> I am using the following Java program to query the elasticsearch server.
> 
> import org.elasticsearch.action.search.SearchResponse;  
> import org.elasticsearch.client.action.search.SearchRequestBuilder;  
> import org.elasticsearch.client.transport.TransportClient;  
> import org.elasticsearch.common.settings.ImmutableSettings;  
> import org.elasticsearch.common.settings.Settings;  
> import org.elasticsearch.common.transport.InetSocketTransportAddress;  
> import org.elasticsearch.index.query.QueryBuilder;  
> import org.elasticsearch.index.query.QueryBuilders;  
> import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;  
> import org.elasticsearch.search.SearchHit;
> 
> public class TestElasticSearch {
> 
> ```
> /**
> * @param args
> */
> public static void main(String[] args) {
> TestElasticSearch es = new TestElasticSearch ();
> es.launchSearch();
> }
> private void launchSearch() {
> String clusterName = "elasticsearch";
> String host = "localhost";
> String queryString = "message:snort";
> 
> Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",
> 
> ```
> 
> clusterName).build();  
> //Settings s = b.build();  
> TransportClient client = new TransportClient(s);  
> client.addTransportAddress(new InetSocketTransportAddress(host, 9300));
> 
> ```
> SearchRequestBuilder builder=client.prepareSearch();
> 
> QueryBuilder
> 
> ```
> 
> qb=QueryBuilders.queryString(queryString).defaultOperator(Operator.AND).  
> allowLeadingWildcard(false).analyzeWildcard(true);
> 
> // builder.addFacet(FacetBuilders.termsFacet("host").field("host"));
> 
> ```
> builder.setFrom(0).setSize(10);
> builder.setQuery(qb);
> builder.setExplain(true);
> 
> SearchResponse searchResponse=builder.execute().actionGet();
> 
> // do something with results
> System.out.println("------------- Results for query \"" + queryString +
> 
> ```
> 
> "" -------------");  
> show(searchResponse);
> 
> ```
> }
> private void show(SearchResponse response) {
> System.out.println("Total hits :"+response.hits().getTotalHits());
> for (SearchHit searchHit : response.hits()) {
> 
> ```
> 
> // System.out.println(searchHit.getSource().get("message"));  
> System.out.println(searchHit.getSource());  
> }  
> }
> 
> }
> 
> Using this program I iterate over SearchResponse object to get the SearchHit  
> Object.From SearchHit object I am extracting various fields and their values  
> respectively.The problem here is that I have to  
> process each row to retrieve the fields and their respective values.
> 
> Please let me know if there is any way using the ES Java API I could get all  
> the distinct values available for a particular field in sorted order, if the  
> search is performed based on a particular query string.
> 
> Thanks  
> Kranti
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/How-to-get-all-the-di](http://elasticsearch-users.115913.n3.nabble.com/How-to-get-all-the-di)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [October 21, 2011, 11:36am UTC](https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649/3 "2011-10-21T11:36:54Z")

</div>

On Fri, 2011-10-21 at 03:57 -0700, Karussell wrote:

> probably you need the scan query type?
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/search-type.html)

But scan doesn't sort.

You need to use scrolling, but without the scan search-type

clint

> On 20 Okt., 15:18, "kranti.vns" [kranti....@gmail.com](mailto:kranti....@gmail.com) wrote:
> 
> > Hi Team,
> > 
> > I am using the following Java program to query the elasticsearch server.
> > 
> > import org.elasticsearch.action.search.SearchResponse;  
> > import org.elasticsearch.client.action.search.SearchRequestBuilder;  
> > import org.elasticsearch.client.transport.TransportClient;  
> > import org.elasticsearch.common.settings.ImmutableSettings;  
> > import org.elasticsearch.common.settings.Settings;  
> > import org.elasticsearch.common.transport.InetSocketTransportAddress;  
> > import org.elasticsearch.index.query.QueryBuilder;  
> > import org.elasticsearch.index.query.QueryBuilders;  
> > import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;  
> > import org.elasticsearch.search.SearchHit;
> > 
> > public class TestElasticSearch {
> > 
> > ```
> > /**
> > * @param args
> > */
> > public static void main(String[] args) {
> > TestElasticSearch es = new TestElasticSearch ();
> > es.launchSearch();
> > }
> > private void launchSearch() {
> > String clusterName = "elasticsearch";
> > String host = "localhost";
> > String queryString = "message:snort";
> > 
> > Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",
> > 
> > ```
> > 
> > clusterName).build();  
> > //Settings s = b.build();  
> > TransportClient client = new TransportClient(s);  
> > client.addTransportAddress(new InetSocketTransportAddress(host, 9300));
> > 
> > ```
> > SearchRequestBuilder builder=client.prepareSearch();
> > 
> > QueryBuilder
> > 
> > ```
> > 
> > qb=QueryBuilders.queryString(queryString).defaultOperator(Operator.AND).  
> > allowLeadingWildcard(false).analyzeWildcard(true);
> > 
> > // builder.addFacet(FacetBuilders.termsFacet("host").field("host"));
> > 
> > ```
> > builder.setFrom(0).setSize(10);
> > builder.setQuery(qb);
> > builder.setExplain(true);
> > 
> > SearchResponse searchResponse=builder.execute().actionGet();
> > 
> > // do something with results
> > System.out.println("------------- Results for query \"" + queryString +
> > 
> > ```
> > 
> > "" -------------");  
> > show(searchResponse);
> > 
> > ```
> > }
> > private void show(SearchResponse response) {
> > System.out.println("Total hits :"+response.hits().getTotalHits());
> > for (SearchHit searchHit : response.hits()) {
> > 
> > ```
> > 
> > // System.out.println(searchHit.getSource().get("message"));  
> > System.out.println(searchHit.getSource());  
> > }  
> > }
> > 
> > }
> > 
> > Using this program I iterate over SearchResponse object to get the SearchHit  
> > Object.From SearchHit object I am extracting various fields and their values  
> > respectively.The problem here is that I have to  
> > process each row to retrieve the fields and their respective values.
> > 
> > Please let me know if there is any way using the ES Java API I could get all  
> > the distinct values available for a particular field in sorted order, if the  
> > search is performed based on a particular query string.
> > 
> > Thanks  
> > Kranti
> > 
> > --  
> > View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/How-to-get-all-the-di](http://elasticsearch-users.115913.n3.nabble.com/How-to-get-all-the-di)...  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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 6, 2017, 3:51am UTC](https://discuss.elastic.co/t/how-to-get-all-the-distinct-values-of-field-in-sorted-order-using-es-java-api/5649/4 "2017-07-06T03:51:13Z")

</div>


