# Extract query result in csv file

**URL:** <https://discuss.elastic.co/t/extract-query-result-in-csv-file/315095>\
**Category:** Elasticsearch\
**Created:** [September 25, 2022, 8:18am UTC](https://discuss.elastic.co/t/extract-query-result-in-csv-file/315095 "2022-09-25T08:18:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shubh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shubh/32/111180_2.png) [@Shubh](https://discuss.elastic.co/u/Shubh)\
**Post date:** [September 25, 2022, 8:18am UTC](https://discuss.elastic.co/t/extract-query-result-in-csv-file/315095/1 "2022-09-25T08:18:49Z")

</div>

Hi all,  
can you help me how would i extract the java query output in csv file my code is given below.

```auto
try {
            RangeQueryBuilder rangeQ = QueryBuilders
                    .rangeQuery("@timestamp")
                    .gte("1663632000000")
                    .lte("1663804799000")
                    .format("epoch_millis");

            TermsAggregationBuilder termsAggregation = AggregationBuilders
                    .terms("term_by_client_id")
                    .field("labels.client_id")
                    .size(100000)
                    .minDocCount(1);

            termsAggregation
                    .subAggregation(
                            AggregationBuilders
                                    .sum("sum_by")
                                    .field("labels.row_count")
                    );
            termsAggregation
                    .subAggregation(
                            AggregationBuilders
                                    .terms("term_By_job")
                                    .field("labels.job_id")
                    );

            SearchRequest searchRequest = new SearchRequest();
            searchRequest.indices("*itm*");

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(rangeQ);
            searchSourceBuilder.aggregation(termsAggregation);
           // searchSourceBuilder.size(100000);
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            System.out.println(searchResponse);

            Aggregations aggregations = searchResponse.getAggregations();
            Map<String, Aggregation> aggregationMap = aggregations.asMap();
            for (Map.Entry<String, Aggregation> each : aggregationMap.entrySet()){
                System.out.println((each.getValue()));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

```

i want to extract the query result in csv file with this program, how i can impliment the export logic in this not able to understand i have tried but it is not woriking for me.  
Thanks.

---

<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:** [October 23, 2022, 8:19am UTC](https://discuss.elastic.co/t/extract-query-result-in-csv-file/315095/2 "2022-10-23T08:19:08Z")

</div>

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