Hard search elasticsearch

How do you search several product fields with boosts, and when do you pick best_fields or cross_fields?

The 30 second answer

multi_match runs the query over several fields. best_fields, the default, scores a document by its single best field, which suits "the whole query appears in the title". cross_fields treats fields with the same analyzer as one big field and looks for each word in any of them, which suits "red Nike" split across colour and brand. I boost the strong fields, like name over description, and add a small tie_breaker.

Could you say this in an interview?

The problem. A shopper types one string, but the words can live in different fields: "red nike running shoes" has a colour, a brand and a product type. How you combine the fields decides both relevance and cost.

The types, in the docs' words.

  • best_fields (default): "Finds documents which match any field, but uses the _score from the best field." Internally it's a dis_max over one match per field.
  • most_fields: matches any field and "combines the _score from each field". Useful when the same text is indexed several ways (stemmed and unstemmed sub-fields).
  • cross_fields: "Treats fields with the same analyzer as though they were one big field. Looks for each word in any field."
  • phrase / phrase_prefix: a match_phrase per field.

tie_breaker changes best_fields from "best field only" to "best field plus tie_breaker times the other matching fields' scores".

Badly written. best_fields with operator: and requires all words to be in one field, so "red nike shoes" finds nothing when "red" is only in color and "nike" only in brand:

{ "query": { "multi_match": {
    "query": "red nike shoes", "operator": "and",
    "fields": ["name", "brand", "color", "description"] } } }

Better. cross_fields checks each word across the fields, so "red" can match color and "nike" can match brand, while boosts keep strong fields on top:

GET products/_search
{ "query": { "bool": {
    "must": [ { "multi_match": {
        "query": "red nike shoes", "type": "cross_fields", "operator": "and",
        "fields": ["name^3", "brand^2", "color", "category_name"] } } ],
    "filter": [ { "term": { "market": "de" } } ]
}}}

Leave long description out of the main query or give it a low weight; it adds cost and noise.

Trade-offs.

  • Every field is searched separately, so cost grows with the number of fields, and the docs note the clause count is roughly fields times terms. The slow-query page covers folding low-value fields into one copy_to field.
  • cross_fields blends fields only within the same analyzer group. Mixing analyzers (a stemmed field and a keyword field) splits the query into groups and can surprise you.
  • Boosts are relative weights, not magic numbers. Tune them against a judgment set, not by feel.

Alternate interview scenarios

"A search for a brand name ranks products that mention it in the description above the brand's own products. Fix it."

Drop description from the query or give it a low weight, boost brand, and consider a term boost on brand.raw when query understanding recognises a brand.

"Title matches should win, but a product matching in title and brand should beat one matching only in title."

Use best_fields with a small tie_breaker, for example 0.3, so other matching fields add a fraction of their score.

Sources (2)
  1. Elasticsearch docs: Multi-match query (types, tie_breaker)elastic.co
  2. Elasticsearch docs: Tune for search speed (search as few fields as possible)elastic.co