DCausse has uploaded a new change for review. https://gerrit.wikimedia.org/r/296935
Change subject: [WIP] Implement new search hooks from core ...................................................................... [WIP] Implement new search hooks from core Bug: T139021 Change-Id: Iaf5d6280724fe388040c277d067f7a913bf9e29e Depends-On: I621704e4edafdf39a028d7c8a06cb1829b3a7cb6 Depends-On: I82a82526e2e254edc1fa7d861d7ac23d9cf07d1c --- M GeoData.php A includes/CirrusGeoPointIndexField.php A includes/GeoPointIndexField.php M includes/Hooks.php 4 files changed, 129 insertions(+), 39 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/GeoData refs/changes/35/296935/1 diff --git a/GeoData.php b/GeoData.php index 3b27b16..fa2289c 100644 --- a/GeoData.php +++ b/GeoData.php @@ -30,6 +30,7 @@ $wgAutoloadClasses['GeoData\Hooks'] = "$dir/includes/Hooks.php"; $wgAutoloadClasses['GeoData\Math'] = "$dir/includes/Math.php"; $wgAutoloadClasses['GeoData\Searcher'] = "$dir/includes/Searcher.php"; +$wgAutoloadClasses['GeoData\GeoPointIndexField'] = "$dir/includes/GeoPointIndexField.php"; $wgMessagesDirs['GeoData'] = __DIR__ . '/i18n'; $wgExtensionMessagesFiles['GeoData'] = "$dir/GeoData.i18n.php"; @@ -44,8 +45,8 @@ $wgHooks['LinksUpdate'][] = 'GeoData\Hooks::onLinksUpdate'; $wgHooks['FileUpload'][] = 'GeoData\Hooks::onFileUpload'; $wgHooks['OutputPageParserOutput'][] = 'GeoData\Hooks::onOutputPageParserOutput'; -$wgHooks['CirrusSearchMappingConfig'][] = 'GeoData\Hooks::onCirrusSearchMappingConfig'; -$wgHooks['CirrusSearchBuildDocumentParse'][] = 'GeoData\Hooks::onCirrusSearchBuildDocumentParse'; +$wgHooks['SearchIndexFields'][] = 'GeoData\Hooks::onSearchIndexFields'; +$wgHooks['SearchDataForIndex'][] = 'GeoData\Hooks::onSearchDataForIndex'; $wgHooks['ParserTestTables'][] = 'GeoData\Hooks::onParserTestTables'; // Use the proper search backend diff --git a/includes/CirrusGeoPointIndexField.php b/includes/CirrusGeoPointIndexField.php new file mode 100644 index 0000000..090a4a6 --- /dev/null +++ b/includes/CirrusGeoPointIndexField.php @@ -0,0 +1,32 @@ +<?php +namespace GeoData; + +use \CirrusSearch\Search\CirrusIndexField; + +class GeoPointIndexField extends CirrusIndexField { + protected $typeName = 'geo_point'; + + protected $indexLatLon = false; + protected $precisionStep = null; + + protected $ignoreMalformed = false; + + protected $geohash = false; + protected $geohashPrecision = 6; + protected $geohashPrefix = false; + + public GeoPointIndexField( $name, SearchEngine $config ) { + parent::_construct( $name, $this->typeName, $config ); + } + + public function getMapping( SearchEngine $engine ) { + $fields = parent::getMapping(); + if ( $this->indexLatLon ) { + $fields['lat_lon'] = true; + } + + if ( $this->precisionStep !== null ) { + $fields['precision_step'] = $this->precisionStep; + } + } + diff --git a/includes/GeoPointIndexField.php b/includes/GeoPointIndexField.php new file mode 100644 index 0000000..2dd2220 --- /dev/null +++ b/includes/GeoPointIndexField.php @@ -0,0 +1,43 @@ +<?php +namespace GeoData; + +use CirrusSearch\Search\CirrusIndexField; +use CirrusSearch\SearchConfig; +use SearchEngine; + +/** + * GeoPoint type for CirrusSearch mapping + */ +class GeoPointIndexField extends CirrusIndexField { + /** + * @var string + */ + protected $typeName = 'geo_point'; + + /** + * @var array customize fielddata settings + */ + protected $fieldDataOptions; + + /** + * @param string $name name of the field + * @param SearchConfig $config CirrusSearch config + * @param array $fieldDataOptions + */ + public function __construct( $name, SearchConfig $config, array $fieldDataOptions = null ) { + parent::__construct( $name, $this->typeName, $config ); + } + + /** + * @param SearchConfig $engine + * @return array elasticsearch mapping + */ + public function getMapping( SearchEngine $engine ) { + $fields = parent::getMapping( $engine ); + if ( $this->fieldDataOptions !== null ) { + $fields['fielddata'] = $this->fieldDataOptions; + } + return $fields; + } +} + diff --git a/includes/Hooks.php b/includes/Hooks.php index 3fb6f7d..cdf92bd 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -15,6 +15,9 @@ use Title; use User; use WikiPage; +use SearchEngine; +use SearchIndexField; +use ContentHandler; /** * Hook handlers @@ -246,55 +249,66 @@ } /** - * CirrusSearchMappingConfig hook handler + * Search index fields hook handler * Adds our stuff to CirrusSearch/Elasticsearch schema * * @param array $config + * @param SearchEngine $engine */ - public static function onCirrusSearchMappingConfig( array &$config ) { + public static function onSearchIndexFields( array &$fields, SearchEngine $engine ) { global $wgGeoDataUseCirrusSearch, $wgGeoDataBackend, $wgGeoDataCoordinatesCompression; - if ( !$wgGeoDataUseCirrusSearch && $wgGeoDataBackend != 'elastic' ) { - return; - } - $pageConfig = $config['page']; + if ( class_exists( '\CirrusSearch' ) && ( $engine instanceof \CirrusSearch + && ( $wgGeoDataUseCirrusSearch || $wgGeoDataBackend == 'elastic') ) + ) { + /** @var \CirrusSearch\CirrusSearch */ + $cirrus = $engine; - $pageConfig['properties']['coordinates'] = [ - 'type' => 'nested', - 'properties' => [ - 'coord' => [ - 'type' => 'geo_point', - 'lat_lon' => true, - ], - 'globe' => [ 'type' => 'string', 'index' => 'not_analyzed' ], - 'primary' => [ 'type' => 'boolean' ], - 'dim' => [ 'type' => 'float' ], - 'type' => [ 'type' => 'string', 'index' => 'not_analyzed' ], - 'name' => [ 'type' => 'string', 'index' => 'no' ], - 'country' => [ 'type' => 'string', 'index' => 'not_analyzed' ], - 'region' => [ 'type' => 'string', 'index' => 'not_analyzed' ], - ], - ]; - if ( $wgGeoDataCoordinatesCompression ) { - $pageConfig['properties']['coordinates']['properties']['coord']['fielddata'] = [ - 'format' => 'compressed', - 'precision' => $wgGeoDataCoordinatesCompression, - ]; + $nested = $engine->makeSearchFieldMapping( 'coordinates', + SearchIndexField::INDEX_TYPE_NESTED ); + $fieldDataOptions = null; + if ( $wgGeoDataCoordinatesCompression ) { + $fieldDataOptions = [ + 'format' => 'compressed', + 'precision' => $wgGeoDataCoordinatesCompression, + ]; + } + $nested->addSubfield( 'coord', new GeoPointIndexField( 'coord', $cirrus->getConfig(), + $fieldDataOptions ) ); + // Setting analyzer to keyword is similar to index => not_analyzed + $keywords = ['globe', 'type', 'country', 'region']; + foreach( $keywords as $keyword ) { + $nested->addSubfield( $keyword, $cirrus->makeSearchFieldMapping( $keyword, + SearchIndexField::INDEX_TYPE_KEYWORD ) ); + } + // We need a BOOLEAN type + $nested->addSubfield( 'primary', $cirrus->makeSearchFieldMapping( 'primary', + SearchIndexField::INDEX_TYPE_BOOL ) ); + $nested->addSubfield( 'dim', $cirrus->makeSearchFieldMapping( 'dim', + SearchIndexField::INDEX_TYPE_NUMBER ) ); + $name = $cirrus->makeSearchFieldMapping( 'name', + SearchIndexField::INDEX_TYPE_TEXT ); + $name->setFlag( SearchIndexField::FLAG_NO_INDEX ); + $nested->addSubfield( 'name', $name ); + + $fields[$nested->getName()] = $nested; + } else { + // Unsupported SearchEngine or explicitely disabled by config } - $config['page'] = $pageConfig; } /** - * CirrusSearchBuildDocumentParse hook handler + * SearchDataForIndex hook handler * - * @param \Elastica\Document $doc - * @param Title $title - * @param Content $content + * @param array[] $fields + * @param ContentHandler $contentHandler + * @param WikiPage $page * @param ParserOutput $parserOutput + * @param SearchEngine $searchEngine */ - public static function onCirrusSearchBuildDocumentParse( \Elastica\Document $doc, - Title $title, - Content $content, - ParserOutput $parserOutput ) + public static function onSearchDataForIndex( array &$fields, ContentHandler $contentHandler, + WikiPage $page, + ParserOutput $parserOutput, + SearchEngine $searchEngine ) { global $wgGeoDataUseCirrusSearch, $wgGeoDataBackend; @@ -311,7 +325,7 @@ } $coords[] = self::coordToElastic( $coord ); } - $doc->set( 'coordinates', $coords ); + $fields['coordinates'] = $coords; } } -- To view, visit https://gerrit.wikimedia.org/r/296935 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iaf5d6280724fe388040c277d067f7a913bf9e29e Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/GeoData Gerrit-Branch: master Gerrit-Owner: DCausse <dcau...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits