From 13041a2c775f3a6baf9659d328d574c21b8b4c6b Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 26 Sep 2023 01:16:42 +0200 Subject: [PATCH] feat(Solr): Added multiple geojson point indexing BREAKING CHANGE: Make sure your Solr managed-schema has a dynamic field named `*_ps` with type `location` multiValued: ```xml ``` --- docker/solr/managed-schema.xml | 1 + .../Subscriber/AbstractIndexingSubscriber.php | 24 +++++++++++++++++++ .../DefaultNodesSourcesIndexingSubscriber.php | 19 +++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/docker/solr/managed-schema.xml b/docker/solr/managed-schema.xml index e1a6dd28..a083247b 100644 --- a/docker/solr/managed-schema.xml +++ b/docker/solr/managed-schema.xml @@ -157,6 +157,7 @@ + diff --git a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/AbstractIndexingSubscriber.php b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/AbstractIndexingSubscriber.php index 961c5bd3..92ceead6 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/AbstractIndexingSubscriber.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/AbstractIndexingSubscriber.php @@ -36,4 +36,28 @@ protected function formatGeoJsonFeature(mixed $geoJson): ?string } return null; } + + protected function formatGeoJsonFeatureCollection(mixed $geoJson): ?array + { + 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'] === 'FeatureCollection' && + isset($geoJson['features']) && + \count($geoJson['features']) > 0 + ) { + return array_filter(array_map(function ($feature) { + return $this->formatGeoJsonFeature($feature); + }, $geoJson['features'])); + } + return null; + } } diff --git a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php index 3f9c14c4..91a84c47 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php @@ -137,6 +137,10 @@ function (Tag $tag) { }); $this->indexSuffixedFields($dateTimeFields, '_dt', $nodeSource, $assoc); + /* + * Make sure your Solr managed-schema has a field named `*_p` with type `location` singleValued + * + */ $pointFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) { return $field->isGeoTag(); }); @@ -147,6 +151,21 @@ function (Tag $tag) { $value = $nodeSource->$getter(); $assoc[$name] = $this->formatGeoJsonFeature($value); } + + /* + * Make sure your Solr managed-schema has a field named `*_ps` with type `location` multiValued + * + */ + $multiPointFields = $node->getNodeType()->getFields()->filter(function (NodeTypeField $field) { + return $field->isMultiGeoTag(); + }); + foreach ($multiPointFields as $field) { + $name = $field->getName(); + $name .= '_ps'; + $getter = $field->getGetterName(); + $value = $nodeSource->$getter(); + $assoc[$name] = $this->formatGeoJsonFeatureCollection($value); + } } $searchableFields = $node->getNodeType()->getSearchableFields();