Elasticsearch & Full Text Search Systems · Lekcja

Łączenie zapytań za pomocą bool

Poznaj zapytanie `bool`, które pozwala łączyć wiele zapytań za pomocą klauzul `must`, `should`, `must_not` i `filter` w celu tworzenia złożonej logiki.

Lekcja 3 z 412 kroki

Łączenie zapytań za pomocą bool to bezpłatna lekcja Elasticsearch & Full Text Search Systems na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Elasticsearch & Full Text Search Systems, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Combine Search Logic

Welcome to combining queries! Up until now, we've looked at single search conditions. But real-world searches often need multiple criteria.

Elasticsearch's bool query is your go-to for building complex search logic. It lets you combine several queries using AND, OR, and NOT conditions.

Anatomy of a Bool Query

A bool query acts like a container for other queries. It has four main clauses:

  • must: All queries inside must match (AND).
  • should: At least one query inside should match (OR).
  • must_not: Queries inside must NOT match (NOT).
  • filter: Queries inside must match, but don't affect relevancy score.

We'll explore each one!

`must`: All Conditions Must Match

The must clause is like an "AND" operator. All queries listed within must must be true for a document to be considered a match.

Documents that satisfy all must clauses contribute positively to the relevancy score.

Example: `must` Clause

This query finds products that are both "laptop" AND "gaming".

{
  "query": {
    "bool": {
      "must": [
        { "match": { "category": "laptop" } },
        { "match": { "tags": "gaming" } }
      ]
    }
  }
}

`should`: At Least One Condition

The should clause is like an "OR" operator. If a document matches any of the queries listed in should, it's considered a potential match.

The more should queries a document matches, the higher its relevancy score usually is.

By default, if there are no must clauses, at least one should clause must match for a document to be returned.

Example: `should` Clause

This query finds products that are either "laptop" OR "tablet".

{
  "query": {
    "bool": {
      "should": [
        { "match": { "category": "laptop" } },
        { "match": { "category": "tablet" } }
      ]
    }
  }
}

`must_not`: Exclude Documents

The must_not clause is like a "NOT" operator. Documents matching any query inside must_not will be excluded from the results.

Queries in must_not do not contribute to the relevancy score.

Example: `must_not` Clause

This query finds all products EXCEPT those in the "refurbished" category.

{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "category": "refurbished" } }
      ]
    }
  }
}

`filter`: Fast, Non-Scoring AND

The filter clause is similar to must in that all queries within it must match. However, there's a key difference:

  • No Scoring: Filter queries don't affect the relevancy score of documents.
  • Caching: Filter queries are often cached, making them very fast for frequently used criteria.

Use filter for "yes/no" conditions where you just want to include or exclude documents without influencing their rank.

Advanced: Multiple Clauses

You can combine all these clauses within a single bool query to build powerful, precise searches. This example finds:

  • Products with "laptop" in the name (must)
  • THAT are either "gaming" OR "ultrabook" (should)
  • BUT are NOT "refurbished" (must_not)
  • AND have a status of "available" (filter)
{
  "query": {
    "bool": {
      "must": { "match": { "name": "laptop" } },
      "should": [
        { "match": { "tags": "gaming" } },
        { "match": { "tags": "ultrabook" } }
      ],
      "must_not": { "term": { "condition": "refurbished" } },
      "filter": { "term": { "status.keyword": "available" } }
    }
  }
}

Complex Query Logic

Consider a bool query designed to find articles:

  • About "Elasticsearch" (must)
  • Published in "2023" OR "2024" (should)
  • But NOT written by "John Doe" (must_not)

Which of the following statements is TRUE regarding this query's behavior?

Recap: Bool Query Power

Great job! You've learned how to combine simple queries into powerful, complex search logic using Elasticsearch's bool query.

  • must: All conditions must be met (AND).
  • should: At least one condition should be met (OR).
  • must_not: Documents must NOT match these conditions (NOT).
  • filter: Conditions must be met, but don't affect relevancy (fast & cached).

Understanding bool queries is crucial for building precise and effective search applications!

Bezpłatny start

Ucz się Elasticsearch & Full Text Search Systems dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Łączenie zapytań za pomocą bool” jest bezpłatna?

Tak — pełny tekst „Łączenie zapytań za pomocą bool” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Elasticsearch & Full Text Search Systems, przejdź na CoddyKit PRO. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.

Co nauczysz się w „Łączenie zapytań za pomocą bool”?

Poznaj zapytanie `bool`, które pozwala łączyć wiele zapytań za pomocą klauzul `must`, `should`, `must_not` i `filter` w celu tworzenia złożonej logiki. Ćwiczysz Elasticsearch & Full Text Search Systems z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Elasticsearch & Full Text Search Systems?

Nie wymagamy żadnego doświadczenia. Elasticsearch & Full Text Search Systems w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Łączenie zapytań za pomocą bool”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Elasticsearch & Full Text Search Systems?

Tak. Każda lekcja Elasticsearch & Full Text Search Systems zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do Query DSL
  2. Zapytania term i match
  3. Łączenie zapytań za pomocą bool
  4. Filtrowanie, zakresy i kontekst zapytania
← Powrót do Elasticsearch & Full Text Search Systems