Elasticsearch Multi-Match and Cross-Field Search
On this page
Searching many fields from one search box
Users type into one box; your documents spread information across fields: first and last names, title and body, brand and product name. multi_match searches several fields, and its type decides how per-field scores combine.
Why best_fields can rank the wrong person first
A customers index with first and last names in separate fields. A search for John Smith with the default type:
GET /customers/_search
{ "query": { "multi_match": { "query": "John Smith", "fields": ["first_name", "last_name"], "type": "best_fields" } } }total 4
2 1.204 Smith | Jones
1 0.693 John | Smith
3 0.693 John | Johnson
4 0.693 Will | Smith"Smith Jones", someone whose first name is Smith, ranks above John Smith himself. best_fields scores each field separately and takes the best one. In first_name, "smith" is rare (only one document), so its IDF is high and that single-field match beats John Smith, whose two terms are split across two fields and each scored separately.
GET /customers/_search
{ "query": { "multi_match": { "query": "John Smith", "fields": ["first_name", "last_name"],
"type": "cross_fields", "operator": "and" } } }total 1
1 1.386 John | Smithcross_fields treats the fields as one big field: each term must appear in at least one of them, and term statistics are blended across fields so a rare-in-one-field term doesn't dominate. With "operator": "and", only the person matching both names is returned.
multi_match types: best_fields, most_fields, cross_fields, phrase
| Type | How it scores | Use for |
|---|---|---|
best_fields (default) | Best single field (plus tie_breaker × the others) | The whole query should match one field well: title or body |
most_fields | Sum across fields | The same text indexed several ways (stemmed, unstemmed, shingles) |
cross_fields | Fields blended as one | Entities split across fields: names, addresses, brand + model |
phrase / phrase_prefix | Phrase match per field | Exact phrases; prefix for simple autocomplete |
bool_prefix | Terms, last as prefix | search_as_you_type fields (Day 11) |
tie_breaker: 0.3 with best_fields adds 30% of the other fields' scores, rewarding documents that match in several places without letting them dominate.
Combining multi_match with field weights
Weights work in every type (Day 10):
{ "multi_match": {
"query": "logi wireless mouse",
"fields": ["name^3", "brand^2", "description"],
"type": "cross_fields",
"operator": "and" } }A product query mixing brand and product words is a classic cross_fields case: "logi" matches the brand, "wireless mouse" the name.
An alternative: copy_to fields
Copy several fields into one combined field at index time and search it with a plain match:
"first_name": { "type": "text", "copy_to": "full_name" },
"last_name": { "type": "text", "copy_to": "full_name" },
"full_name": { "type": "text" }Simple and fast, at the cost of extra index space and losing per-field weighting.
Check yourself
best_fields scores fields separately; "smith" was rare in first_name, so that single-field match scored higher than John Smith's two terms split across fields.
When one entity's words are spread across several fields, like first and last name, or brand and model. It treats the fields as one big field, so "John Smith" matches the person with first name John and last name Smith best.
Searching the same text indexed with different analyzers, such as an exact subfield and a stemmed one, adding up the evidence from each version so documents matching in several ways rank higher.
It adds a fraction of the scores from other matching fields to the best field's score, so documents matching in several fields get some credit.
When you always search the same group of fields together. One combined field is simpler and avoids per-field scoring differences.