Query DSLmedium3-5 years

A `bool` query's `filter` array has `{ "term": { "title": "Kafka" } }`, where `title` is mapped as `text` with the standard analyser. It matches nothing, even for documents whose title is literally "Kafka". Diagnose it, and name the two different fixes depending on what the query actually needs to do.

term matches an exact, unanalysed string against the exact terms in the index. title as a text field was analysed at index time — the standard analyser at minimum lowercases, so "Kafka" was indexed as the term kafka, not Kafka. The term query for "Kafka" (capitalized) never matches the indexed lowercase term. Two different fixes depending on intent: if the goal is full-text matching (find documents whose title contains the word, case-insensitively, contributing to relevance), use match instead of term, which analyses the query text too. If the goal is an exact-value filter (title equals this precise string, no analysis, no scoring), query the title.keyword sub-field instead, which dynamic mapping created and stores the value unanalysed.

The lesson behind it →
More on Query DSL