Skip to content

Commit c577624

Browse files
committed
TASK: Improve viewNodeCommand to display a useful output
Add parameter $field to display a given field
1 parent b000295 commit c577624

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

Classes/Command/SearchCommandController.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Neos\Flow\Cli\CommandController;
2727
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
2828
use Neos\Media\Domain\Model\ResourceBasedInterface;
29+
use Neos\Utility\Arrays;
2930

3031
/**
3132
* @Flow\Scope("singleton")
@@ -54,6 +55,7 @@ class SearchCommandController extends CommandController
5455
* @throws Exception
5556
* @throws QueryBuildingException
5657
* @throws IllegalObjectTypeException
58+
* @throws Exception\ConfigurationException
5759
*/
5860
public function fulltextCommand(string $searchWord, string $path = '/', ?string $dimensions = null): void
5961
{
@@ -68,7 +70,7 @@ public function fulltextCommand(string $searchWord, string $path = '/', ?string
6870
$queryBuilder = new ElasticSearchQueryBuilder();
6971
$queryBuilder = $queryBuilder->query($contextNode)
7072
->fulltext($searchWord)
71-
->limit(100)
73+
->limit(10)
7274
->termSuggestions($searchWord)
7375
->log(__CLASS__);
7476

@@ -93,15 +95,16 @@ public function fulltextCommand(string $searchWord, string $path = '/', ?string
9395
*
9496
* @param string $identifier The node identifier
9597
* @param string|null $dimensions Dimensions, specified in JSON format, like '{"language":["de"]}'
98+
* @param string $field Name or path to a source field to display. Eg. "__fulltext.h1"
9699
* @throws Exception
97100
* @throws IllegalObjectTypeException
98101
* @throws QueryBuildingException
99102
* @throws \Flowpack\ElasticSearch\Exception
100103
* @throws \Neos\Flow\Http\Exception
101104
*/
102-
public function viewNodeCommand(string $identifier, ?string $dimensions = null): void
105+
public function viewNodeCommand(string $identifier, ?string $dimensions = null, string $field = ''): void
103106
{
104-
if ($dimensions !== null && is_array(json_decode($dimensions, true)) === false) {
107+
if ($dimensions !== null && is_array(json_decode($dimensions, true, 512, JSON_THROW_ON_ERROR)) === false) {
105108
$this->outputLine('<error>Error: </error>The Dimensions must be given as a JSON array like \'{"language":["de"]}\'');
106109
$this->sendAndExit(1);
107110
}
@@ -112,10 +115,23 @@ public function viewNodeCommand(string $identifier, ?string $dimensions = null):
112115
$queryBuilder->query($context->getRootNode());
113116
$queryBuilder->exactMatch('__identifier', $identifier);
114117

118+
$queryBuilder->getRequest()->setValueByPath('_source', []);
119+
115120
if ($queryBuilder->count() > 0) {
116121
$this->outputLine();
117122
$this->outputLine('<info>Results</info>');
118-
$this->outputResults($queryBuilder->execute());
123+
124+
foreach ($queryBuilder->execute() as $node) {
125+
$this->outputLine('<b>%s</b>', [(string)$node]);
126+
$data = $queryBuilder->getFullElasticSearchHitForNode($node);
127+
128+
if ($field !== '') {
129+
$data = Arrays::getValueByPath($data, '_source.' . $field);
130+
}
131+
132+
$this->outputLine(print_r($data, true));
133+
$this->outputLine();
134+
}
119135
} else {
120136
$this->outputLine();
121137
$this->outputLine('No document matching the given node identifier');
@@ -135,6 +151,10 @@ private function outputResults(ElasticSearchQueryResult $result): void
135151
$properties[$propertyName] = '<b>' . $propertyName . '</b>: ' . (string)$propertyValue->getResource()->getFilename();
136152
} elseif ($propertyValue instanceof \DateTime) {
137153
$properties[$propertyName] = '<b>' . $propertyName . '</b>: ' . $propertyValue->format('Y-m-d H:i');
154+
} elseif (is_array($propertyValue)) {
155+
$properties[$propertyName] = '<b>' . $propertyName . '</b>: ' . 'array';
156+
} elseif ($propertyValue instanceof NodeInterface) {
157+
$properties[$propertyName] = '<b>' . $propertyName . '</b>: ' . $propertyValue->getIdentifier();
138158
} else {
139159
$properties[$propertyName] = '<b>' . $propertyName . '</b>: ' . (string)$propertyValue;
140160
}
@@ -144,12 +164,11 @@ private function outputResults(ElasticSearchQueryResult $result): void
144164
'identifier' => $node->getIdentifier(),
145165
'label' => $node->getLabel(),
146166
'nodeType' => $node->getNodeType()->getName(),
147-
'contextPath' => implode(PHP_EOL, explode('@', $node->getContextPath())),
148167
'properties' => implode(PHP_EOL, $properties),
149168
];
150169
}, $result->toArray());
151170

152-
$this->output->outputTable($results, ['Identifier', 'Label', 'Node Type', 'Context', 'Properties']);
171+
$this->output->outputTable($results, ['Identifier', 'Label', 'Node Type', 'Properties']);
153172
}
154173

155174
/**
@@ -163,7 +182,7 @@ private function createContext(string $dimensions = null): Context
163182
];
164183

165184
if ($dimensions !== null) {
166-
$contextConfiguration['dimensions'] = json_decode($dimensions, true);
185+
$contextConfiguration['dimensions'] = json_decode($dimensions, true, 512, JSON_THROW_ON_ERROR);
167186
}
168187

169188
return $this->contextFactory->create($contextConfiguration);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Shows the mapping created for the NodeTypes.
102102

103103
Shows a list of NodeTypes and if they are configured to be indexable
104104

105-
./flow search:viewnode <nodeIdentifier>
105+
./flow search:viewnode <nodeIdentifier> [<dimensionCombinationAsJson>] [<field>]
106106

107107
Shows all contents that are indexed fo a given node.
108108

0 commit comments

Comments
 (0)