Elasticsearch(3) show me some examples

Let's clear up that confusion right now. Seeing the actual query structure makes a world of difference.

To make this completely clear, let's pretend we have an index called store_products. Before we search, let's look at the type of data we are working with. Imagine our index has documents that look like this:

json 复制代码
{
  "name": "Wireless Noise-Canceling Headphones",
  "brand": "Sony",
  "category": "Electronics",
  "price": 199.99,
  "in_stock": true,
  "tags": ["audio", "wireless", "gadget"]
}

Now, let's look at three detailed, real-world search examples, ranging from simple to advanced.


Example 1: The "Bool" Query (Combining Queries and Filters)

In the real world, you rarely just search for a keyword. Usually, a user types a word, and then clicks some checkboxes to filter the results. In Elasticsearch, we do this using a bool (Boolean) query.

Inside a bool query, we use two main clauses:

  • must: The results must match this text search (calculates a relevance score).
  • filter: The results must match this exact criteria (fast, cached, does not affect the score).

The Scenario

A user searches your store for the word "wireless" , but they only want items in the "Electronics" category that cost under $250.

json 复制代码
GET /store_products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "wireless"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "category.keyword": "Electronics"
          }
        },
        {
          "range": {
            "price": {
              "lt": 250.00
            }
          }
        }
      ]
    }
  }
}

💡 Crucial Detail: What is .keyword?

Notice that for the category filter, I wrote "category.keyword" instead of just "category".

  • category (Text field) is broken down into lowercase tokens for searching (e.g., "electronics").
  • category.keyword (Keyword field) treats the entire string as one exact unit ("Electronics"). When doing exact filters, always use the .keyword version of a text field.

Example 2: Aggregations (Getting Analytics Data)

Aggregations don't just find documents; they calculate data about your documents. Think of it like a GROUP BY and AVG() in SQL.

The Scenario

You want to build a dashboard sidebar. You need Elasticsearch to look at all your products and tell you:

  1. How many products are in each category? (Bucket Aggregation)
  2. What is the average_price of the products in each of those categories? (Metric Aggregation)
json 复制代码
GET /store_products/_search
{
  "size": 0, 
  "aggs": {
    "group_by_category": {
      "terms": {
        "field": "category.keyword"
      },
      "aggs": {
        "average_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

💡 Crucial Detail: Why "size": 0?

By default, a search returns the top 10 matching documents plus the aggregation data. By setting "size": 0, we tell Elasticsearch: "I don't want to see individual products. I only want the analytical totals." This makes the query incredibly fast.


Example 3: Putting It All Together (Search, Filter, and Aggregate)

This is the ultimate intermediate query. You can combine everything we just learned into a single request.

The Scenario

A user searches for the word "gadget" . You want to show them the matching items that are in stock , but you also want to show them a breakdown of which brands make those gadgets and how much they cost on average.

json 复制代码
GET /store_products/_search
{
  "size": 5,
  "query": {
    "bool": {
      "must": [
        { "match": { "tags": "gadget" } }
      ],
      "filter": [
        { "term": { "in_stock": true } }
      ]
    }
  },
  "aggs": {
    "brands_breakdown": {
      "terms": {
        "field": "brand.keyword"
      },
      "aggs": {
        "avg_brand_price": {
          "avg": { "field": "price" }
        }
      }
    }
  }
}

What Elasticsearch will return:

  1. The top 5 documents ("size": 5) that have the tag "gadget" and are currently in stock, ranked by how relevant they are to the search.
  2. An aggregation block that only looks at those specific matching gadgets, groups them by brand, and calculates the average price per brand.

If you paste these templates into Elasticvue or Kibana, you can easily change the field names to match your own data!

相关推荐
ApacheSeaTunnel5 小时前
Apache SeaTunnel AI CLI Benchmark:7 款大模型、100 个 ETL 任务实测,谁真正能跑起来?
大数据·ai·开源·大模型·数据集成·cli·seatunnel·技术分享·数据同步
电商API_180079052475 小时前
企业ERP进销存场景|京东商品详情接口自动同步方案|凭证鉴权批量调用技术实操
大数据·运维·人工智能·爬虫·数据挖掘·网络爬虫
小稻穗6 小时前
市场调研样本选型坐标系:2026六家国内样本平台能力横向校验
大数据·运维·数据分析
极光代码工作室6 小时前
基于Spark的日志监控与分析平台
大数据·hadoop·python·spark·数据可视化
wuhanzhanhui7 小时前
从高强钢到碳纤维!2026武汉汽车材料轻量化制造技术展会,重塑未来造车新范式
大数据·汽车·制造
lupai7 小时前
手机在网状态查询 API 新手实战指南
大数据·python·智能手机
AI_Auto7 小时前
工业与AI融合应用 | 四个实战用例!机械装备行业AI+数字孪生+机器人落地全景
大数据·人工智能·机器人·制造
fengyehongWorld8 小时前
Jenkins 安装与简单配置
运维·jenkins
小羊Yveesss8 小时前
模板建站哪个平台好?模板数量之外还要比较编辑与SEO能力
大数据·人工智能·小程序
千瓜8 小时前
用户洞察:负鼠走红?解读新世代“动物人格”
大数据·人工智能·数据分析·生活·新媒体