-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #832 from City-of-Helsinki/UHF-10643
UHF-10643: Add suggestions search api index.
- Loading branch information
Showing
13 changed files
with
470 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
uuid: 1a06b7cd-eb89-4e5d-bb5d-2bfb922b9543 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
config: | ||
- search_api.server.default | ||
module: | ||
- helfi_annif | ||
- helfi_etusivu | ||
- helfi_react_search | ||
id: suggestions | ||
name: Suggestions | ||
description: '' | ||
read_only: false | ||
field_settings: | ||
keywords: | ||
label: Keywords | ||
datasource_id: 'entity:suggested_topics' | ||
property_path: keywords_scored | ||
type: scored_item | ||
parent_bundle: | ||
label: 'Parent bundle' | ||
datasource_id: 'entity:suggested_topics' | ||
property_path: parent_bundle | ||
type: string | ||
dependencies: | ||
module: | ||
- helfi_annif | ||
parent_id: | ||
label: 'Parent ID' | ||
datasource_id: 'entity:suggested_topics' | ||
property_path: parent_id | ||
type: string | ||
dependencies: | ||
module: | ||
- helfi_annif | ||
parent_instance: | ||
label: 'Parent instance' | ||
datasource_id: 'entity:suggested_topics' | ||
property_path: parent_instance | ||
type: string | ||
dependencies: | ||
module: | ||
- helfi_annif | ||
parent_type: | ||
label: 'Parent type' | ||
datasource_id: 'entity:suggested_topics' | ||
property_path: parent_type | ||
type: string | ||
dependencies: | ||
module: | ||
- helfi_annif | ||
datasource_settings: | ||
'entity:suggested_topics': { } | ||
processor_settings: | ||
add_url: { } | ||
aggregated_field: { } | ||
custom_value: { } | ||
district_image_absolute_url: { } | ||
entity_type: { } | ||
language_with_fallback: { } | ||
main_image_url: { } | ||
project_execution_schedule: { } | ||
project_image_absolute_url: { } | ||
project_plan_schedule: { } | ||
rendered_item: { } | ||
scored_reference: { } | ||
uuid_langcode: { } | ||
tracker_settings: | ||
default: | ||
indexing_order: fifo | ||
options: | ||
cron_limit: 50 | ||
delete_on_fail: true | ||
index_directly: true | ||
track_changes_in_references: true | ||
server: default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
public/modules/custom/helfi_annif/src/EventSubscriber/SearchApiSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_annif\EventSubscriber; | ||
|
||
use Drupal\search_api\Event\MappingFieldTypesEvent; | ||
use Drupal\search_api\Event\SearchApiEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Search api event subscriber. | ||
*/ | ||
final class SearchApiSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents(): array { | ||
return [ | ||
SearchApiEvents::MAPPING_FIELD_TYPES => 'mapFieldTypes', | ||
]; | ||
} | ||
|
||
/** | ||
* Map custom field types. | ||
*/ | ||
public function mapFieldTypes(MappingFieldTypesEvent $event): void { | ||
$mapping = &$event->getFieldTypeMapping(); | ||
$mapping['scored_item'] = 'scored_item'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
public/modules/custom/helfi_annif/src/Plugin/search_api/data_type/ScoredItemDataType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_annif\Plugin\search_api\data_type; | ||
|
||
use Drupal\search_api\DataType\DataTypePluginBase; | ||
|
||
/** | ||
* Provides a string data type. | ||
* | ||
* @SearchApiDataType( | ||
* id = "scored_item", | ||
* label = @Translation("Scored item"), | ||
* description = @Translation("Item with score."), | ||
* fallback_type = "string" | ||
* ) | ||
*/ | ||
class ScoredItemDataType extends DataTypePluginBase { | ||
} |
90 changes: 90 additions & 0 deletions
90
...c/modules/custom/helfi_annif/src/Plugin/search_api/processor/ScoredReferenceProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\helfi_annif\Plugin\search_api\processor; | ||
|
||
use Drupal\Core\Entity\ContentEntityInterface; | ||
use Drupal\Core\Field\FieldDefinitionInterface; | ||
use Drupal\helfi_annif\Plugin\Field\FieldType\ScoredEntityReferenceItem; | ||
use Drupal\search_api\Datasource\DatasourceInterface; | ||
use Drupal\search_api\Item\ItemInterface; | ||
use Drupal\search_api\Processor\ProcessorPluginBase; | ||
use Drupal\search_api\Processor\ProcessorProperty; | ||
|
||
/** | ||
* Indexes uuid with langcode. | ||
* | ||
* @SearchApiProcessor( | ||
* id = "scored_reference", | ||
* label = @Translation("Scored references"), | ||
* description = @Translation("Indexes scored references"), | ||
* stages = { | ||
* "add_properties" = 0 | ||
* }, | ||
* locked = true, | ||
* hidden = true, | ||
* ) | ||
*/ | ||
final class ScoredReferenceProcessor extends ProcessorPluginBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPropertyDefinitions(?DataSourceInterface $datasource = NULL) : array { | ||
$properties = []; | ||
|
||
if ($datasource) { | ||
$propertyDefinitions = $datasource->getPropertyDefinitions(); | ||
foreach ($propertyDefinitions as $id => $definition) { | ||
if ( | ||
$definition instanceof FieldDefinitionInterface && | ||
$definition->getType() === 'scored_entity_reference' | ||
) { | ||
$properties[$id . '_scored'] = new ProcessorProperty([ | ||
'label' => $this->t('Scored reference'), | ||
'description' => $this->t('Indexes referenced item labels with a score'), | ||
'type' => 'scored_item', | ||
'processor_id' => $this->getPluginId(), | ||
]); | ||
} | ||
} | ||
} | ||
|
||
return $properties; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addFieldValues(Iteminterface $item) : void { | ||
$entity = $item->getOriginalObject()?->getValue(); | ||
|
||
if (!$entity instanceof ContentEntityInterface) { | ||
return; | ||
} | ||
|
||
$objectSupport = $this->index->getServerInstance()->getBackendId() === 'elasticsearch'; | ||
|
||
foreach ($item->getFields() as $field) { | ||
if ($field->getOriginalType() !== 'scored_item') { | ||
continue; | ||
} | ||
|
||
$property = substr($field->getPropertyPath(), 0, -strlen("_scored")); | ||
|
||
$scoredReferenceField = $entity->get($property); | ||
foreach ($scoredReferenceField as $scoredReference) { | ||
assert($scoredReference instanceof ScoredEntityReferenceItem); | ||
|
||
$value = [ | ||
'score' => (float) $scoredReference->score, | ||
'label' => $scoredReference->entity->id(), | ||
]; | ||
|
||
$field->addValue($objectSupport ? $value : json_encode($value)); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.