|
| 1 | +# Elastic Search |
| 2 | + |
| 3 | +This document contains useful things about Elasticsearch |
| 4 | + |
| 5 | +# `multi_match` Query in Elasticsearch |
| 6 | + |
| 7 | +The `multi_match` query is used to search for a given text across multiple fields in an Elasticsearch index. |
| 8 | + |
| 9 | +It provides various types to control how the matching is executed and scored. |
| 10 | + |
| 11 | +There are multiple types of `multi_match` queries: |
| 12 | + |
| 13 | +- `best_fields`: Returns the highest score from any one field. |
| 14 | +- `most_fields`: Combines the scores from all fields. |
| 15 | +- `cross_fields`: Treats fields as one big field for scoring. |
| 16 | +- `phrase`: Searches for the query as an exact phrase. |
| 17 | +- `phrase_prefix`: Searches for the query as a prefix of a phrase. |
| 18 | + |
| 19 | + |
| 20 | +## `best_fields` |
| 21 | + |
| 22 | +The `best_fields` type searches each field separately and returns the highest score from any one of the fields. |
| 23 | + |
| 24 | +This type is useful when you want to find documents where at least one field matches the query well. |
| 25 | + |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "size": 5, |
| 30 | + "query": { |
| 31 | + "bool": { |
| 32 | + "must": { |
| 33 | + "multi_match": { |
| 34 | + "query": "How do I run docker on Windows?", |
| 35 | + "fields": ["question", "text"], |
| 36 | + "type": "best_fields" |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## `most_fields` |
| 45 | + |
| 46 | +The `most_fields` type searches each field and combines the scores from all fields. |
| 47 | + |
| 48 | +This is useful when the relevance of a document increases with more matching fields. |
| 49 | + |
| 50 | +```json |
| 51 | +{ |
| 52 | + "multi_match": { |
| 53 | + "query": "How do I run docker on Windows?", |
| 54 | + "fields": ["question^4", "text"], |
| 55 | + "type": "most_fields" |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## `cross_fields` |
| 61 | + |
| 62 | +The `cross_fields` type treats fields as though they were one big field. |
| 63 | + |
| 64 | +It is suitable for cases where you have fields representing the same text in different ways, such as synonyms. |
| 65 | + |
| 66 | +```json |
| 67 | +{ |
| 68 | + "multi_match": { |
| 69 | + "query": "How do I run docker on Windows?", |
| 70 | + "fields": ["question", "text"], |
| 71 | + "type": "cross_fields" |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +## `phrase` |
| 77 | + |
| 78 | +The `phrase` type looks for the query as an exact phrase within the fields. |
| 79 | + |
| 80 | +It is useful for exact match searches. |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "multi_match": { |
| 85 | + "query": "How do I run docker on Windows?", |
| 86 | + "fields": ["question", "text"], |
| 87 | + "type": "phrase" |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## `phrase_prefix` |
| 93 | + |
| 94 | +The `phrase_prefix` type searches for documents that contain the query as a prefix of a phrase. |
| 95 | + |
| 96 | +This is useful for autocomplete or typeahead functionality. |
| 97 | + |
| 98 | + |
| 99 | +```json |
| 100 | +{ |
| 101 | + "multi_match": { |
| 102 | + "query": "How do I run docker on Windows?", |
| 103 | + "fields": ["question", "text"], |
| 104 | + "type": "phrase_prefix" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
0 commit comments