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!

相关推荐
CHENGQUAN_kenan5 分钟前
亚马逊境外店铺利润境内回流:税务规则、佛山实操与自查要点
大数据·经验分享·教育电商
Blossom i25 分钟前
Python+spark2.0+Hadoop机器学习与大数据实战第十一章:Python Spark的集成开发环境
大数据·hadoop·spark
逸模31 分钟前
从2-3天到30分钟——逸模“秒级升维“如何重构出图效率
大数据·人工智能·工程·公装·连锁店
MC皮蛋侠客43 分钟前
TDengine C++ 系列(4):时序查询——窗口聚合、插值、连接与时序函数
大数据·c++·tdengine
科技风向标go1 小时前
2026户外太阳能监控怎么选不踩坑?户外(格行AOV+黑光)、工程(海康大华)、生态(小米萤石)——三大派系技术路线全解析
大数据·人工智能·智能家居·监控·户外安防
环球科讯1 小时前
金融赋能焕新居——建行广东江门分行启动家装补贴季助力消费升温
大数据·人工智能·物联网
LONGZETECH2 小时前
无人机实训高成本痛点解法:虚拟仿真实现 70% 耗材损耗下降
大数据·算法·unity·架构·无人机
2401_865261632 小时前
亦唐科技(YIKTANG):创新引领国产贴片机发展,稳居行业领军地位
大数据·人工智能·物联网
阿部多瑞 ABU2 小时前
私域道德的“立法”与公共秩序的失序——法律-道德异化悖论研究
大数据·人工智能
研华科技Advantech2 小时前
【案例分享】从单点突破到全场景复制:研华赋能药机装备迈向高效合规新范式
大数据·人工智能·边缘计算·工业设备升级