what is the dot product
Type a word into a search box and the engine does not compare texts letter by letter. It turns each text into a vector and looks at how much two vectors point the same way. The tool that measures "how much the same way" with a single number is the dot product: a·b = |a||b|cos θ, the lengths of the two vectors times the cosine of the angle between them. Strip off the lengths and only cos θ remains: that is cosine similarity. The same direction means alike, a right angle means unrelated, and opposite directions mean exact opposites. Search, recommendation, and word embeddings all run on this one line.
Keep the green arrow a fixed and handle the blue arrow b with two sliders. One slider changes the angle of b and the other its length. The value of a·b shown below responds to both sliders. Lengthen b and the value grows across the board: the length effect. Turn b and the value is positive when b points the same way, 0 at a right angle, and negative when it points the opposite way: the alignment effect. The color of the readout switches among green, faint, and red with the sign, and the foot of b dropped onto a (the gold dot) moves along too. You can feel with your own hands that a single dot product carries both length and alignment at once.
The dot product can also be seen another way. Lay a along the +x axis with length 2 and turn only the angle of b with the slider: the bar drawn in gold is the shadow of b dropped straight onto a, its projection. Its length is written at the end of the bar. Then a·b is exactly |a| times that projection length. Tilt b down toward a and the shadow lengthens, so the dot product is large; stand b up and the shadow shortens, so the dot product shrinks. At a right angle the shadow is 0, so the dot product is 0 too. Past a right angle the shadow falls on the other side and the dot product turns negative. In the end, the dot product measures how far one vector extends in another's direction, as a length, and multiplies it by |a|.
Look at the sign of the dot product on its own and the rule is very simple. Put a on the +x axis and sweep the angle θ of b from 0 degrees to 360 degrees with the slider: the plane is painted in two regions. The half-plane on the same side as a (θ within 90 degrees) is green, and there the dot product is positive. At exactly 90 degrees and 270 degrees, the right angles, it is 0. The opposite half-plane (between 90 degrees and 270 degrees) is red, so the dot product is negative. Turn b and the label below switches among positive, 0, and negative while the value is shown as well. You see at a glance that one sign decides whether two vectors are on the same side, unrelated, or on opposite sides.
Now strip off the length entirely: cos θ = a·b / (|a||b|). This value always lies between -1 and +1. Keep a fixed and change the angle and length of b with the two sliders, and a decisive scene appears. Lengthen or shorten only b and cos θ does not budge; the needle on the gauge drawn from -1 to +1 below stays where it is. Turn the angle instead and the needle slides right along. Cosine similarity ignores size and looks only at direction. That is why search engines and embeddings judge whether two texts are alike by the direction they point alone, however long or short the texts are.
Finally, catch the moment when the dot product is exactly 0 yourself. a is fixed; drag the blue arrow b around the plane and the value of a·b appears below in real time. As b passes the spot where it makes a right angle with a, the value crosses 0, the arrow lights up in gold, and the word "orthogonal" appears. A dot product of 0 means the two directions are perfectly perpendicular, that is, unrelated and independent. This property is what makes coordinate axes clean. Because the x-axis and the y-axis are orthogonal, moving along one leaves the value on the other undisturbed. Every clean basis comes from this zero dot product.