Java, Build query builder

I am using Elasticsarch Java High level client, i have year field in my document i.e(2013,2014,2015....etc)
I need fetch how many unique year like (2013,2014,2016),2017 and count of specific year for that what querybuilder()
need to use

sample json

  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "2",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "1",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "3",
    "interest_payment_cat": "2",
    "dti": "13.71",
    "interest_rate": "13.53",
    "term": " 36 months",
    "id": "9674655",
    "purpose_cat": "6",
    "total_pymnt": "11152.70436",
    "annual_inc": "82000",
    "loan_amount": "9600",
    "interest_payments": "High",
    "income_category": "Low",
    "income_cat": "1",
    "home_ownership": "MORTGAGE",
    "emp_length_int": "10",
    "grade": "B",
    "installment": "325.92",
    "total_rec_prncp": "9600",
    "region": "munster",
    "final_d": "1062015",
    "application_type_cat": "1"
  },
  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "2",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "1",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "2",
    "interest_payment_cat": "1",
    "dti": "22.87",
    "interest_rate": "12.85",
    "term": " 36 months",
    "id": "9656372",
    "purpose_cat": "6",
    "total_pymnt": "4202.57",
    "annual_inc": "35000",
    "loan_amount": "5000",
    "interest_payments": "Low",
    "income_category": "Low",
    "income_cat": "1",
    "home_ownership": "OWN",
    "emp_length_int": "6.05",
    "grade": "B",
    "installment": "168.11",
    "total_rec_prncp": "3264.21",
    "region": "leinster",
    "final_d": "1012016",
    "application_type_cat": "1"
  },
  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "4",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "2",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "1",
    "interest_payment_cat": "2",
    "dti": "25.41",
    "interest_rate": "17.57",
    "term": " 60 months",
    "id": "9209595",
    "purpose_cat": "6",
    "total_pymnt": "29215.00999",
    "annual_inc": "51000",
    "loan_amount": "22800",
    "interest_payments": "High",
    "income_category": "Low",
    "income_cat": "1",
    "home_ownership": "RENT",
    "emp_length_int": "6.05",
    "grade": "D",
    "installment": "573.66",
    "total_rec_prncp": "22800",
    "region": "ulster",
    "final_d": "1112015",
    "application_type_cat": "1"
  },
  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "3",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "1",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "3",
    "interest_payment_cat": "2",
    "dti": "18.41",
    "interest_rate": "16.24",
    "term": " 36 months",
    "id": "9209574",
    "purpose_cat": "6",
    "total_pymnt": "33669.14633",
    "annual_inc": "101417",
    "loan_amount": "28000",
    "interest_payments": "High",
    "income_category": "Medium",
    "income_cat": "2",
    "home_ownership": "MORTGAGE",
    "emp_length_int": "10",
    "grade": "C",
    "installment": "987.72",
    "total_rec_prncp": "28000",
    "region": "ulster",
    "final_d": "1062015",
    "application_type_cat": "1"
  },
  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "2",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "1",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "3",
    "interest_payment_cat": "1",
    "dti": "12.86",
    "interest_rate": "10.99",
    "term": " 36 months",
    "id": "8982054",
    "purpose_cat": "6",
    "total_pymnt": "6872.65",
    "annual_inc": "60000",
    "loan_amount": "8400",
    "interest_payments": "Low",
    "income_category": "Low",
    "income_cat": "1",
    "home_ownership": "MORTGAGE",
    "emp_length_int": "10",
    "grade": "B",
    "installment": "274.97",
    "total_rec_prncp": "5534.16",
    "region": "ulster",
    "final_d": "1012016",
    "application_type_cat": "1"
  },
  {
    "loan_condition_cat": "0",
    "year": "2013",
    "application_type": "INDIVIDUAL",
    "purpose": "debt_consolidation",
    "grade_cat": "4",
    "issue_d": "01-12-2013",
    "recoveries": "0",
    "term_cat": "2",
    "loan_condition": "Good Loan",
    "home_ownership_cat": "3",
    "interest_payment_cat": "2",
    "dti": "29.86",
    "interest_rate": "18.25",
    "term": " 60 months",
    "id": "9219603",
    "purpose_cat": "6",
    "total_pymnt": "12765",
    "annual_inc": "51000",
    "loan_amount": "20000",
    "interest_payments": "High",
    "income_category": "Low",
    "income_cat": "1",
    "home_ownership": "MORTGAGE",
    "emp_length_int": "10",
    "grade": "D",
    "installment": "510.6",
    "total_rec_prncp": "6222.31",
    "region": "ulster",
    "final_d": "1012016",
    "application_type_cat": "1"
  }]```

If the field is a keyword type I'd use a terms aggregation. If it's a date type, I'd use a date histogram aggregation.

it's a keywords type by using terms aggregation we can get that? ok i will try

in documentation there is no terms aggregation method, Can give me a sample snippet from i could understand?

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-aggregation-builders.html

This document shows How it works I have understood, but I need to write as java code(using High client) for that i need snippet

An example here:

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