การเน้นผลการค้นหา
เพิ่มการเน้นในผลการค้นหาเพื่อทำให้คำที่ตรงกันในเอกสารที่ส่งกลับมามองเห็นได้ชัดเจน ช่วยยกระดับประสบการณ์ผู้ใช้
การเน้นผลการค้นหา เป็นบทเรียน Elasticsearch & Full Text Search Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elasticsearch & Full Text Search Systems และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elasticsearch & Full Text Search Systems มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Highlight Results?
Imagine searching for a recipe and seeing 'chicken' highlighted exactly where it appears in the ingredients and steps. That's highlighting!
It visually emphasizes the matching terms in your search results, making it much easier for users to quickly scan and find relevant information.
- Improved User Experience: Users quickly spot why a document is relevant.
- Contextual Clues: Provides snippets of text around the match, giving context.
- Faster Information Retrieval: Reduces time spent reading irrelevant parts.
Your First Highlight
Adding basic highlighting to your Elasticsearch search is straightforward. You just need to include a highlight block in your search request.
This block specifies which fields you want to highlight. By default, Elasticsearch wraps the matching terms with <em> and </em> tags.
curl -X GET "localhost:9200/products/_search?pretty" \
-H 'Content-Type: application/json' \
-d'
{
"query": {
"match": {
"description": "lightweight laptop"
}
},
"highlight": {
"fields": {
"description": {}
}
}
}'Highlighting Specific Fields
In the previous example, we told Elasticsearch to highlight matches found in the description field.
The highlight.fields object is where you list all the fields you want to apply highlighting to. For each field, you can provide an empty object {} for default behavior, or specify custom options.
Only fields that are indexed for full-text search (like text fields) can be highlighted effectively.
Highlighting Multiple Fields
Often, you'll want to highlight matching terms across several fields within the same document, such as a product's title and its content.
Simply add more fields to the fields object in your highlight section. Elasticsearch will process each field independently.
curl -X GET "localhost:9200/articles/_search?pretty" \
-H 'Content-Type: application/json' \
-d'
{
"query": {
"match": {
"text": "Elasticsearch indexing"
}
},
"highlight": {
"fields": {
"title": {},
"content": {}
}
}
}'Customizing Highlight Tags
The default <em> tags are fine, but you might want to use different HTML tags or CSS classes for styling your highlighted terms.
You can change these using the pre_tags and post_tags parameters within your highlight block. These parameters accept arrays of strings.
curl -X GET "localhost:9200/products/_search?pretty" \
-H 'Content-Type: application/json' \
-d'
{
"query": {
"match": {
"name": "wireless headphones"
}
},
"highlight": {
"pre_tags": ["<span class=\"highlight\">"],
"post_tags": ["</span>"],
"fields": {
"name": {}
}
}
}'Controlling Snippet Length (fragment_size)
When a document is very long, you usually don't want to return the entire field with highlights. Instead, you want short, relevant snippets.
The fragment_size parameter controls the maximum length (in characters) of the highlighted fragments. Elasticsearch tries to break fragments at sentence boundaries or natural breaks.
curl -X GET "localhost:9200/blogs/_search?pretty" \
-H 'Content-Type: application/json' \
-d'
{
"query": {
"match": {
"body": "data analytics"
}
},
"highlight": {
"fragment_size": 100,
"fields": {
"body": {}
}
}
}'Multiple Fragments & No Matches
You can also control the number of snippets returned per field using number_of_fragments. Set it to 0 to return the entire field content as a single fragment (if fragment_size is also 0).
What if there are no matches in a field, but you still want to see its content? Use no_match_size to specify the length of the fragment to return if no matches are found. By default, if there's no match, no fragment is returned for that field.
Advanced Fragment Boundaries
For even more precise control over how fragments are generated, you can use boundary_scanner, boundary_chars, and boundary_max_scan.
boundary_scanner: Defines how fragments are split (e.g.,sentence,word,chars).boundary_chars: Custom characters to use as fragment boundaries whenboundary_scannerischars.boundary_max_scan: How far to scan for boundary characters.
These are useful for languages without clear sentence structures or for specific content types.
Highlighting from Matched Fields
Sometimes, your search query matches in one field (e.g., content), but you want to highlight the corresponding terms in another field (e.g., a shorter summary or title) for display.
The matched_fields parameter allows you to specify a list of fields that will be used to generate highlights for the current field. This is powerful for showing concise, highlighted snippets from a related, more prominent field.
curl -X GET "localhost:9200/documents/_search?pretty" \
-H 'Content-Type: application/json' \
-d'
{
"query": {
"match": {
"full_text": "distributed systems"
}
},
"highlight": {
"fields": {
"abstract": {
"matched_fields": ["full_text"],
"fragment_size": 100
}
}
}
}'Highlighting Options Quiz
Which of the following parameters can be used to customize how Elasticsearch generates search result highlights?
Recap: Emphasize Key Finds
You've learned how to bring your search results to life with highlighting! This powerful feature is crucial for improving user experience by visually emphasizing matching terms.
- We started with basic highlighting using the
highlightblock. - You can specify
fieldsto highlight and customizepre_tags/post_tags. fragment_sizeandnumber_of_fragmentsgive you control over snippet length and count.- Advanced options like
boundary_scannerandmatched_fieldsprovide fine-grained control for complex scenarios.
Go forth and make your search results sparkle!
เรียนรู้ Elasticsearch & Full Text Search Systems ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การเน้นผลการค้นหา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเน้นผลการค้นหา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elasticsearch & Full Text Search Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elasticsearch & Full Text Search Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเน้นผลการค้นหา”
เพิ่มการเน้นในผลการค้นหาเพื่อทำให้คำที่ตรงกันในเอกสารที่ส่งกลับมามองเห็นได้ชัดเจน ช่วยยกระดับประสบการณ์ผู้ใช้ คุณปฏิบัติ Elasticsearch & Full Text Search Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elasticsearch & Full Text Search Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elasticsearch & Full Text Search Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การเน้นผลการค้นหา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Elasticsearch & Full Text Search Systems นี้ได้ไหม
ได้ บทเรียน Elasticsearch & Full Text Search Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การค้นหาวลีและความใกล้เคียง
- การสอบถามแบบคลุมเครือและไวลด์การ์ด
- การเน้นผลการค้นหา
- การรวมผลสำหรับการค้นหาแบบแบ่งหมวด