Skip to content

Commit

Permalink
Merge pull request #149 from ProfessionalWiki/index-item-type-refactor
Browse files Browse the repository at this point in the history
Index item type refactor
  • Loading branch information
JeroenDeDauw authored Jan 23, 2025
2 parents 38fc53b + 1c92d81 commit e14db07
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/Application/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ public function getItemTypes(): array {
return array_values( $itemTypes );
}

public function isComplete(): bool {
return $this->itemTypeProperty !== null;
}

}
6 changes: 4 additions & 2 deletions src/Application/StatementListTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function translateStatements( StatementList $statements ): array {
}

$propertyIds = $this->getPropertiesToIndex( $itemType );
$propertyIds[] = $this->config->getItemTypeProperty();

$values = [];

Expand All @@ -45,12 +44,15 @@ public function translateStatements( StatementList $statements ): array {
* @return PropertyId[]
*/
private function getPropertiesToIndex( ItemId $itemType ): array {
return array_values(
$properties = array_values(
array_map(
fn( FacetConfig $config ) => $config->propertyId,
$this->config->getFacetConfigForItemType( $itemType )
)
);
$properties[] = $this->config->getItemTypeProperty();

return $properties;
}

private function translatePropertyStatements( StatementList $statements, PropertyId $propertyId ): array {
Expand Down
14 changes: 9 additions & 5 deletions src/EntryPoints/WikibaseFacetedSearchHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ public static function onSearchIndexFields( array &$fields, SearchEngine $engine
return;
}

$fields = WikibaseFacetedSearchExtension::getInstance()->newSearchIndexFieldsBuilder( $engine )->createFields()
+ $fields;
if ( WikibaseFacetedSearchExtension::getInstance()->getConfig()->isComplete() ) {
$fields = WikibaseFacetedSearchExtension::getInstance()->newSearchIndexFieldsBuilder( $engine )->createFieldObjects()
+ $fields;
}
}

public static function onSearchDataForIndex(
Expand All @@ -165,9 +167,11 @@ public static function onSearchDataForIndex(
return;
}

$fields = WikibaseFacetedSearchExtension::getInstance()->newStatementListTranslator()->translateStatements(
WikibaseFacetedSearchExtension::getInstance()->newStatementsLookup()->getStatements( $page )
) + $fields;
if ( WikibaseFacetedSearchExtension::getInstance()->getConfig()->isComplete() ) {
$fields = WikibaseFacetedSearchExtension::getInstance()->newStatementListTranslator()->translateStatements(
WikibaseFacetedSearchExtension::getInstance()->newStatementsLookup()->getStatements( $page )
) + $fields;
}
}

}
12 changes: 3 additions & 9 deletions src/Persistence/Search/SearchIndexFieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ public function __construct(
}

/**
* @return array<string, SearchIndexField>
* @return array<string, SearchIndexField> Field objects indexed by field/index name
*/
public function createFields(): array {
try {
$itemType = $this->config->getItemTypeProperty();
} catch ( Exception ) {
return [];
}

return $this->makeItemTypeSearchFieldMapping( $itemType )
public function createFieldObjects(): array {
return $this->makeItemTypeSearchFieldMapping( $this->config->getItemTypeProperty() )
+ $this->makeFacetSearchFieldMappings( $this->config->getFacets()->asArray() );
}

Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/Application/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ public function testGetItemTypes(): void {
);
}

public function testWithoutItemTypeIsNotComplete(): void {
$this->assertFalse( ( new Config() )->isComplete() );
}

public function testWithOnlyItemTypeIsComplete(): void {
$config = new Config( itemTypeProperty: new NumericPropertyId( 'P1' ) );

$this->assertTrue( $config->isComplete() );
}

public function testFullConfigIsComplete(): void {
$config = new Config(
linkTargetSitelinkSiteId: 'enwiki',
itemTypeProperty: new NumericPropertyId( 'P1' ),
facets: new FacetConfigList(
new FacetConfig( new ItemId( 'Q1' ), new NumericPropertyId( 'P1' ), FacetType::LIST ),
new FacetConfig( new ItemId( 'Q2' ), new NumericPropertyId( 'P2' ), FacetType::LIST )
)
);

$this->assertTrue( $config->isComplete() );
}

}
18 changes: 11 additions & 7 deletions tests/phpunit/Persistence/Search/SearchIndexFieldsBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public function setUp(): void {
$this->cirrusSearch = new CirrusSearch();
}

public function testEmptyConfigReturnsNoFields(): void {
public function testEmptyConfigThrowsException(): void {
$builder = $this->newBuilder( new Config() );

$this->assertSame( [], $builder->createFields() );
$this->expectException( \RuntimeException::class );

$builder->createFieldObjects();
}

private function newBuilder( Config $config ): SearchIndexFieldsBuilder {
Expand All @@ -44,15 +46,17 @@ private function newBuilder( Config $config ): SearchIndexFieldsBuilder {
);
}

public function testConfigWithoutItemTypeReturnsNoFields(): void {
public function testConfigWithoutItemTypeThrowsException(): void {
$builder = $this->newBuilder( new Config(
facets: new FacetConfigList(
$this->newFacetConfig( 'Q1', 'P100' ),
$this->newFacetConfig( 'Q1', 'P200' ),
)
) );

$this->assertSame( [], $builder->createFields() );
$this->expectException( \RuntimeException::class );

$builder->createFieldObjects();
}

private function newDataTypeLookup(): InMemoryDataTypeLookup {
Expand Down Expand Up @@ -86,7 +90,7 @@ public function testReturnsFieldForItemType(): void {
[
'wbfs_P1' => new AggregatableKeywordIndexField( 'wbfs_P1', SearchIndexField::INDEX_TYPE_KEYWORD, $this->cirrusSearch->getConfig() )
],
$builder->createFields()
$builder->createFieldObjects()
);
}

Expand All @@ -112,7 +116,7 @@ public function testReturnsFieldsForConfig(): void {
'wbfs_P300' => new DatetimeIndexField( 'wbfs_P300', SearchIndexField::INDEX_TYPE_DATETIME, $this->cirrusSearch->getConfig() ),
'wbfs_P400' => new AggregatableKeywordIndexField( 'wbfs_P400', SearchIndexField::INDEX_TYPE_KEYWORD, $this->cirrusSearch->getConfig() )
],
$builder->createFields()
$builder->createFieldObjects()
);
}

Expand Down Expand Up @@ -146,7 +150,7 @@ public function testDoesNotReturnFieldsForMissingProperties(): void {
'wbfs_P100' => new NumberIndexField( 'wbfs_P100', SearchIndexField::INDEX_TYPE_NUMBER, $this->cirrusSearch->getConfig() ),
'wbfs_P200' => new AggregatableKeywordIndexField( 'wbfs_P200', SearchIndexField::INDEX_TYPE_KEYWORD, $this->cirrusSearch->getConfig() )
],
$builder->createFields()
$builder->createFieldObjects()
);
}

Expand Down

0 comments on commit e14db07

Please sign in to comment.