Phrase queries require that the field is indexed with a
record of
position.running shoes, and we want to omit results like
running sleek shoes or shoes running — these results contain the right tokens, but not in the exact order and position
that the query specifies.
Enter the ### phrase operator:
running sleek shoes and shoes running did not match the phrase running shoes despite having the tokens running and
shoes because they appear in the wrong order or with other words in between.
How It Works
Let’s look at what happens under the hood for the above phrase query:- Retrieves the tokenizer configuration of the
descriptioncolumn. In this example, let’s assumedescriptionuses the simple tokenizer. - Tokenizes the query string with the same tokenizer. This means
running shoesbecomes two tokens:runningandshoes. - Finds all rows where
descriptioncontainsrunningimmediately followed byshoes.
Examples
Let’s consider a few more hypothetical documents to see whether they would be returned by the phrase query. These examples assume that index uses the default tokenizer and token filters, and that the query isrunning shoes.
| Original Text | Tokens | Match | Reason | Related |
|---|---|---|---|---|
| Sleek running shoes | sleek running shoes | ✅ | Contains running and shoes, in that order. | |
| Running shoes sleek | sleek running shoes | ❌ | running and shoes not in the right order. | Match conjunction |
| SLeeK RUNNING ShOeS | sleek running shoes | ✅ | Contains running and shoes, in that order. | Lowercasing |
| Sleek run shoe | sleek run shoe | ❌ | Does not contain both running and shoes. | Stemming |
| Sleke ruining shoez | sleke ruining shoez | ❌ | Does not contain both running and shoes. | |
| White jogging shoes | white jogging shoes | ❌ | Does not contain both running and shoes. |
Adding Slop
Slop allows the token ordering requirement of phrase queries to be relaxed. It specifies how many changes — like extra words in between or transposed word positions — are allowed while still considering the phrase a match:- An extra word in between (e.g.
sleek shoesvs.sleek running shoes) has a slop of1 - A transposition (e.g.
running shoesvs.shoes running) has a slop of2
slop(n), where n is the maximum allowed slop.
Using a Custom Tokenizer
Like the match query, the phrase query supports custom query tokenization.Using a Text Array
The phrase operator also accepts a text array as the right-hand side argument. If a text array is provided, each element of the array is treated as an exact token, which means that no further processing is done. The following query matches documents containing the tokenshoes immediately followed by running: