Skip to content

Commit

Permalink
feat(Solr): Index integer, decimal, datetime, string and geojson fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Sep 25, 2023
1 parent 8e794bd commit a62c29c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ services:
aliases:
- ${APP_NAMESPACE}_solr
default:
aliases:
- solr
labels:
- "traefik.enable=true"
- "traefik.http.services.${APP_NAMESPACE}_solr.loadbalancer.server.scheme=http"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,26 @@ protected function formatDateTimeToUTC(\DateTimeInterface $dateTime): string
{
return gmdate('Y-m-d\TH:i:s\Z', $dateTime->getTimestamp());
}

protected function formatGeoJsonFeature(mixed $geoJson): ?string
{
if (null === $geoJson) {
return null;
}
if (\is_string($geoJson)) {
$geoJson = \json_decode($geoJson, true);
}
if (!\is_array($geoJson)) {
return null;
}

if (
isset($geoJson['type']) &&
$geoJson['type'] === 'Feature' &&
isset($geoJson['geometry']['coordinates'])
) {
return $geoJson['geometry']['coordinates'][1] . ',' . $geoJson['geometry']['coordinates'][0];
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public function onIndexing(NodesSourcesIndexingEvent $event): void
$assoc[SolariumNodeSource::TYPE_DISCRIMINATOR] = SolariumNodeSource::DOCUMENT_TYPE;
// Need a nodeSourceId field
$assoc[SolariumNodeSource::IDENTIFIER_KEY] = $nodeSource->getId();
$assoc['node_type_s'] = $node->getNodeType()->getName();
$assoc['node_type_s'] = $nodeSource->getNodeTypeName();
$assoc['node_name_s'] = $node->getNodeName();
$assoc['slug_s'] = $node->getNodeName();
$assoc['node_status_i'] = $node->getStatus();
$assoc['node_visible_b'] = $node->isVisible();
$assoc['node_reachable_b'] = $nodeSource->isReachable();

// Need a locale field
$locale = $nodeSource->getTranslation()->getLocale();
Expand Down Expand Up @@ -109,18 +111,42 @@ function (Tag $tag) {
$allOut = array_filter(array_unique($allOut));
// Use all_tags_slugs_ss to be compatible with other data types
$assoc['all_tags_slugs_ss'] = $allOut;
}

$criteria = new Criteria();
$criteria->andWhere(Criteria::expr()->eq("type", AbstractField::BOOLEAN_T));
$booleanFields = $node->getNodeType()->getFields()->matching($criteria);

/** @var NodeTypeField $booleanField */
foreach ($booleanFields as $booleanField) {
$name = $booleanField->getName();
$name .= '_b';
$getter = $booleanField->getGetterName();
$assoc[$name] = $nodeSource->$getter();
$booleanFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isBoolean();
});
$this->indexSuffixedFields($booleanFields, '_b', $nodeSource, $assoc);

$numberFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isInteger();
});
$this->indexSuffixedFields($numberFields, '_i', $nodeSource, $assoc);

$decimalFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isDecimal();
});
$this->indexSuffixedFields($decimalFields, '_f', $nodeSource, $assoc);

$stringFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isEnum() || $field->isCountry() || $field->isColor() || $field->isEmail();
});
$this->indexSuffixedFields($stringFields, '_s', $nodeSource, $assoc);

$dateTimeFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isDate() || $field->isDateTime();
});
$this->indexSuffixedFields($dateTimeFields, '_dt', $nodeSource, $assoc);

$pointFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) {
return $field->isGeoTag();
});
foreach ($pointFields as $field) {
$name = $field->getName();
$name .= '_p';
$getter = $field->getGetterName();
$value = $nodeSource->$getter();
$assoc[$name] = $this->formatGeoJsonFeature($value);
}
}

$searchableFields = $node->getNodeType()->getSearchableFields();
Expand Down Expand Up @@ -156,6 +182,28 @@ function (Tag $tag) {
$event->setAssociations($assoc);
}

/**
* @param iterable<NodeTypeField> $fields
* @param string $suffix
* @param NodesSources $nodeSource
* @param array $assoc
* @return void
*/
protected function indexSuffixedFields(iterable $fields, string $suffix, NodesSources $nodeSource, array &$assoc): void
{
foreach ($fields as $field) {
$name = $field->getName();
$name .= $suffix;
$getter = $field->getGetterName();
$value = $nodeSource->$getter();
if ($value instanceof \DateTimeInterface) {
$assoc[$name] = $this->formatDateTimeToUTC($value);
} elseif (null !== $value) {
$assoc[$name] = $value;
}
}
}

/**
* @param NodesSources $source
* @return bool
Expand Down

0 comments on commit a62c29c

Please sign in to comment.