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

<code>{
  "query": {
    "match_all": {}
  }
}</code>

2. Match Query

Method: POST

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

<code>{
  "query": {
    "match": {
      "name": "莉莉"
    }
  }
}</code>

3. Multi‑Field Match Query

Method: POST

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

<code>{
  "query": {
    "multi_match": {
      "query": "张三",
      "fields": ["name","sex"]
    }
  }
}</code>

4. Term (Exact) Query

Method: POST

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

<code>{
  "query": {
    "term": {
      "name": "joke"
    }
  }
}</code>

5. Multi‑Term Exact Query

Method: POST

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

<code>{
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}</code>

6. Retrieve Specific Fields

Method: POST

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

<code>{
  "_source": ["name","sex"],
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}</code>

7. Include Fields (Source Filtering)

Method: POST

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

<code>{
  "_source": {
    "includes": ["name","age"]
  },
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}</code>

8. Exclude Fields (Source Filtering)

Method: POST

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

<code>{
  "_source": {
    "excludes": ["name","age"]
  },
  "query": {
    "terms": {
      "name": ["莉","莉"]
    }
  }
}</code>

9. Bool (Combined) Query

Method: POST

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

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

10. Range Query

Method: POST

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

<code>{
  "query": {
    "range": {
      "age": {"gte": 30, "lte": 40}
    }
  }
}</code>

11. Fuzzy Query

Method: POST

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

<code>{
  "query": {
    "fuzzy": {
      "name": {"value": "莉莉", "fuzziness": 1}
    }
  }
}</code>

12. Single‑Field Sorting

Method: POST

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

<code>{
  "sort": [{"age": {"order": "desc"}}]
}</code>

13. Multi‑Field Sorting

Method: POST

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

<code>{
  "sort": [
    {"age": {"order": "desc"}},
    {"_score": {"order": "asc"}}
  ]
}</code>

14. Highlight Query

Method: POST

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

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

15. Pagination Query

Method: POST

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

<code>{
  "sort": {"age": {"order": "desc"}},
  "from": 0,
  "size": 2
}</code>

16. Max Aggregation

Method: POST

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

<code>{
  "aggs": {"max_age": {"max": {"field": "age"}}},
  "size": 0
}</code>

17. Average Aggregation

Method: POST

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

<code>{
  "aggs": {"avg_age": {"avg": {"field": "age"}}},
  "size": 0
}</code>

18. Stats Aggregation

Method: POST

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

<code>{
  "aggs": {"stats_age": {"stats": {"field": "age"}}},
  "size": 0
}</code>

19. Terms (Bucket) Aggregation

Method: POST

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

<code>{
  "aggs": {"age_groupby": {"terms": {"field": "age"}}},
  "size": 0
}</code>

20. SQL‑style Query

Method: POST

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

<code>{
  "query": "select * from users"
}</code>

Analyzer

1. Standard Analyzer (IK Max Word)

Method: POST

URL: http://localhost:9200/_analyze

<code>{
  "analyzer": "ik_max_word",
  "text": "我是中国人"
}</code>
ElasticsearchREST 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

login 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.