Comprehensive Elasticsearch Query Guide with Java High Level REST Client Examples
This tutorial provides a thorough overview of Elasticsearch 7.13.2 query techniques—including term, terms, range, prefix, wildcard, boolean, filter, and aggregation queries—showing both raw DSL syntax and equivalent Java High Level REST Client code, enabling developers to quickly master search operations on large datasets.
This article presents a comprehensive guide to Elasticsearch query scenarios using version 7.13.2, illustrating how to perform term, terms, range, prefix, wildcard, boolean, filter, and aggregation queries with both raw DSL and the Java High Level REST Client.
Test Data Example
A sample MySQL row is indexed as an Elasticsearch document:
{
"_index": "person",
"_type": "_doc",
"_id": "4",
"_source": {
"address": "峨眉山",
"modifyTime": "2021-06-29 19:46:25",
"createTime": "2021-05-14 11:37:07",
"sect": "峨嵋派",
"sex": "男",
"skill": "降龙十八掌",
"name": "宋青书",
"id": 4,
"power": 50,
"age": 21
}
}1. Term Query (Exact Match)
SQL:
select * from person where name = '张无忌';Elasticsearch DSL:
GET /person/_search
{
"query": {
"term": {
"name.keyword": {
"value": "张无忌",
"boost": 1.0
}
}
}
}Java High Level REST Client:
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.termQuery("name.keyword", "张无忌"));
searchRequest.source(builder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);2. Terms Query (Multiple Values)
SQL:
select * from persons where sect in ('明教','武当派');Elasticsearch DSL:
GET /person/_search
{
"query": {
"terms": {
"sect.keyword": ["明教", "武当派"],
"boost": 1.0
}
}
}Java:
builder.query(QueryBuilders.termsQuery("sect.keyword", Arrays.asList("明教", "武当派")));3. Range Query
SQL:
select * from persons where age between 18 and 22;Elasticsearch DSL:
GET /person/_search
{
"query": {
"range": {
"age": {"from": 10, "to": 20, "include_lower": true, "include_upper": true, "boost": 1.0}
}
}
}Java:
builder.query(QueryBuilders.rangeQuery("age").gte(10).lte(30));4. Prefix and Wildcard Queries
Prefix (like SQL LIKE '武当%' ) DSL:
GET /person/_search
{
"query": {"prefix": {"sect.keyword": {"value": "武当", "boost": 1.0}}}
}Wildcard (like SQL LIKE '张%忌' ) DSL:
GET /person/_search
{
"query": {"wildcard": {"sect.keyword": {"wildcard": "张*忌", "boost": 1.0}}}
}5. Boolean (Compound) Queries
Combining multiple conditions with must , should , and must_not :
GET /person/_search
{
"query": {
"bool": {
"must": [{"term": {"sex": {"value": "女", "boost": 1.0}}}],
"must_not": [{"term": {"sect.keyword": {"value": "明教", "boost": 1.0}}}],
"should": [
{"term": {"address.keyword": {"value": "峨眉山", "boost": 1.0}}},
{"term": {"skill.keyword": {"value": "暗器", "boost": 1.0}}}
],
"minimum_should_match": "1"
}
}
}Java:
BoolQueryBuilder bool = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sex", "女"))
.mustNot(QueryBuilders.termQuery("sect.keyword", "明教"))
.should(QueryBuilders.termQuery("address.keyword", "峨眉山"))
.should(QueryBuilders.termQuery("skill.keyword", "暗器"))
.minimumShouldMatch(1);
builder.query(bool);6. Filter Queries (No Scoring)
Using filter to improve performance by skipping score calculation:
GET /person/_search
{
"query": {
"bool": {
"filter": [{"term": {"sex": {"value": "男", "boost": 1.0}}}]
}
}
}7. Aggregations
Maximum age aggregation:
GET /person/_search
{
"size": 0,
"aggregations": {"max_age": {"max": {"field": "age"}}}
}Cardinality (distinct count) of sect:
GET /person/_search
{
"size": 0,
"aggregations": {"sect_count": {"cardinality": {"field": "sect.keyword"}}}
}Terms aggregation to count documents per sect:
GET /person/_search
{
"size": 0,
"aggregations": {"sect_count": {"terms": {"field": "sect.keyword", "size": 10}}}
}Range aggregation for age groups:
GET /person/_search
{
"size": 0,
"aggregations": {
"age_groups": {
"range": {
"field": "age",
"ranges": [
{"to": 20},
{"from": 21, "to": 40},
{"from": 41, "to": 60},
{"from": 61}
]
}
}
}
}All examples are accompanied by the equivalent Java High Level REST Client code, allowing developers to copy‑paste and adapt the snippets for real projects.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.