Term 与 Match 查询
学习使用 `term` 查询进行精确值匹配,以及使用 `match` 查询进行全文分析和相关性评分。
Term 与 Match 查询 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Intro to Term & Match Queries
Welcome to the fundamentals of querying data in Elasticsearch! Today, we'll explore two essential query types: term and match.
Understanding their differences is crucial for effective search. We'll learn when to use each to get exactly the results you need, whether it's an exact value or a flexible full-text search.
Indexing Sample Documents
Before we query, let's index some simple documents into an index called products. We'll use these examples throughout the lesson.
Run these curl commands in your terminal (assuming Elasticsearch is running on localhost:9200):
curl -X PUT "localhost:9200/products/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Laptop Pro X1",
"category": "Electronics",
"description": "A powerful and lightweight laptop for professionals.",
"price": 1200
}'
curl -X PUT "localhost:9200/products/_doc/2?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Wireless Mouse",
"category": "Accessories",
"description": "Ergonomic wireless mouse with long battery life.",
"price": 25
}'
curl -X PUT "localhost:9200/products/_doc/3?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Gaming Keyboard",
"category": "Electronics",
"description": "Mechanical keyboard for intense gaming sessions.",
"price": 90
}'`term` Query: Exact Matching
The term query is used for finding exact values in a field. It looks for a precise match without any text analysis (like lowercasing or stemming).
- It's ideal for structured data like product IDs, categories, tags, or status fields.
- It works best with fields that are mapped as
keyword(non-analyzed strings) or numeric types.
`term` Query in Action
Let's find all products with the exact category "Electronics". Notice we're querying category.keyword because the default text field would be analyzed.
Try this curl command:
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"category.keyword": "Electronics"
}
}
}'Understanding .keyword
When you index a string field (like category), Elasticsearch often creates two versions by default:
category: Atextfield, which is analyzed (broken into words, lowercased).category.keyword: Akeywordfield, which is treated as a single, exact string.
The term query expects an exact match, so using the .keyword sub-field ensures your query string isn't analyzed and matches the exact stored value.
`match` Query: Full-Text Power
The match query is your go-to for full-text search. Unlike term, it's designed to be smart about text.
- It performs text analysis on your search query.
- It breaks your query string into individual terms, lowercases them, and sometimes stems them (e.g., "running" -> "run").
- It then finds documents that contain these analyzed terms, and also assigns a relevancy score.
`match` Query in Action
Let's search for products with "powerful laptop" in their description. Even if the document contains "A powerful and lightweight laptop", the match query will find it.
Try this curl command:
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"description": "powerful laptop"
}
}
}'The Magic of Text Analysis
When you use a match query, Elasticsearch's text analysis process kicks in:
- Tokenization: "powerful laptop" becomes "powerful" and "laptop".
- Lowercasing: "Powerful" becomes "powerful".
- Stop Words: Common words like "a", "the" might be removed (depending on the analyzer).
This makes match queries highly flexible for natural language search, finding relevant results even with minor variations in phrasing or case.
`term` vs `match`: Key Differences
Here's a quick summary of when to use each query type:
termquery:
- For exact value matching.
- Does NOT perform text analysis on query string.
- Best forkeywordfields, IDs, precise filters.matchquery:
- For full-text search.
- PERFORMS text analysis on query string.
- Best fortextfields, search bars, descriptive content.
Query Understanding Check
Which of the following statements are true regarding term and match queries in Elasticsearch?
Recap: `term` & `match`
Great job! You've learned the fundamental difference between term and match queries.
termis for surgical, exact value searches.matchis for flexible, full-text searches leveraging powerful text analysis.
Knowing when to use each is key to building effective search experiences. Next, we'll explore how to combine these and other queries to create even more sophisticated search logic!
常见问题解答
「Term 与 Match 查询」课时是免费的吗?
是的 — 「Term 与 Match 查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「Term 与 Match 查询」这节课中我会学到什么?
学习使用 `term` 查询进行精确值匹配,以及使用 `match` 查询进行全文分析和相关性评分。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「Term 与 Match 查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Query DSL 简介
- Term 与 Match 查询
- 使用 Bool 组合查询
- 过滤、范围与查询上下文