jenkins-bot has submitted this change and it was merged. Change subject: Implement new search hooks from core ......................................................................
Implement new search hooks from core Bug: T139021 Change-Id: Iaf5d6280724fe388040c277d067f7a913bf9e29e --- M extension.json A includes/CoordinatesIndexField.php A includes/GeoPointIndexField.php M includes/Hooks.php 4 files changed, 117 insertions(+), 36 deletions(-) Approvals: Smalyshev: Looks good to me, approved MaxSem: Looks good to me, approved EBernhardson: Looks good to me, approved jenkins-bot: Verified diff --git a/extension.json b/extension.json index 4f9376e..f92678f 100644 --- a/extension.json +++ b/extension.json @@ -27,9 +27,11 @@ "CoordinatesOutput": "includes/BC.php", "GeoData\\BoundingBox": "includes/BoundingBox.php", "GeoData\\Coord": "includes/Coord.php", + "GeoData\\CoordinatesIndexField" : "includes/CoordinatesIndexField.php", "GeoData\\CoordinatesOutput": "includes/CoordinatesOutput.php", "GeoData\\CoordinatesParserFunction": "includes/CoordinatesParserFunction.php", "GeoData\\GeoData": "includes/GeoData.body.php", + "GeoData\\GeoPointIndexField" : "includes/GeoPointIndexField.php", "GeoData\\Globe": "includes/Globe.php", "GeoData\\Hooks": "includes/Hooks.php", "GeoData\\Math": "includes/Math.php", @@ -42,8 +44,8 @@ "LinksUpdate": "GeoData\\Hooks::onLinksUpdate", "FileUpload": "GeoData\\Hooks::onFileUpload", "OutputPageParserOutput": "GeoData\\Hooks::onOutputPageParserOutput", - "CirrusSearchMappingConfig": "GeoData\\Hooks::onCirrusSearchMappingConfig", - "CirrusSearchBuildDocumentParse": "GeoData\\Hooks::onCirrusSearchBuildDocumentParse", + "SearchIndexFields": "GeoData\\Hooks::onSearchIndexFields", + "SearchDataForIndex": "GeoData\\Hooks::onSearchDataForIndex", "ParserTestTables": "GeoData\\Hooks::onParserTestTables", "ApiQuery::moduleManager": "GeoData\\Hooks::onApiQueryModuleManager" }, diff --git a/includes/CoordinatesIndexField.php b/includes/CoordinatesIndexField.php new file mode 100644 index 0000000..a90e6b2 --- /dev/null +++ b/includes/CoordinatesIndexField.php @@ -0,0 +1,46 @@ +<?php +namespace GeoData; + +use CirrusSearch\Search\NestedIndexField; +use CirrusSearch\SearchConfig; +use SearchEngine; +use SearchIndexField; + +/** + * Nested type for CirrusSearch mapping + */ +class CoordinatesIndexField extends NestedIndexField { + /** + * @param string $name name of the field + * @param SearchConfig $config CirrusSearch config + */ + public function __construct( $name, SearchConfig $config ) { + parent::__construct( $name, $this->typeName, $config ); + } + + /** + * Builds a new CoordinatesIndexField nested field + * @param string $name field name + * @param SearchConfig + * @param SearchEngine $engine + * @return CoordinatesIndexField + */ + public static function build( $name, SearchConfig $config, SearchEngine $engine ) { + $nested = new self( $name, $config ); + $nested->addSubfield( 'coord', new GeoPointIndexField( 'coord', $config ) ); + // Setting analyzer to keyword is similar to index => not_analyzed + $keywords = ['globe', 'type', 'country', 'region']; + foreach( $keywords as $keyword ) { + $nested->addSubfield( $keyword, $engine->makeSearchFieldMapping( $keyword, + SearchIndexField::INDEX_TYPE_KEYWORD ) ); + } + $nested->addSubfield( 'primary', $engine->makeSearchFieldMapping( 'primary', + SearchIndexField::INDEX_TYPE_BOOL ) ); + $nested->addSubfield( 'dim', $engine->makeSearchFieldMapping( 'dim', + SearchIndexField::INDEX_TYPE_NUMBER ) ); + $name = $engine->makeSearchFieldMapping( 'name', SearchIndexField::INDEX_TYPE_TEXT ); + $name->setFlag( SearchIndexField::FLAG_NO_INDEX ); + $nested->addSubfield( 'name', $name ); + return $nested; + } +} diff --git a/includes/GeoPointIndexField.php b/includes/GeoPointIndexField.php new file mode 100644 index 0000000..b9c7bff --- /dev/null +++ b/includes/GeoPointIndexField.php @@ -0,0 +1,39 @@ +<?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'; + + + /** + * @param string $name name of the field + * @param SearchConfig $config CirrusSearch config + */ + public function __construct( $name, SearchConfig $config ) { + parent::__construct( $name, $this->typeName, $config ); + } + + /** + * @param SearchConfig $engine + * @return array elasticsearch mapping + */ + public function getMapping( SearchEngine $engine ) { + $fields = parent::getMapping( $engine ); + // Used by the geo distance query to run bounding box + // optimization query + // @fixme: lat_lon will be removed in elastic 5x + $fields['lat_lon'] = true; + return $fields; + } +} + diff --git a/includes/Hooks.php b/includes/Hooks.php index 6fb7b2f..8ce3092 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -16,6 +16,8 @@ use Title; use User; use WikiPage; +use SearchEngine; +use ContentHandler; /** * Hook handlers @@ -238,50 +240,42 @@ } /** - * CirrusSearchMappingConfig hook handler + * Search index fields hook handler * Adds our stuff to CirrusSearch/Elasticsearch schema * - * @param array $config + * @param array $fields + * @param SearchEngine $engine */ - public static function onCirrusSearchMappingConfig( array &$config ) { + public static function onSearchIndexFields( array &$fields, SearchEngine $engine ) { global $wgGeoDataUseCirrusSearch, $wgGeoDataBackend; - if ( !$wgGeoDataUseCirrusSearch && $wgGeoDataBackend != 'elastic' ) { - return; + if ( $engine instanceof \CirrusSearch + && ( $wgGeoDataUseCirrusSearch || $wgGeoDataBackend == 'elastic' ) + ) { + /** + * @var \CirrusSearch $engine + */ + $fields['coordinates'] = CoordinatesIndexField::build( 'coordinates', $engine->getConfig(), $engine ); + } else { + // Unsupported SearchEngine or explicitly disabled by config } - $pageConfig = $config['page']; - - $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' ], - ], - ]; - $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; if ( ( $wgGeoDataUseCirrusSearch || $wgGeoDataBackend == 'elastic' ) ) { @@ -297,7 +291,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: merged Gerrit-Change-Id: Iaf5d6280724fe388040c277d067f7a913bf9e29e Gerrit-PatchSet: 7 Gerrit-Project: mediawiki/extensions/GeoData Gerrit-Branch: master Gerrit-Owner: DCausse <dcau...@wikimedia.org> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: DCausse <dcau...@wikimedia.org> Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org> Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com> Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits