Elasticsearch & Full Text Search Systems · บทเรียน

การสอบถามแบบ Term และ Match

เรียนรู้การใช้การสอบถาม `term` เพื่อจับคู่ค่าที่ตรงกันทุกประการ และการสอบถาม `match` เพื่อวิเคราะห์ข้อความแบบเต็มและคำนวณคะแนนความเกี่ยวข้อง

บทเรียน 2 จาก 411 ขั้นตอน

การสอบถามแบบ Term และ Match เป็นบทเรียน Elasticsearch & Full Text Search Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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: A text field, which is analyzed (broken into words, lowercased).
  • category.keyword: A keyword field, 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:

  1. Tokenization: "powerful laptop" becomes "powerful" and "laptop".
  2. Lowercasing: "Powerful" becomes "powerful".
  3. 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:

  • term query:
    - For exact value matching.
    - Does NOT perform text analysis on query string.
    - Best for keyword fields, IDs, precise filters.
  • match query:
    - For full-text search.
    - PERFORMS text analysis on query string.
    - Best for text fields, 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.

  • term is for surgical, exact value searches.
  • match is 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!

เริ่มต้นได้ฟรี

เรียนรู้ Elasticsearch & Full Text Search Systems ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสอบถามแบบ Term และ Match” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสอบถามแบบ Term และ Match” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elasticsearch & Full Text Search Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elasticsearch & Full Text Search Systems มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสอบถามแบบ Term และ Match”

เรียนรู้การใช้การสอบถาม `term` เพื่อจับคู่ค่าที่ตรงกันทุกประการ และการสอบถาม `match` เพื่อวิเคราะห์ข้อความแบบเต็มและคำนวณคะแนนความเกี่ยวข้อง คุณปฏิบัติ Elasticsearch & Full Text Search Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elasticsearch & Full Text Search Systems หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elasticsearch & Full Text Search Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การสอบถามแบบ Term และ Match” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elasticsearch & Full Text Search Systems นี้ได้ไหม

ได้ บทเรียน Elasticsearch & Full Text Search Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. บทนำสู่ Query DSL
  2. การสอบถามแบบ Term และ Match
  3. การรวมการสอบถามด้วย Bool
  4. การกรอง ช่วงค่า และบริบทการสืบค้น
← กลับไปที่ Elasticsearch & Full Text Search Systems