-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
645 additions
and
18 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
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
31 changes: 31 additions & 0 deletions
31
src/Persistence/Search/Query/DelegatingFacetQueryBuilder.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,31 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query; | ||
|
||
use Elastica\Query\AbstractQuery; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\FacetConfig; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\FacetType; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\PropertyConstraints; | ||
|
||
class DelegatingFacetQueryBuilder implements FacetQueryBuilder { | ||
|
||
/** | ||
* @var array<string, FacetQueryBuilder> | ||
*/ | ||
private array $buildersPerType; | ||
|
||
public function addBuilder( FacetType $type, FacetQueryBuilder $builder ): void { | ||
$this->buildersPerType[$type->value] = $builder; | ||
} | ||
|
||
public function buildQuery( FacetConfig $config, PropertyConstraints $constraints ): ?AbstractQuery { | ||
if ( !array_key_exists( $config->type->value, $this->buildersPerType ) ) { | ||
throw new \RuntimeException( 'No query builder for facet type ' . $config->type->value ); | ||
} | ||
|
||
return $this->buildersPerType[$config->type->value]->buildQuery( $config, $constraints ); | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query; | ||
|
||
use Elastica\Query\AbstractQuery; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\FacetConfig; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\PropertyConstraints; | ||
|
||
interface FacetQueryBuilder { | ||
|
||
public function buildQuery( FacetConfig $config, PropertyConstraints $constraints ): ?AbstractQuery; | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query; | ||
|
||
use Elastica\Query; | ||
use Elastica\Query\AbstractQuery; | ||
use Wikibase\DataModel\Entity\ItemId; | ||
use Wikibase\DataModel\Entity\PropertyId; | ||
|
||
class ItemTypeQueryBuilder { | ||
|
||
public function __construct( | ||
private readonly PropertyId $itemTypeProperty | ||
) { | ||
} | ||
|
||
public function buildQuery( array $itemTypes ): AbstractQuery { | ||
return new Query\Terms( | ||
'wbfs_' . $this->itemTypeProperty->getSerialization(), | ||
array_map( | ||
fn( ItemId $itemType ) => $itemType->getSerialization(), | ||
$itemTypes | ||
) | ||
); | ||
} | ||
|
||
} |
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,56 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query; | ||
|
||
use Elastica\Query; | ||
use Elastica\Query\AbstractQuery; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\FacetConfig; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\PropertyConstraints; | ||
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; | ||
|
||
class ListFacetQueryBuilder implements FacetQueryBuilder { | ||
|
||
public function __construct( | ||
private readonly PropertyDataTypeLookup $dataTypeLookup | ||
) { | ||
} | ||
|
||
public function buildQuery( FacetConfig $config, PropertyConstraints $constraints ): ?AbstractQuery { | ||
$name = 'wbfs_' . $config->propertyId->getSerialization(); | ||
|
||
return match ( $this->dataTypeLookup->getDataTypeIdForProperty( $config->propertyId ) ) { | ||
'string' => $this->buildStringQuery( $name, $constraints ), | ||
'wikibase-item' => $this->buildStringQuery( $name, $constraints ), | ||
'quantity' => $this->buildQuantityQuery( $name, $constraints ), | ||
default => null | ||
}; | ||
} | ||
|
||
private function buildStringQuery( string $name, PropertyConstraints $constraints ): AbstractQuery { | ||
return new Query\Terms( | ||
$name, | ||
$this->getFacetValues( $constraints ) | ||
); | ||
} | ||
|
||
private function getFacetValues( PropertyConstraints $constraints ): array { | ||
if ( $constraints->getOrSelectedValues() !== [] ) { | ||
return $constraints->getOrSelectedValues(); | ||
} | ||
|
||
return $constraints->getAndSelectedValues(); | ||
} | ||
|
||
private function buildQuantityQuery( string $name, PropertyConstraints $constraints ): AbstractQuery { | ||
return new Query\Terms( | ||
$name, | ||
array_map( | ||
fn( $value ) => (float)$value, | ||
$this->getFacetValues( $constraints ) | ||
) | ||
); | ||
} | ||
|
||
} |
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,50 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace ProfessionalWiki\WikibaseFacetedSearch\Persistence\Search\Query; | ||
|
||
use Elastica\Query; | ||
use Elastica\Query\AbstractQuery; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\FacetConfig; | ||
use ProfessionalWiki\WikibaseFacetedSearch\Application\PropertyConstraints; | ||
use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; | ||
|
||
class RangeFacetQueryBuilder implements FacetQueryBuilder { | ||
|
||
public function __construct( | ||
private readonly PropertyDataTypeLookup $dataTypeLookup | ||
) { | ||
} | ||
|
||
public function buildQuery( FacetConfig $config, PropertyConstraints $constraints ): ?AbstractQuery { | ||
$name = 'wbfs_' . $config->propertyId->getSerialization(); | ||
|
||
return match ( $this->dataTypeLookup->getDataTypeIdForProperty( $config->propertyId ) ) { | ||
'quantity' => $this->buildQuantityQuery( $name, $constraints ), | ||
'time' => $this->buildTimeQuery( $name, $constraints ), | ||
default => null | ||
}; | ||
} | ||
|
||
private function buildQuantityQuery( string $name, PropertyConstraints $constraints ): AbstractQuery { | ||
return new Query\Range( | ||
$name, | ||
[ | ||
'gte' => $constraints->getInclusiveMinimum(), | ||
'lte' => $constraints->getInclusiveMaximum() | ||
] | ||
); | ||
} | ||
|
||
private function buildTimeQuery( string $name, PropertyConstraints $constraints ): AbstractQuery { | ||
return new Query\Range( | ||
$name, | ||
[ | ||
'gte' => $constraints->getInclusiveMinimum() === null ? null : (int)$constraints->getInclusiveMinimum() . '-01-01', | ||
'lte' => $constraints->getInclusiveMaximum() === null ? null : (int)$constraints->getInclusiveMaximum() . '-12-31', | ||
] | ||
); | ||
} | ||
|
||
} |
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
Oops, something went wrong.