Catalog My learning Blog
Elasticsearch Tutorial: How Full-Text Search Works · Phase 2 · Lesson 15 · 3 min read

Elasticsearch Multi-Match and Cross-Field Search

On this page
  1. Searching many fields from one search box
  2. Why best_fields can rank the wrong person first
  3. multi_match types: best_fields, most_fields, cross_fields, phrase
  4. Combining multi_match with field weights
  5. An alternative: copy_to fields
  6. Check yourself

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:

HTTP
GET /customers/_search
{ "query": { "multi_match": { "query": "John Smith", "fields": ["first_name", "last_name"], "type": "best_fields" } } }
Code
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.

HTTP
GET /customers/_search
{ "query": { "multi_match": { "query": "John Smith", "fields": ["first_name", "last_name"],
                              "type": "cross_fields", "operator": "and" } } }
Code
total 1
  1    1.386   John | Smith

cross_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.

best_fields scores each field alone and keeps the best; cross_fields treats several fields as one, so terms split across fields combine correctly.
best_fields scores each field alone and keeps the best; cross_fields treats several fields as one, so terms split across fields combine correctly.

multi_match types: best_fields, most_fields, cross_fields, phrase

TypeHow it scoresUse for
best_fields (default)Best single field (plus tie_breaker × the others)The whole query should match one field well: title or body
most_fieldsSum across fieldsThe same text indexed several ways (stemmed, unstemmed, shingles)
cross_fieldsFields blended as oneEntities split across fields: names, addresses, brand + model
phrase / phrase_prefixPhrase match per fieldExact phrases; prefix for simple autocomplete
bool_prefixTerms, last as prefixsearch_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):

JSON
{ "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:

YAML
"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.