How to Install Elasticsearch 5.6.9 and Perform Advanced Data Aggregations with Java
This guide walks you through downloading and configuring Elasticsearch 5.6.9, setting system limits, creating indices, inserting and deleting documents, executing complex aggregation queries via HTTP, and integrating Elasticsearch with Java using the Transport client for powerful data analysis.
1. Download and Install Elasticsearch
Visit the official Elasticsearch site, choose version 5.6.9, and download it. The minimum required JDK is 1.8, so ensure the JAVA_HOME environment variable is set.
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.9.tar.gz tar -zxvf elasticsearch-5.6.9.tar.gz -C /usr/local/ cd /usr/local/elasticsearch-5.6.9
Edit config/elasticsearch.yml to set the cluster name, node name, data and log paths, network host and HTTP port.
cluster.name: tsk-es node.name: tsk1 path.data: /opt/data/elastic/data path.logs: /opt/data/elastic/log network.host: 0.0.0.0 http.port: 9200
Create a non‑root user (e.g., elastic) and grant it permission to the Elasticsearch directories.
Increase the file‑descriptor limits:
* soft nofile 65536 * hard nofile 65536 * soft nproc 2048 * hard nproc 4096
Raise the virtual memory map count:
vm.max_map_count=262144
Switch to the elastic user and start Elasticsearch:
su elastic bin/elasticsearch
Run it in the background if needed:
nohup bin/elasticsearch > /opt/data/elastic/elastic.log 2>&1 &
2. Basic ES Operations
All operations are performed via HTTP. Create an index with a mapping:
PUT http://192.168.0.1:9200/shopsinfo { "mappings": { "shopsOrder": { "properties": { "shopid": {"type": "string", "index": "not_analyzed"}, "createdate": {"type": "string", "index": "not_analyzed"}, "timestamp": {"type": "long"}, "paymentType": {"type": "string", "index": "not_analyzed"}, "amount": {"type": "long"} } } } }
Insert a document:
POST http://192.168.0.1:9200/shopsinfo/shopsOrder { "shopid": "96119", "createdate": "20180410", "timestamp": 1523289600000, "paymentType": "alipay", "amount": 6917 }
Delete by query:
POST http://192.168.0.1:9200/shopsinfo/shopsOrder/_delete_by_query
Search:
GET http://192.168.0.1:9200/shopsinfo/shopsOrder/_search
3. Query and Aggregation
Simple filtered query (SQL equivalent shown for reference):
POST http://192.168.0.1:9200/shopsinfo/shopsOrder/_search { "size": 20, "query": { "bool": { "must": [ {"range": {"timestamp": {"gte": 1523671189000}}}, {"terms": {"shopid": ["100000002", "100000006"]}} ] } } }
Aggregation to sum amount for the same filter:
{ "aggs": { "query_amount": {"sum": {"field": "amount"}} }, "query": { "bool": { "must": [ {"range": {"timestamp": {"gte": 1523671189000}}}, {"terms": {"shopid": ["100000002", "100000006"]}} ] } } }
Daily bucket aggregation:
{ "size": 0, "aggs": { "orderDate": { "terms": {"field": "createdate", "order": {"_term": "asc"}}, "aggs": {"query_amount": {"sum": {"field": "amount"}}} } }, "query": { "bool": { "must": [ {"range": {"timestamp": {"gte": 1523671189000}}}, {"terms": {"shopid": ["100000002", "100000006"]}} ] } } }
Complex aggregation: total amount, amount per paymentType, and daily amount per payment method.
{ "size": 0, "aggs": { "amount": {"sum": {"field": "amount"}}, "paymenttype": { "terms": {"field": "paymentType"}, "aggs": { "query_amount": {"sum": {"field": "amount"}}, "payment_date": { "terms": {"field": "createdate"}, "aggs": {"query_amount": {"sum": {"field": "amount"}}} } } } }, "query": { "bool": { "must": [ {"range": {"timestamp": {"gte": 1523671189000}}}, {"terms": {"shopid": ["100000002", "100000006"]}} ] } } }
4. Java Integration
Add the matching Elasticsearch 5.6.9 client dependencies:
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.9</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.9</version> </dependency>
Create a TransportClient:
Settings settings = Settings.builder() .put("cluster.name", "tsk-es") .put("client.transport.sniff", true) .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), PORT));
Index a document (JSON string or Java bean):
IndexResponse response = client.prepareIndex(index, mapping) .setSource(jsonStr, XContentType.JSON) .get();
Example aggregation method (simplified):
public void getAmountData(Long startTimestamp, String... shopIds) { SearchRequestBuilder sb = client.prepareSearch("shopsinfo").setTypes("shopsOrder"); TermsQueryBuilder shopQuery = QueryBuilders.termsQuery("shopid", shopIds); RangeQueryBuilder timeQuery = QueryBuilders.rangeQuery("timestamp").gte(startTimestamp); sb.setQuery(QueryBuilders.boolQuery().must(shopQuery).must(timeQuery)); sb.setSize(0); SumAggregationBuilder sumAgg = AggregationBuilders.sum("query_amount").field("amount"); TermsAggregationBuilder paymentAgg = AggregationBuilders.terms("paymentType").field("paymentType").size(100).subAggregation(sumAgg); TermsAggregationBuilder dateAgg = AggregationBuilders.terms("payment_date").field("createdate").order(Order.term(true)).size(100).subAggregation(sumAgg); paymentAgg.subAggregation(dateAgg); sb.addAggregation(sumAgg).addAggregation(paymentAgg); SearchResponse resp = sb.execute().actionGet(); // Process resp.getAggregations() as needed }
The response contains aggregation results under the aggregations field, where each bucket provides key, doc_count, and the computed value for the sum.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
