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\ElasticsearchBundle\Result; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This is the class for inner hit result with nested support. |
18
|
|
|
*/ |
19
|
|
|
class InnerHitValue |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $rawData; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Manager |
28
|
|
|
*/ |
29
|
|
|
private $manager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $rawData |
33
|
|
|
* @param Manager $manager |
34
|
|
|
*/ |
35
|
|
|
public function __construct($rawData, Manager $manager) |
36
|
|
|
{ |
37
|
|
|
$this->rawData = $rawData; |
38
|
|
|
$this->manager = $manager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns array of specific inner hit objects |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* |
46
|
|
|
* @return object[] |
47
|
|
|
*/ |
48
|
|
|
public function getValue($name) |
49
|
|
|
{ |
50
|
|
|
if (!isset($this->rawData[$name])) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$hits = []; |
55
|
|
|
|
56
|
|
|
foreach ($this->rawData[$name]['hits']['hits'] as $hit) { |
57
|
|
|
|
58
|
|
|
if (isset($hit['_parent'])) { |
59
|
|
|
$hits[] = $this->manager->getConverter()->convertToDocument($hit, $this->manager); |
60
|
|
|
} else { |
61
|
|
|
$fields = $hit; |
62
|
|
|
$metadata = $this->manager |
63
|
|
|
->getMetadataCollector() |
64
|
|
|
->getMappings( |
65
|
|
|
$this->manager->getConfig()['mappings'] |
66
|
|
|
)[$hit['_type']]; |
67
|
|
|
|
68
|
|
|
while (isset($fields['_nested'])) { |
69
|
|
|
$fields = $fields['_nested']; |
70
|
|
|
$metadata = $metadata['aliases'][$fields['field']]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$hits[] = $this->manager->getConverter()->assignArrayToObject( |
74
|
|
|
$hit['_source'], |
75
|
|
|
new $metadata['namespace'], |
76
|
|
|
$metadata['aliases'] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
}; |
80
|
|
|
|
81
|
|
|
return $hits; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the count of inner hits for a specific hit |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return integer |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function getCount($name) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if (!isset($this->rawData[$name])) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->rawData[$name]['hits']['total']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns inner hits for a specified inner hit |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* |
105
|
|
|
* @return InnerHitValue[]|null |
106
|
|
|
*/ |
107
|
|
|
public function getInnerHits($name) |
108
|
|
|
{ |
109
|
|
|
if (!isset($this->rawData[$name]['hits']['hits'][0]['inner_hits'])) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$hits = []; |
114
|
|
|
|
115
|
|
|
foreach ($this->rawData[$name]['hits']['hits'] as $hit) { |
116
|
|
|
$hits[] = new self($hit['inner_hits'], $this->manager); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $hits; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.