Master Elasticsearch Queries: Boost, Phrase, Multi‑Match & Query String Explained
This guide shows how to use Elasticsearch's match, match_phrase, multi_match, and query_string queries—including field boosting, phrase slop, and type options—through clear PHP client examples and sample responses for precise full‑text search control.
Match Query
Use a bool query with should clauses to combine several match queries, assigning a boost value to each field so that matches in higher‑boosted fields contribute more to the relevance score.
{
"query": {
"bool": {
"should": [
{
"match": {
"product_name": {
"query": "apple",
"boost": 3
}
}
},
{
"match": {
"description": {
"query": "apple",
"boost": 1
}
}
}
]
}
}
}The query searches the product_name and description fields for the term "apple"; the product_name match is weighted three times higher than the description match.
Exact Phrase Match (match_phrase)
The match_phrase query forces the terms to appear in the specified order. A simple PHP client example searches the title field for an exact phrase.
$client = ElasticSearchCLient::getInstance();
$indexParams = [
'index' => 'resty_product_test_index',
'body' => [
'query' => [
'match_phrase' => [
'title' => '开源技术小栈20245直播间'
]
]
]
];
$indexResponse = $client->search($indexParams);A sample hit returned by this query:
"hits": [
{
"_index": "resty_product_test_index",
"_type": "_doc",
"_id": "20245",
"_score": 2.3340414,
"_source": {
"id": 20245,
"title": "开源技术小栈20245直播间",
"content": "开源技术小栈-腾讯云开发者社区,开源技术小栈20245直播间"
}
}
]Adding a slop parameter relaxes the positional constraint, allowing a limited number of intervening terms.
$client = ElasticSearchCLient::getInstance();
$indexParams = [
'index' => 'resty_product_test_index',
'body' => [
'query' => [
'match_phrase' => [
'title' => [
'query' => '开源技术小栈20245直播间',
'slop' => 2
]
]
]
]
];
$indexResponse = $client->search($indexParams);Multi‑field Search (multi_match)
The multi_match query lets a single query string be searched across multiple fields such as title and content. The simplest form specifies the query and the target fields.
$client = ElasticSearchCLient::getInstance();
$query = '开源技术小栈';
$indexParams = [
'index' => 'resty_product_test_index',
'body' => [
'query' => [
'multi_match' => [
'query' => $query,
'fields' => ['title', 'content']
]
]
]
];
$indexResponse = $client->search($indexParams);Results contain hits where either title or content includes the phrase "开源技术小栈".
The type parameter changes the matching algorithm. For example, using "best_fields" selects the single field with the highest score.
$client = ElasticSearchCLient::getInstance();
$query = '开发者社区';
$indexParams = [
'index' => 'resty_product_test_index',
'body' => [
'query' => [
'multi_match' => [
'query' => $query,
'fields' => ['title', 'content'],
"type" => "best_fields"
]
]
]
];
$indexResponse = $client->search($indexParams);This query searches the same two fields but applies the "best_fields" strategy, which prefers the field that yields the highest relevance score for each document.
Advanced Query String
The query_string query supports Lucene syntax, enabling complex logical operators and field‑specific searches. A basic example searches the title field for the keyword "开发者社区".
$client = ElasticSearchCLient::getInstance();
$query = '开发者社区';
$indexParams = [
'index' => 'resty_product_test_index',
'body' => [
'query' => [
'query_string' => [
'default_field' => 'title',
'query' => $query
]
]
]
];
$indexResponse = $client->search($indexParams);This query returns documents whose title contains the term "开发者社区" while allowing the full expressive power of Lucene query syntax for more sophisticated searches.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
