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

  • deleted -

probably you need the scan query type?

On 20 Okt., 15:18, "kranti.vns" 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...
Sent from the Elasticsearch Users mailing list archive at Nabble.com.

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

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 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...
Sent from the Elasticsearch Users mailing list archive at Nabble.com.