news 2026/6/10 1:48:01

Elasticsearch(3) show me some examples

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 calledstore_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:

{"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 abool(Boolean) query.

Inside aboolquery, we use two main clauses:

  • must: The resultsmustmatch this text search (calculates a relevance score).
  • filter: The resultsmustmatch 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 costunder $250.

The Actual Search Query

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.keywordversion of a text field.

Example 2: Aggregations (Getting Analytics Data)

Aggregations don’t just find documents; they calculate dataaboutyour documents. Think of it like aGROUP BYandAVG()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 eachcategory? (Bucket Aggregation)
  2. What is theaverage_priceof the products in each of those categories? (Metric Aggregation)

The Actual Search Query

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 documentsplusthe 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 arein stock, but youalsowant to show them a breakdown of which brands make those gadgets and how much they cost on average.

The Actual Search Query

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 blockthat 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!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 1:46:22

探果Tengo全场景AI生态平台:AI不止会聊天,更能替你干活

2026年了,如果你对AI的认知还停留在"问它一个问题,它回你一段话",那你可能已经落后了一个时代。 最近一个叫探果Tengo全场景AI生态平台的产品火了。它不是又一个聊天机器人,而是一个能真正替你跑腿、替你干活的桌面智能…

作者头像 李华
网站建设 2026/6/10 1:35:06

2026 年全球新型网络钓鱼形态与全域防御技术研究

摘要 全球网络诈骗犯罪呈现产业化、技术化、跨区域发展态势,诈骗手段持续迭代升级,传统安全防护机制与用户风险认知已难以适配新型威胁。基于谷歌 2026 年 6 月全球诈骗风险预警报告,本文系统梳理当前四大主流网络诈骗类型,分别为…

作者头像 李华
网站建设 2026/6/10 1:30:23

专业的大玻璃封阳台哪家强

当下家居装修,“大板玻璃封阳台”已成为不可逆的潮流趋势。通透视野与极简线条不仅拉高层高感,更能让室内光线实现质的飞跃。但大量业主在咨询时都会问同一个问题:大玻璃封阳台到底该选哪家? 市场上从本土定制店到全国连锁大牌林立…

作者头像 李华
网站建设 2026/6/10 1:27:15

2026年AI问答优化服务商有哪些

2026年AI问答优化服务商有哪些,这样看才不容易选偏 2026年再聊“AI问答优化服务商有哪些”,这事真不是图新鲜了。 中国互联网络信息中心发布的第57次《中国互联网络发展状况统计报告》提到,截至2025年12月,我国生成式人工智能用户…

作者头像 李华