Elasticsearch & Full Text Search Systems · Lektion

Term- und Match-Abfragen

Lernen Sie, `term`-Abfragen für den exakten Abgleich von Werten und `match`-Abfragen für Volltextanalyse und Relevanzbewertung zu verwenden.

Lektion 2 von 411 Schritte

Term- und Match-Abfragen ist eine kostenlose Elasticsearch & Full Text Search Systems-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Elasticsearch & Full Text Search Systems-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Elasticsearch & Full Text Search Systems-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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!

Kostenlos starten

Lerne Elasticsearch & Full Text Search Systems mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Term- und Match-Abfragen“ kostenlos?

Ja — der vollständige Text von „Term- und Match-Abfragen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Elasticsearch & Full Text Search Systems-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Elasticsearch & Full Text Search Systems-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Term- und Match-Abfragen“?

Lernen Sie, `term`-Abfragen für den exakten Abgleich von Werten und `match`-Abfragen für Volltextanalyse und Relevanzbewertung zu verwenden. Du übst Elasticsearch & Full Text Search Systems mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Elasticsearch & Full Text Search Systems zu starten?

Keine Vorkenntnisse erforderlich. Elasticsearch & Full Text Search Systems auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Term- und Match-Abfragen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Elasticsearch & Full Text Search Systems-Lektion Code schreiben und ausführen?

Ja. Jede Elasticsearch & Full Text Search Systems-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in die Query DSL
  2. Term- und Match-Abfragen
  3. Abfragen mit Bool kombinieren
  4. Filter, Wertebereiche und Abfragekontext
← Zurück zu Elasticsearch & Full Text Search Systems