Skip to content
Educora
AdvancedGrade 1122 min41 / 59

Searching the internet: search engines and queries

Learn how a search engine works with its index, how to build queries with AND, OR and NOT, and how to count the pages found for a query with Euler–Venn diagrams.

Check yourself
In this lesson you will learn
  • explain how a search engine works: crawler, index and ranking
  • write queries with AND, OR, NOT, brackets and quotes, and order queries by the number of pages found
  • draw Euler–Venn diagrams for two and three keywords and apply the inclusion–exclusion formula
  • solve DİM’s coded tasks on search queries step by step

Aysel is preparing a project about Caspian seals. When she types “Caspian” into a search engine, millions of pages are found — nobody can read them all. With “Caspian seal” there are far fewer results, and adding the word “protection” narrows them even more. A well-built query finds what you need in minutes instead of hours. DİM’s entrance exam tests this topic too: in three of the four exams of 2025–2026 there was a coded task in which a table lists some queries with the number of pages found, and you must find the result of another query.

How a search engine works

Definition
Search engine (search server)

A service that finds web pages matching the keywords a user types and shows them as a list. Examples: Google, Bing, Yandex.

At the moment you send a query, the search engine does not scan the whole internet — that would take hours or even days. It looks in an index prepared in advance. The work goes like this:

  1. 1
    The crawler visits pages

    A special program — the crawler (also called a spider or robot) — moves from page to page along links and downloads the text of each page.

  2. 2
    Indexing

    For every word the engine builds a list of the pages where it occurs. It is like the index at the back of a book: “Nizami — pp. 12, 48, 95”.

  3. 3
    Processing the query

    The keywords of the query are looked up in the index, and their page lists are intersected or joined according to the Boolean operators.

  4. 4
    Ranking

    The pages found are ordered by how well they match: is the keyword in the title, how many sites link to the page, and so on. The results page also shows an approximate count, such as “about 120,000 results”.

So every keyword gives a set of pages: all the pages where the word occurs. The Boolean operators in a query are simply operations on these sets. The rest of the lesson is built on this simple idea.

The query language: AND, OR, NOT

Keywords can be combined with Boolean operators. DİM tasks write them in English — AND, OR, NOT; some books use the signs &, | and ~ instead. You know these operations from the lesson “Boolean logic and logic gates”: here they act on sets of pages instead of statements.

OperationWritten asPages foundNumber of pages
AND (intersection)A AND B, A & Bpages with both wordsdecreases
OR (union)A OR B, A | Bpages with at least one of the wordsincreases
NOT (negation)A AND NOT B, A & ~Bpages with A but without Bdecreases
Quotation marks"A B"pages where the words stand side by side in exactly this orderdecreases

Without brackets NOT is done first, then AND, and OR last. For example, the query A OR B AND C means A OR (B AND C). If you need another order, write brackets: (A OR B) AND C. Real search engines also have their own rules: in Google, for example, words separated by spaces are joined with AND, OR is written in capital letters, a minus sign before a word (-advert) excludes it, and quotation marks search for an exact phrase.

Ordering queries

Four queries were sent to a search server:
1) chess OR draughts
2) chess AND draughts AND tournament
3) chess
4) chess AND tournament
List the query numbers in increasing order of the number of pages found.

Show solution
Think of each query as a set of pages.
Every page of query 2 contains all three words, so it also contains “chess” and “tournament”: these pages lie inside the result of query 4.
Every page of query 4 contains “chess”: it is part of query 3.
Every page with “chess” also belongs to query 1, and query 1 adds the pages with “draughts”.
So 2 ⊆ 4 ⊆ 3 ⊆ 1.
Answer: 2, 4, 3, 1.

Euler–Venn diagrams: two keywords

Draw the pages found for each keyword as a circle. The common part of the circles is the pages with both words (AND), the whole area covered by the two circles is the result of OR, and the part of a circle outside the other circle is obtained with NOT. Such a picture is called an Euler–Venn diagram.

ABA ANDNOT BA ANDBB ANDNOT AA OR B
Two keywords split the diagram into three parts.

When we add n(A) and n(B) to count the pages of A OR B, the common part is counted twice: it lies inside circle A and inside circle B. So we subtract it once:

n(A OR B) = n(A) + n(B) − n(A AND B)
where:
  • n(A), n(B)the numbers of pages found for queries A and B
  • n(A AND B)the number of pages with both words
  • n(A OR B)the number of pages with at least one of the words

The inclusion–exclusion formula for two sets. If three of the four quantities are known, the fourth can be found.

n(A AND NOT B) = n(A) − n(A AND B)
where:
  • n(A AND NOT B)the number of pages with A but without B

The common part is removed from circle A — not the whole of B!

Two keywords

The table shows queries and the numbers of pages found:
football — 520
volleyball — 380
football OR volleyball — 760
How many pages are found for a) football AND volleyball, b) football AND NOT volleyball, c) volleyball AND NOT football?

Show solution
a) n(football AND volleyball) = 520 + 380 − 760 = 140.
b) n(football AND NOT volleyball) = 520 − 140 = 380.
c) n(volleyball AND NOT football) = 380 − 140 = 240.
Check: the three parts add up to 380 + 140 + 240 = 760 = n(football OR volleyball). ✓
Finding the unknown from the formula

1) n(A) = 250, n(B) = 400, n(A AND B) = 90. n(A OR B) = ?
2) n(A OR B) = 900, n(A) = 600, n(A AND B) = 150. n(B) = ?
3) n(A) = 330, n(B) = 270, n(A OR B) = 600. n(A AND B) = ? What does it mean?

Show solution
1) n(A OR B) = 250 + 400 − 90 = 560.
2) 900 = 600 + n(B) − 150 ⇒ n(B) = 900 − 600 + 150 = 450.
3) n(A AND B) = 330 + 270 − 600 = 0: there are no common pages, the circles do not overlap.

Three keywords: DİM tasks

With three keywords the three circles split the diagram into 7 parts. Number the parts and write N₁, N₂, …, N₇ for the number of pages in each part. Every query is the sum of some of these parts — that is the whole secret of the task.

ABC1234567
Numbering of the parts: 1–3 — one circle, 4–6 — two circles, 7 — three circles.
QueryParts of the diagram
A1 + 4 + 5 + 7
A AND B4 + 7
A AND B AND C7
(A OR B) AND C5 + 6 + 7
A AND NOT B1 + 5
A OR B OR C1 + 2 + 3 + 4 + 5 + 6 + 7
n(A OR B OR C) = n(A) + n(B) + n(C) − n(A AND B) − n(A AND C) − n(B AND C) + n(A AND B AND C)
where:
  • n(A AND B AND C)the number of pages with all three words (the centre, part 7)

The inclusion–exclusion formula for three sets: subtracting the pairwise intersections removes the centre three times, so it is added back once.

  1. 1
    Draw the diagram

    Draw one circle per keyword and number the parts.

  2. 2
    Rewrite the table in parts

    Write every row of the table as a sum of parts, e.g. n(B AND C) = N₆ + N₇.

  3. 3
    Start from the centre

    Start with the narrowest query (usually the AND of all three words) and find the parts from the inside out.

  4. 4
    Express the asked query

    Decide which parts make up the asked query and add them up.

  5. 5
    Check

    No part may be negative; if possible, check the answer with the formula as well.

Coded task 1

The table shows queries and the numbers of pages found by the search server:
(book OR magazine) AND poetry — 540
book AND poetry — 310
magazine AND poetry — 290
How many pages are found for book AND magazine AND poetry?

Show solution
Let A = book, B = magazine, C = poetry. Every query contains “poetry”, so we only look inside circle C.
n(book AND poetry) = N₅ + N₇ = 310
n(magazine AND poetry) = N₆ + N₇ = 290
n((book OR magazine) AND poetry) = N₅ + N₆ + N₇ = 540
Adding the first two equations counts N₇ twice: (N₅ + N₇) + (N₆ + N₇) = 600.
So N₇ = 600 − 540 = 60.
Answer: 60.
Coded task 2

The table shows queries and the numbers of pages found:
Caspian AND oil AND gas — 90
oil AND gas — 230
Caspian AND gas — 170
How many pages are found for (Caspian OR oil) AND gas?

Show solution
A = Caspian, B = oil, C = gas.
N₇ = 90 (all three words).
n(oil AND gas) = N₆ + N₇ = 230 ⇒ N₆ = 140.
n(Caspian AND gas) = N₅ + N₇ = 170 ⇒ N₅ = 80.
n((Caspian OR oil) AND gas) = N₅ + N₆ + N₇ = 80 + 140 + 90 = 310.
Short way: 170 + 230 − 90 = 310 — the two-set inclusion–exclusion formula inside circle C.
Answer: 310.
Coded task 3: the whole diagram

The table shows queries and the numbers of pages found:
A — 400
B — 500
C — 300
A OR B OR C — 970
A AND B — 100
A AND C — 70
A AND B AND C — 30
How many pages are found for B AND NOT C?

Show solution
B AND NOT C is the part of circle B outside C: N₂ + N₄.
The whole diagram holds 970. Removing circle C (300) leaves N₁ + N₂ + N₄ = 970 − 300 = 670.
Now find N₁ (only A): N₇ = 30; N₄ = 100 − 30 = 70; N₅ = 70 − 30 = 40; N₁ = 400 − 70 − 40 − 30 = 260.
So N₂ + N₄ = 670 − 260 = 410.
Note: B AND C is not in the table, and it is not needed.
Answer: 410.

Query operations match Python’s set operations exactly: & is AND, | is OR, - is AND NOT. Check the formula yourself on a small example (pages are shown as numbers):

Python
football = {1, 2, 3, 4, 5, 6, 7}
volleyball = {5, 6, 7, 8, 9}
basketball = {2, 7, 9, 10}

print('football AND volleyball:', sorted(football & volleyball))
print('football OR volleyball:', sorted(football | volleyball))
print('football AND NOT volleyball:', sorted(football - volleyball))
print('(football OR volleyball) AND basketball:',
      sorted((football | volleyball) & basketball))

# inclusion-exclusion: n(A OR B) = n(A) + n(B) - n(A AND B)
n_or = len(football) + len(volleyball) - len(football & volleyball)
print(n_or, len(football | volleyball))
▸ Expected output
football AND volleyball: [5, 6, 7]
football OR volleyball: [1, 2, 3, 4, 5, 6, 7, 8, 9]
football AND NOT volleyball: [1, 2, 3, 4]
(football OR volleyball) AND basketball: [2, 7, 9]
9 9
Change the sets and try other queries.
Quick practice
  1. 1.n(A) = 300, n(B) = 200, n(A AND B) = 50. n(A OR B) =
  2. 2.n(A) = 300, n(A AND B) = 50. n(A AND NOT B) =
  3. 3.n((A OR B) AND C) = 400, n(A AND C) = 250, n(B AND C) = 230. n(A AND B AND C) =
  4. 4.n(A AND B AND C) = 40, n(A AND C) = 150, n(B AND C) = 110. n((A OR B) AND C) =

Key points

  • A search engine searches not the internet itself but an index collected in advance by crawlers.
  • AND reduces the number of pages (intersection), OR increases it (union), NOT excludes; without brackets the order is NOT, AND, OR.
  • n(A OR B) = n(A) + n(B) − n(A AND B); n(A AND NOT B) = n(A) − n(A AND B).
  • Three keywords split the diagram into 7 parts; every query is a sum of these parts.
  • In a DİM task start from the centre (the AND of all three words), work from the inside out and check that no part is negative.

Check yourself

12 questions. Every correct answer earns XP.

1 / 12
What does a crawler (search robot) do?