Databases 8 min read

Master Advanced Elasticsearch Queries: From Match All to Aggregations

This guide walks through Elasticsearch 7.8.0 advanced query techniques—including match‑all, term, multi‑match, range, fuzzy, sorting, highlighting, pagination, and various aggregations—showing the required HTTP methods, endpoints, request bodies, and example responses for each operation.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Advanced Elasticsearch Queries: From Match All to Aggregations

Environment: Elasticsearch 7.8.0 + Postman

Elasticsearch is a Lucene‑based search server that provides a distributed, multi‑user full‑text search engine via a RESTful web interface. Developed in Java and released under the Apache license, it is widely used in cloud computing for real‑time, stable, reliable, and fast search. Official clients exist for Java, .NET (C#), PHP, Python, Groovy, Ruby, and many other languages. According to DB‑Engines, Elasticsearch is the most popular enterprise search engine, followed by Apache Solr, both built on Lucene.

Advanced Query Operations

1. Query All Documents

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "match_all": {}
  }
}

2. Match Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "match": {
      "name": "莉莉"
    }
  }
}

3. Multi‑Field Match Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "multi_match": {
      "query": "张三",
      "fields": ["name","sex"]
    }
  }
}

4. Term (Exact) Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "term": {
      "name": "joke"
    }
  }
}

5. Multi‑Term Exact Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}

6. Retrieve Specific Fields

Method: POST

URL: http://localhost:9200/users/_search

{
  "_source": ["name","sex"],
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}

7. Include Fields (Source Filtering)

Method: POST

URL: http://localhost:9200/users/_search

{
  "_source": {
    "includes": ["name","age"]
  },
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}

8. Exclude Fields (Source Filtering)

Method: POST

URL: http://localhost:9200/users/_search

{
  "_source": {
    "excludes": ["name","age"]
  },
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}

9. Bool (Combined) Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "bool": {
      "must": [
        {"match": {"name": "莉莉"}}
      ],
      "must_not": [
        {"match": {"age": 40}}
      ],
      "should": [
        {"match": {"sex": "女"}}
      ]
    }
  }
}

10. Range Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "range": {
      "age": {"gte": 30, "lte": 40}
    }
  }
}

11. Fuzzy Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {
    "fuzzy": {
      "name": {"value": "莉莉", "fuzziness": 1}
    }
  }
}

12. Single‑Field Sorting

Method: POST

URL: http://localhost:9200/users/_search

{
  "sort": [{"age": {"order": "desc"}}]
}

13. Multi‑Field Sorting

Method: POST

URL: http://localhost:9200/users/_search

{
  "sort": [
    {"age": {"order": "desc"}},
    {"_score": {"order": "asc"}}
  ]
}

14. Highlight Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": {"match": {"name": "莉莉"}},
  "highlight": {
    "pre_tags": "<font color='read'>",
    "post_tags": "</font>",
    "fields": {"name": {}}
  }
}

15. Pagination Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "sort": {"age": {"order": "desc"}},
  "from": 0,
  "size": 2
}

16. Max Aggregation

Method: POST

URL: http://localhost:9200/users/_search

{
  "aggs": {"max_age": {"max": {"field": "age"}}},
  "size": 0
}

17. Average Aggregation

Method: POST

URL: http://localhost:9200/users/_search

{
  "aggs": {"avg_age": {"avg": {"field": "age"}}},
  "size": 0
}

18. Stats Aggregation

Method: POST

URL: http://localhost:9200/users/_search

{
  "aggs": {"stats_age": {"stats": {"field": "age"}}},
  "size": 0
}

19. Terms (Bucket) Aggregation

Method: POST

URL: http://localhost:9200/users/_search

{
  "aggs": {"age_groupby": {"terms": {"field": "age"}}},
  "size": 0
}

20. SQL‑style Query

Method: POST

URL: http://localhost:9200/users/_search

{
  "query": "select * from users"
}

Analyzer

1. Standard Analyzer (IK Max Word)

Method: POST

URL: http://localhost:9200/_analyze

{
  "analyzer": "ik_max_word",
  "text": "我是中国人"
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

REST APIQuery DSLSearchaggregation
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.