Skip to content

Commit c9eef70

Browse files
multi_match query clarification
1 parent ff2d9d0 commit c9eef70

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

01-intro/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ Query:
151151
}
152152
```
153153

154+
We use `"type": "best_fields"`. You can read more about
155+
different types of `multi_match` search in [elastic-search.md](elastic-search.md).
154156

155157

156158
# Notes

01-intro/elastic-search.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
```

cohorts/2024/01-intro/homework.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ Which function do you use for adding your data to elastic?
6161

6262
Now let's search in our index.
6363

64-
For a query "How do I execute a command in a running docker container?", what's the score for the top ranking result?
64+
We will execute a query "How do I execute a command in a running docker container?".
6565

6666
Use only `question` and `text` fields and give `question` a boost of 4, and use `"type": "best_fields"`.
6767

68+
What's the score for the top ranking result?
69+
6870
* 94.05
6971
* 84.05
7072
* 74.05

0 commit comments

Comments
 (0)