This lab covers the Elasticsearch full-text queries match, match_phrase and match_phrase_prefix, using the customers sample index. Below the gist you will find how these queries differ from the term query, how to run them on a secured Elasticsearch 8.x cluster, the most useful extra options (operator, minimum_should_match, fuzziness, slop, max_expansions), and how to work out why a query returns no hits.
Reference
- Sample Data & Query – https://github.com/devopsschool-demo-labs-projects/elasticsearch
Term vs match: why analysis decides the result
The gist opens with "TERM VS MATCH" but only shows the match family. The difference comes down to analysis. When a document is indexed, a text field such as name goes through an analyzer. With the default standard analyzer, Frank Norris is stored as the lowercase tokens frank and norris. A match query runs the same analyzer on your search text before it looks up tokens. A term query skips analysis and looks for the exact value you typed.
| Query | Search text analyzed? | Typical field type | Use it for |
|---|---|---|---|
term | No | keyword, numbers, dates, booleans | Exact values: IDs, status codes, tags |
match | Yes | text | Full-text search where any or all words should match |
match_phrase | Yes | text | Words that must appear together and in order |
match_phrase_prefix | Yes, last word treated as a prefix | text | Simple search-as-you-type on small data sets |
Run these two queries to see the difference. The first usually returns no hits, because the inverted index holds webster, not Webster. The second searches the keyword sub-field that dynamic mapping creates for strings, which stores the original value as one exact token. The full value has to match, so replace Webster with a complete name from your data.
# term on an analyzed text field: usually 0 hits
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "term": { "name": "Webster" } }
}'
# term on the keyword sub-field: exact, case-sensitive full value
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": { "term": { "name.keyword": "Webster" } }
}'Code language: PHP (php)
Note: The name.keyword sub-field only exists if the index used dynamic mapping or an explicit multi-field mapping. Check with GET customers/_mapping before you rely on it.
Running the queries on Elasticsearch 8.x and later
The curl commands in the gist assume a cluster with no security, served over plain HTTP. That was the default in older releases. Since Elasticsearch 8.0, security is on by default: the HTTP layer uses TLS and every request needs credentials. On a default 8.x install, add https, the CA certificate and a user:
export ES_URL=https://localhost:9200
export ELASTIC_PASSWORD='your-password'
curl --cacert /path/to/http_ca.crt -u elastic:$ELASTIC_PASSWORD \
-XGET "$ES_URL/customers/_search?pretty" \
-H 'Content-Type: application/json' -d'
{
"query": { "match": { "name": "webster" } }
}'Code language: PHP (php)
In a lab you can use -k instead of --cacert to skip certificate checks. Never do that against a production cluster. A simpler option is Kibana Dev Tools > Console, which handles authentication for you. Paste only the method, path and body:
GET customers/_search
{
"query": { "match": { "name": "webster" } }
}Code language: JavaScript (javascript)
In each response, check hits.total.value for the number of matches and _score on each hit for relevance. Results are sorted by _score, highest first, unless you add a sort clause.
Getting more out of match
In the examples above, operator: or (the default) returns customers whose name contains frank or norris. Documents that contain both words score higher. With operator: and, both tokens must be present. minimum_should_match sits between the two, and fuzziness tolerates typos.
# At least 2 of the 3 words must match
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"street": {
"query": "tompkins place avenue",
"minimum_should_match": 2
}
}
}
}'
# Typo tolerance: "webstr" still finds "webster"
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"name": { "query": "webstr", "fuzziness": "AUTO" }
}
}
}'Code language: PHP (php)
minimum_should_matchaccepts a number (2) or a percentage ("75%").fuzziness: AUTOallows edits based on term length: none for very short terms, one edit for medium terms and two for longer terms.- A
matchquery targets one field. To search several fields at once, usemulti_matchwith afieldsarray.
match_phrase and slop
match_phrase requires every token to be present and in the same order, next to each other. That is why match on tompkins place returns any street containing place, while match_phrase returns only streets that contain the exact phrase. Use slop to allow other words in between, or the terms in a different order:
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_phrase": {
"street": { "query": "tompkins place", "slop": 1 }
}
}
}'Code language: PHP (php)
slop is the number of position moves allowed. slop: 1 lets one extra word sit between tompkins and place. Swapping the two words takes a slop of 2. Higher values match more loosely and cost more to run.
match_phrase_prefix and its limits
match_phrase_prefix works like match_phrase, except the last token is treated as a prefix. In clymer st, clymer must match exactly and st can expand to street, st and so on. Elasticsearch expands the prefix into at most max_expansions terms (default 50), so on large indices some valid matches can be missed silently.
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match_phrase_prefix": {
"name": { "query": "ma", "max_expansions": 10 }
}
}
}'Code language: PHP (php)
Note: Use match_phrase_prefix for quick labs and small data sets. For production autocomplete, use a search_as_you_type field or the completion suggester. Both are built for prefix matching and give complete, predictable results.
Finding out why a query does or does not match
When results look wrong, look at the tokens Elasticsearch actually stores. The _analyze API runs a field's analyzer on any text, and explain shows how each hit's score was calculated.
# Which tokens does the street field produce?
curl -XGET 'localhost:9200/customers/_analyze?pretty' -H 'Content-Type: application/json' -d'
{ "field": "street", "text": "Tompkins Place" }'
# Field types and sub-fields
curl -XGET 'localhost:9200/customers/_mapping?pretty'
# Scoring details per hit
curl -XGET 'localhost:9200/customers/_search?pretty' -H 'Content-Type: application/json' -d'
{ "explain": true, "query": { "match": { "name": "frank norris" } } }'Code language: PHP (php)
With the standard analyzer, _analyze returns the tokens tompkins and place, both lowercase. If your term value differs from these tokens, it will not match.
Common errors and fixes
| Symptom | Cause | Fix |
|---|---|---|
curl: (52) Empty reply from server | Plain HTTP sent to an 8.x node that has TLS enabled | Use https:// and --cacert (or -k in a lab) |
security_exception with status 401 | No credentials or wrong credentials | Add -u elastic:password or an API key header |
Content-Type header [...] is not supported, status 406 | The -H 'Content-Type: application/json' header is missing | Always send the JSON content type with a body |
index_not_found_exception, status 404 | The sample data is not loaded, or the index name is wrong | Load the data from the reference repo, then check with GET _cat/indices |
parsing_exception: match doesn't support multiple fields | Two fields placed inside one match | Use multi_match or a bool query with several match clauses |
term returns 0 hits | Searching a text field, which holds analyzed, lowercase tokens | Use match, or term on the .keyword sub-field |
Corrections to the original
- The comment
----DEFAUL OPERATOR IS orshould read "default operator isor". It refers to thematchquery that follows it, which sends nooperator. - The heading "TERM VS MATCH" has no
termexample under it. The first section above adds one and explains the difference. - The curl commands target
http://localhost:9200with no credentials. That only works on clusters with security disabled. On a default Elasticsearch 8.x or later install, usehttps, a CA certificate and a username and password, as shown above.
Frequently asked questions
What is the difference between match and term in Elasticsearch?
match analyzes your search text with the field's analyzer before it looks up tokens, so it suits full-text text fields. term looks up the exact value without analysis, so it suits keyword, numeric and date fields.
What is the default operator of the match query?
The default is or. A document matches if any of the analyzed words is present. Set operator to and to require all words, or use minimum_should_match for a middle ground.
When should I use match_phrase instead of match?
Use match_phrase when the words must appear together and in the same order, such as a street name or a state like puerto rico. Add slop if you want to allow a few words in between.
Why does match_phrase_prefix miss some results?
The last word is expanded into at most max_expansions matching terms (50 by default), so other valid completions are left out. For autocomplete, use a search_as_you_type field or the completion suggester.
Can I use GET with a request body in curl for Elasticsearch?
Yes. Elasticsearch accepts the _search body with either GET or POST. -XGET -d works in curl, and POST is a safe alternative for clients that drop GET bodies.
Related tutorials
- What is Elasticsearch and use cases of Elasticsearch?
- Top 50 Elasticsearch Interview Questions and Answers
- Top 50 Kibana Interview Questions and Answers
- More Elastic Stack (ELK) tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals