Skip to content
Snippets Groups Projects
Commit 2bcaa125 authored by John Fitzpatrick's avatar John Fitzpatrick Committed by Simonas Šerlinskas
Browse files

Update to allow multi-match query with no fields. (#291)

From the Elasticsearch documentation:

If no fields are provided, the multi_match query defaults to the
`index.query.default_field` index settings, which in turn defaults to
`*`. `*` extracts all fields in the mapping that are eligible to term
queries and filters the metadata fields. All extracted fields are then
combined to build a query.
parent 7b6ad65b
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,12 @@ use ONGR\ElasticsearchDSL\ParametersTrait;
* Represents Elasticsearch "multi_match" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
*
* Allows `$fields` to be an empty array to represent 'no fields'. From the Elasticsearch documentation:
*
* If no fields are provided, the multi_match query defaults to the `index.query.default_field` index settings,
* which in turn defaults to `*`. `*` extracts all fields in the mapping that are eligible to term queries and filters
* the metadata fields. All extracted fields are then combined to build a query.
*/
class MultiMatchQuery implements BuilderInterface
{
......@@ -59,9 +65,11 @@ class MultiMatchQuery implements BuilderInterface
public function toArray()
{
$query = [
'fields' => $this->fields,
'query' => $this->query,
];
if (count($this->fields)) {
$query['fields'] = $this->fields;
}
$output = $this->processArray($query);
......
......@@ -30,4 +30,19 @@ class MultiMatchQueryTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($expected, $query->toArray());
}
/**
* Tests multi-match query with no fields.
*/
public function testToArrayWithNoFields()
{
$query = new MultiMatchQuery([], 'this is a test');
$expected = [
'multi_match' => [
'query' => 'this is a test',
],
];
$this->assertEquals($expected, $query->toArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment