Skip to content

Commit a5d77e6

Browse files
authored
Restructure queries (#195)
* move queries to specific domain level folder * fixed namespaces * added deprecation messages
1 parent 645d1ea commit a5d77e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2698
-1911
lines changed

Diff for: src/Query/BoolQuery.php

+3-110
Original file line numberDiff line numberDiff line change
@@ -11,120 +11,13 @@
1111

1212
namespace ONGR\ElasticsearchDSL\Query;
1313

14-
use ONGR\ElasticsearchDSL\BuilderInterface;
15-
use ONGR\ElasticsearchDSL\ParametersTrait;
16-
use Ramsey\Uuid\Uuid;
17-
1814
/**
1915
* Represents Elasticsearch "bool" query.
2016
*
2117
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
18+
*
19+
* @deprecated Use the extended class instead. This class is left only for BC compatibility.
2220
*/
23-
class BoolQuery implements BuilderInterface
21+
class BoolQuery extends \ONGR\ElasticsearchDSL\Query\Compound\BoolQuery
2422
{
25-
use ParametersTrait;
26-
27-
const MUST = 'must';
28-
const MUST_NOT = 'must_not';
29-
const SHOULD = 'should';
30-
const FILTER = 'filter';
31-
32-
/**
33-
* @var array
34-
*/
35-
private $container = [];
36-
37-
/**
38-
* Constructor to prepare container.
39-
*/
40-
public function __construct()
41-
{
42-
$this->container = [];
43-
}
44-
45-
/**
46-
* Returns the query instances (by bool type).
47-
*
48-
* @param string|null $boolType
49-
*
50-
* @return array
51-
*/
52-
public function getQueries($boolType = null)
53-
{
54-
if ($boolType === null) {
55-
$queries = [];
56-
57-
foreach ($this->container as $item) {
58-
$queries = array_merge($queries, $item);
59-
}
60-
61-
return $queries;
62-
}
63-
64-
if (isset($this->container[$boolType])) {
65-
return $this->container[$boolType];
66-
}
67-
68-
return [];
69-
}
70-
71-
/**
72-
* Add BuilderInterface object to bool operator.
73-
*
74-
* @param BuilderInterface $query Query add to the bool.
75-
* @param string $type Bool type. Example: must, must_not, should.
76-
* @param string $key Key that indicates a builder id.
77-
*
78-
* @return string Key of added builder.
79-
*
80-
* @throws \UnexpectedValueException
81-
*/
82-
public function add(BuilderInterface $query, $type = self::MUST, $key = null)
83-
{
84-
if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
85-
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
86-
}
87-
88-
if (!$key) {
89-
$key = bin2hex(random_bytes(30));
90-
}
91-
92-
$this->container[$type][$key] = $query;
93-
94-
return $key;
95-
}
96-
97-
/**
98-
* {@inheritdoc}
99-
*/
100-
public function toArray()
101-
{
102-
if (count($this->container) === 1 && isset($this->container[self::MUST])
103-
&& count($this->container[self::MUST]) === 1) {
104-
$query = reset($this->container[self::MUST]);
105-
106-
return $query->toArray();
107-
}
108-
109-
$output = [];
110-
111-
foreach ($this->container as $boolType => $builders) {
112-
/** @var BuilderInterface $builder */
113-
foreach ($builders as $builder) {
114-
$output[$boolType][] = $builder->toArray();
115-
}
116-
}
117-
118-
$output = $this->processArray($output);
119-
120-
return [$this->getType() => $output];
121-
}
122-
123-
/**
124-
* {@inheritdoc}
125-
*/
126-
public function getType()
127-
{
128-
return 'bool';
129-
}
13023
}

Diff for: src/Query/BoostingQuery.php

+3-51
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,13 @@
1111

1212
namespace ONGR\ElasticsearchDSL\Query;
1313

14-
use ONGR\ElasticsearchDSL\BuilderInterface;
15-
1614
/**
1715
* Represents Elasticsearch "boosting" query.
1816
*
1917
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html
18+
*
19+
* @deprecated Use the extended class instead. This class is left only for BC compatibility.
2020
*/
21-
class BoostingQuery implements BuilderInterface
21+
class BoostingQuery extends \ONGR\ElasticsearchDSL\Query\Compound\BoostingQuery
2222
{
23-
/**
24-
* @var BuilderInterface
25-
*/
26-
private $positive;
27-
28-
/**
29-
* @var BuilderInterface
30-
*/
31-
private $negative;
32-
33-
/**
34-
* @var int|float
35-
*/
36-
private $negativeBoost;
37-
38-
/**
39-
* @param BuilderInterface $positive
40-
* @param BuilderInterface $negative
41-
* @param int|float $negativeBoost
42-
*/
43-
public function __construct(BuilderInterface $positive, BuilderInterface $negative, $negativeBoost)
44-
{
45-
$this->positive = $positive;
46-
$this->negative = $negative;
47-
$this->negativeBoost = $negativeBoost;
48-
}
49-
50-
/**
51-
* {@inheritdoc}
52-
*/
53-
public function getType()
54-
{
55-
return 'boosting';
56-
}
57-
58-
/**
59-
* {@inheritdoc}
60-
*/
61-
public function toArray()
62-
{
63-
$query = [
64-
'positive' => $this->positive->toArray(),
65-
'negative' => $this->negative->toArray(),
66-
'negative_boost' => $this->negativeBoost,
67-
];
68-
69-
return [$this->getType() => $query];
70-
}
7123
}

Diff for: src/Query/CommonTermsQuery.php

+3-51
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,13 @@
1111

1212
namespace ONGR\ElasticsearchDSL\Query;
1313

14-
use ONGR\ElasticsearchDSL\BuilderInterface;
15-
use ONGR\ElasticsearchDSL\ParametersTrait;
16-
1714
/**
1815
* Represents Elasticsearch "common" query.
1916
*
2017
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html
18+
*
19+
* @deprecated Use the extended class instead. This class is left only for BC compatibility.
2120
*/
22-
class CommonTermsQuery implements BuilderInterface
21+
class CommonTermsQuery extends \ONGR\ElasticsearchDSL\Query\FullText\CommonTermsQuery
2322
{
24-
use ParametersTrait;
25-
26-
/**
27-
* @var string
28-
*/
29-
private $field;
30-
31-
/**
32-
* @var string
33-
*/
34-
private $query;
35-
36-
/**
37-
* @param string $field
38-
* @param string $query
39-
* @param array $parameters
40-
*/
41-
public function __construct($field, $query, array $parameters = [])
42-
{
43-
$this->field = $field;
44-
$this->query = $query;
45-
$this->setParameters($parameters);
46-
}
47-
48-
/**
49-
* {@inheritdoc}
50-
*/
51-
public function getType()
52-
{
53-
return 'common';
54-
}
55-
56-
/**
57-
* {@inheritdoc}
58-
*/
59-
public function toArray()
60-
{
61-
$query = [
62-
'query' => $this->query,
63-
];
64-
65-
$output = [
66-
$this->field => $this->processArray($query),
67-
];
68-
69-
return [$this->getType() => $output];
70-
}
7123
}

Diff for: src/Query/Compound/BoolQuery.php

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Query\Compound;
13+
14+
use ONGR\ElasticsearchDSL\BuilderInterface;
15+
use ONGR\ElasticsearchDSL\ParametersTrait;
16+
17+
/**
18+
* Represents Elasticsearch "bool" query.
19+
*
20+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
21+
*/
22+
class BoolQuery implements BuilderInterface
23+
{
24+
use ParametersTrait;
25+
26+
const MUST = 'must';
27+
const MUST_NOT = 'must_not';
28+
const SHOULD = 'should';
29+
const FILTER = 'filter';
30+
31+
/**
32+
* @var array
33+
*/
34+
private $container = [];
35+
36+
/**
37+
* Constructor to prepare container.
38+
*/
39+
public function __construct()
40+
{
41+
$this->container = [];
42+
}
43+
44+
/**
45+
* Returns the query instances (by bool type).
46+
*
47+
* @param string|null $boolType
48+
*
49+
* @return array
50+
*/
51+
public function getQueries($boolType = null)
52+
{
53+
if ($boolType === null) {
54+
$queries = [];
55+
56+
foreach ($this->container as $item) {
57+
$queries = array_merge($queries, $item);
58+
}
59+
60+
return $queries;
61+
}
62+
63+
if (isset($this->container[$boolType])) {
64+
return $this->container[$boolType];
65+
}
66+
67+
return [];
68+
}
69+
70+
/**
71+
* Add BuilderInterface object to bool operator.
72+
*
73+
* @param BuilderInterface $query Query add to the bool.
74+
* @param string $type Bool type. Example: must, must_not, should.
75+
* @param string $key Key that indicates a builder id.
76+
*
77+
* @return string Key of added builder.
78+
*
79+
* @throws \UnexpectedValueException
80+
*/
81+
public function add(BuilderInterface $query, $type = self::MUST, $key = null)
82+
{
83+
if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
84+
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
85+
}
86+
87+
if (!$key) {
88+
$key = bin2hex(random_bytes(30));
89+
}
90+
91+
$this->container[$type][$key] = $query;
92+
93+
return $key;
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function toArray()
100+
{
101+
if (count($this->container) === 1 && isset($this->container[self::MUST])
102+
&& count($this->container[self::MUST]) === 1) {
103+
$query = reset($this->container[self::MUST]);
104+
105+
return $query->toArray();
106+
}
107+
108+
$output = [];
109+
110+
foreach ($this->container as $boolType => $builders) {
111+
/** @var BuilderInterface $builder */
112+
foreach ($builders as $builder) {
113+
$output[$boolType][] = $builder->toArray();
114+
}
115+
}
116+
117+
$output = $this->processArray($output);
118+
119+
return [$this->getType() => $output];
120+
}
121+
122+
/**
123+
* {@inheritdoc}
124+
*/
125+
public function getType()
126+
{
127+
return 'bool';
128+
}
129+
}

0 commit comments

Comments
 (0)