Elasticsearch Full Text Search
Full-text search powers search bars in e-commerce sites, blogs, and document management tools. Elasticsearch goes beyond exact matching — it understands language, handles typos, and ranks results by how well they match.
How Full-Text Search Differs from Exact Search
User types: "best laptops 2024" Exact search (term query): Looks for documents with EXACTLY "best laptops 2024" Returns very few or zero results Full-text search (match query): Breaks into: ["best", "laptops", "2024"] Finds documents containing any of these words Ranks documents with more matches higher Returns many useful results
match Query Deep Dive
The match query applies the same analyzer to your search text as was applied to the field when the document was indexed. Both go through the same word-splitting and lowercasing process, so "Laptops" and "laptops" both match.
GET /articles/_search
{
"query": {
"match": {
"content": "machine learning algorithms"
}
}
}
By default, this matches documents containing "machine" OR "learning" OR "algorithms." Change the operator to AND to require all three words:
GET /articles/_search
{
"query": {
"match": {
"content": {
"query": "machine learning algorithms",
"operator": "and"
}
}
}
}
match_phrase — Keeping Words Together
Use match_phrase when word order matters. "machine learning" should not match a document that says "the machine that keeps learning slowly."
GET /articles/_search
{
"query": {
"match_phrase": {
"content": "machine learning"
}
}
}
This returns only documents where "machine" appears immediately before "learning" — the phrase is intact.
match_phrase_prefix — Autocomplete Search
This query finds documents where the last word in your phrase is treated as a prefix. This powers autocomplete search bars.
GET /articles/_search
{
"query": {
"match_phrase_prefix": {
"title": "machine lea"
}
}
}
Results include "machine learning," "machine learning course," "machine learning basics" — any title starting with that phrase prefix.
multi_match — Search Across Multiple Fields
Search one query across several fields at once, like a search bar that checks both the title and description:
GET /articles/_search
{
"query": {
"multi_match": {
"query": "neural networks",
"fields": ["title", "content", "tags"]
}
}
}
You can boost the importance of certain fields using the caret symbol:
"fields": ["title^3", "content^1", "tags^2"]
A match in the title scores 3 times higher than a match in the content field.
Boosting Visualized
Document A: "neural networks" appears in title Document B: "neural networks" appears in content only With boosts: title^3, content^1 Document A score: 3 × base_score = 0.9 Document B score: 1 × base_score = 0.3 Result order: Document A first ✔
fuzzy — Typo-Tolerant Search
Fuzzy search finds documents even when the user makes a spelling mistake. It measures "edit distance" — how many single-character changes separate the typed word from a stored word.
GET /products/_search
{
"query": {
"fuzzy": {
"name": {
"value": "keyborad",
"fuzziness": "AUTO"
}
}
}
}
"keyborad" (typo) still matches "keyboard" because only two characters are transposed. AUTO fuzziness means Elasticsearch picks the right edit distance based on word length.
Edit Distance Examples
"keyborad" → "keyboard" (2 character swap) fuzziness: 2 ✔ "cat" → "car" (1 substitution) fuzziness: 1 ✔ "helo" → "hello" (1 insertion) fuzziness: 1 ✔ "xylophone" → "saxophone" (many changes) too different ✘
query_string — Power User Search
This query understands a mini-language with AND, OR, NOT, wildcards, and field-specific searches. Use it for advanced search interfaces where technical users type their own queries.
GET /articles/_search
{
"query": {
"query_string": {
"query": "title:(deep learning) AND NOT tags:beginner",
"default_field": "content"
}
}
}
Highlighting Search Terms in Results
Elasticsearch returns snippets of text with the matched words highlighted — ideal for showing users where their search term appeared:
GET /articles/_search
{
"query": {
"match": { "content": "neural networks" }
},
"highlight": {
"fields": {
"content": {}
}
}
}
The response includes a highlight block with the matched sentence, and the matching words are wrapped in <em> tags by default.
"highlight": {
"content": [
"Convolutional <em>neural networks</em> are widely used in image recognition."
]
}
