Distinct search using Java API

It's easy as documented in About the Elasticsearch category.

DELETE index
PUT index/_doc/1
{
  "foo": "bar"
}
GET index/_search
{
  "query": {
    "match": {
      "foo": "bar"
    }
  }
}

But just with your mapping, data and then with the Cardinality aggregation | Elasticsearch Guide [7.12] | Elastic. In that link you also have some "Kibana" examples.