Smalyshev has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/336911 )

Change subject: [WIP] Add indexing for descriptions
......................................................................

[WIP] Add indexing for descriptions

Descriptions follow the same pattern as labels, 'descriptions'
field includes per-language fields. Only one description per language.

Bug: T125500
Change-Id: I459c619daf8f7e6c47c1a486428f0ca38f7c8faf
---
M repo/Wikibase.php
M repo/includes/Search/Elastic/ElasticTermResult.php
M repo/includes/Search/Elastic/EntitySearchElastic.php
M repo/includes/Search/Elastic/Fields/DescriptionProviderFieldDefinitions.php
A repo/includes/Search/Elastic/Fields/DescriptionsField.php
M repo/tests/phpunit/includes/Search/Elastic/ElasticTermResultTest.php
M repo/tests/phpunit/includes/Search/Elastic/EntitySearchElasticTest.php
7 files changed, 79 insertions(+), 61 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/11/336911/1

diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index 94007b7..2c18d85 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -212,10 +212,6 @@
                                $entitySearchHelper = new 
Wikibase\Repo\Search\Elastic\EntitySearchElastic(
                                        
$repo->getLanguageFallbackChainFactory(),
                                        $repo->getEntityIdParser(),
-                                       new 
Wikibase\Lib\Store\LanguageFallbackLabelDescriptionLookup(
-                                               $repo->getTermLookup(),
-                                               
$repo->getLanguageFallbackChainFactory()->newFromLanguage( 
$repo->getUserLanguage() )
-                                       ),
                                        $repo->getContentModelMappings(),
                                        $mainModule->getRequest(),
                                        $settings
diff --git a/repo/includes/Search/Elastic/ElasticTermResult.php 
b/repo/includes/Search/Elastic/ElasticTermResult.php
index acb8af4..0e2db98 100644
--- a/repo/includes/Search/Elastic/ElasticTermResult.php
+++ b/repo/includes/Search/Elastic/ElasticTermResult.php
@@ -6,7 +6,6 @@
 use CirrusSearch\Search\SearchContext;
 use Wikibase\DataModel\Entity\EntityIdParser;
 use Wikibase\DataModel\Entity\EntityIdParsingException;
-use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\DataModel\Term\Term;
 use Wikibase\Lib\Interactors\TermSearchResult;
 
@@ -15,11 +14,6 @@
  * a Wikibase entity by its label or alias.
  */
 class ElasticTermResult implements ResultsType {
-       /**
-        * @var LabelDescriptionLookup
-        */
-       private $labelDescriptionLookup;
-
        /**
         * @var EntityIdParser
         */
@@ -32,11 +26,9 @@
         */
        private $languageCodes;
 
-       public function __construct( EntityIdParser $idParser,
-                                    LabelDescriptionLookup 
$labelDescriptionLookup, $languageCodes ) {
+       public function __construct( EntityIdParser $idParser, array 
$languageCodes ) {
                $this->idParser = $idParser;
                $this->languageCodes = $languageCodes;
-               $this->labelDescriptionLookup = $labelDescriptionLookup;
        }
 
        /**
@@ -48,6 +40,7 @@
                $fields = [ 'namespace', 'title' ];
                foreach ( $this->languageCodes as $code ) {
                        $fields[] = "labels.$code";
+                       $fields[] = "descriptions.$code";
                }
                return $fields;
        }
@@ -102,11 +95,12 @@
        /**
         * Locate label for display among the source data, basing on fallback 
chain.
         * @param array $sourceData
+        * @param string $field
         * @return null|Term
         */
-       private function findLabelForDisplay( $sourceData ) {
+       private function findLabelForDisplay( $sourceData, $field ) {
                foreach ( $this->languageCodes as $code ) {
-                       if ( !empty( $sourceData['labels'][$code] ) && 
$sourceData['labels'][$code][0] !== '' ) {
+                       if ( !empty( $sourceData[$field][$code] ) && 
$sourceData[$field][$code][0] !== '' ) {
                                return new Term( $code, 
$sourceData['labels'][$code][0] );
                        }
                }
@@ -131,7 +125,8 @@
 
                        $highlight = $r->getHighlights();
                        $matchedTermType = 'label';
-                       $displayLabel = $this->findLabelForDisplay( $sourceData 
);
+                       $displayLabel = $this->findLabelForDisplay( 
$sourceData, 'labels' );
+                       $displayDescription = $this->findLabelForDisplay( 
$sourceData, 'descriptions' );
 
                        if ( !empty( $highlight['title'] ) ) {
                                $matchedTermType = 'entityId';
@@ -162,8 +157,6 @@
                                // This should not happen, but just in case, 
it's better to return something
                                $displayLabel = $matchedTerm;
                        }
-                       // TODO: eventually this will be fetched from ES.
-                       $displayDescription = 
$this->labelDescriptionLookup->getDescription( $entityId );
 
                        $results[$entityId->getSerialization()] = new 
TermSearchResult(
                                $matchedTerm, $matchedTermType, $entityId, 
$displayLabel,
diff --git a/repo/includes/Search/Elastic/EntitySearchElastic.php 
b/repo/includes/Search/Elastic/EntitySearchElastic.php
index d468aea..ece804e 100644
--- a/repo/includes/Search/Elastic/EntitySearchElastic.php
+++ b/repo/includes/Search/Elastic/EntitySearchElastic.php
@@ -10,7 +10,6 @@
 use RequestContext;
 use WebRequest;
 use Wikibase\DataModel\Entity\EntityIdParser;
-use Wikibase\DataModel\Services\Lookup\LabelDescriptionLookup;
 use Wikibase\LanguageFallbackChainFactory;
 use Wikibase\Lib\Interactors\TermSearchResult;
 use Wikibase\Repo\Api\EntitySearcher;
@@ -29,11 +28,6 @@
         * @var LanguageFallbackChainFactory
         */
        private $languageChainFactory;
-       /**
-        * @var LabelDescriptionLookup
-        */
-       private $labelDescriptionLookup;
-
        /**
         * @var EntityIdParser
         */
@@ -60,7 +54,6 @@
        /**
         * @param LanguageFallbackChainFactory $languageChainFactory
         * @param EntityIdParser $idParser
-        * @param LabelDescriptionLookup $labelDescriptionLookup
         * @param $contentModelMap
         * @param WebRequest $request
         * @param array $settings
@@ -68,14 +61,12 @@
        public function __construct(
                LanguageFallbackChainFactory $languageChainFactory,
                EntityIdParser $idParser,
-               LabelDescriptionLookup $labelDescriptionLookup,
                $contentModelMap,
                WebRequest $request,
                array $settings
        ) {
                $this->languageChainFactory = $languageChainFactory;
                $this->idParser = $idParser;
-               $this->labelDescriptionLookup = $labelDescriptionLookup;
                $this->contentModelMap = $contentModelMap;
                $this->request = $request;
                $this->settings = $settings;
@@ -203,7 +194,6 @@
                $searcher = new WikibasePrefixSearcher( 0, $limit );
                $searcher->setResultsType( new ElasticTermResult(
                        $this->idParser,
-                       $this->labelDescriptionLookup,
                        $this->fallbackCodes
                ) );
 
diff --git 
a/repo/includes/Search/Elastic/Fields/DescriptionProviderFieldDefinitions.php 
b/repo/includes/Search/Elastic/Fields/DescriptionProviderFieldDefinitions.php
index 6d07ed2..6df0bb5 100644
--- 
a/repo/includes/Search/Elastic/Fields/DescriptionProviderFieldDefinitions.php
+++ 
b/repo/includes/Search/Elastic/Fields/DescriptionProviderFieldDefinitions.php
@@ -25,7 +25,7 @@
        public function getFields() {
                $fields = [];
 
-               // TODO: nothing for now, will add later
+               $fields['descriptions'] = new DescriptionsField( 
$this->languageCodes );
 
                return $fields;
        }
diff --git a/repo/includes/Search/Elastic/Fields/DescriptionsField.php 
b/repo/includes/Search/Elastic/Fields/DescriptionsField.php
new file mode 100644
index 0000000..361fdaa
--- /dev/null
+++ b/repo/includes/Search/Elastic/Fields/DescriptionsField.php
@@ -0,0 +1,71 @@
+<?php
+namespace Wikibase\Repo\Search\Elastic\Fields;
+
+use Flow\Import\SourceStore\Exception;
+use SearchEngine;
+use SearchIndexFieldDefinition;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\AliasesProvider;
+use Wikibase\DataModel\Term\DescriptionsProvider;
+use Wikibase\DataModel\Term\LabelsProvider;
+
+/**
+ * Field which contains per-language specific descriptions.
+ */
+class DescriptionsField implements WikibaseIndexField {
+
+       /**
+        * List of available languages
+        * @var string[]
+        */
+       private $languages;
+
+       public function __construct( $languages ) {
+               $this->languages = $languages;
+       }
+
+       /**
+        * @param SearchEngine $engine
+        * @param string $name
+        * @return null|\SearchIndexField
+        */
+       public function getMappingField( SearchEngine $engine, $name ) {
+               $field = $engine->makeSearchFieldMapping( $name, 
\SearchIndexField::INDEX_TYPE_NESTED );
+               if ( is_callable( [ $field, 'setMergeCallback' ] ) ) {
+                       $field->setMergeCallback( function ( $that ) use ( 
$field ) {
+                               return $field;
+                       } );
+
+               }
+               /**
+                * @var SearchIndexFieldDefinition
+                */
+               foreach ( $this->languages as $language ) {
+                       $langConfig = $engine->makeSearchFieldMapping( 
"$name.$language",
+                               \SearchIndexField::INDEX_TYPE_TEXT );
+
+                       $field->addSubfield( $language, $langConfig );
+               }
+
+               return $field;
+       }
+
+       /**
+        * @param EntityDocument $entity
+        *
+        * @return mixed Get the value of the field to be indexed when a 
page/document
+        *               is indexed. This might be an array with nested data, 
if the field
+        *               is defined with nested type or an int or string for 
simple field types.
+        */
+       public function getFieldData( EntityDocument $entity ) {
+               if ( !( $entity instanceof DescriptionsProvider ) ) {
+                       return [];
+               }
+               $data = [];
+               foreach ( $entity->getDescriptions() as $language => $desc ) {
+                       $data[$language] = $desc->getText();
+               }
+               return $data;
+       }
+
+}
diff --git 
a/repo/tests/phpunit/includes/Search/Elastic/ElasticTermResultTest.php 
b/repo/tests/phpunit/includes/Search/Elastic/ElasticTermResultTest.php
index 5b28efb..d21578f 100644
--- a/repo/tests/phpunit/includes/Search/Elastic/ElasticTermResultTest.php
+++ b/repo/tests/phpunit/includes/Search/Elastic/ElasticTermResultTest.php
@@ -164,21 +164,6 @@
        }
 
        /**
-        * Get a lookup that always returns a pt label and description suffixed 
by the entity ID
-        *
-        * @return LabelDescriptionLookup
-        */
-       private function getMockLabelDescriptionLookup() {
-               $mock = $this->getMockBuilder( LabelDescriptionLookup::class )
-                       ->disableOriginalConstructor()
-                       ->getMock();
-               $mock->expects( $this->any() )
-                       ->method( 'getDescription' )
-                       ->will( $this->returnValue( new Term( 'en', 
'DESCRIPTION' ) ) );
-               return $mock;
-       }
-
-       /**
         * @dataProvider termResultsProvider
         * @param $languages
         * @param $resultData
@@ -187,7 +172,6 @@
        public function testTransformResult( $languages, $resultData, $expected 
) {
                $res = new ElasticTermResult(
                        new BasicEntityIdParser(),
-                       $this->getMockLabelDescriptionLookup(),
                        $languages
                );
 
diff --git 
a/repo/tests/phpunit/includes/Search/Elastic/EntitySearchElasticTest.php 
b/repo/tests/phpunit/includes/Search/Elastic/EntitySearchElasticTest.php
index e860c24..c035026 100644
--- a/repo/tests/phpunit/includes/Search/Elastic/EntitySearchElasticTest.php
+++ b/repo/tests/phpunit/includes/Search/Elastic/EntitySearchElasticTest.php
@@ -14,28 +14,12 @@
  */
 class EntitySearchElasticTest extends MediaWikiTestCase {
 
-       /**
-        * Get a lookup that always returns a fixed label and description
-        *
-        * @return LabelDescriptionLookup
-        */
-       private function getMockLabelDescriptionLookup() {
-               $mock = $this->getMockBuilder( LabelDescriptionLookup::class )
-                               ->disableOriginalConstructor()
-                               ->getMock();
-               $mock->expects( $this->any() )
-                       ->method( 'getDescription' )
-                       ->will( $this->returnValue( new Term( 'en', 
'DESCRIPTION' ) ) );
-               return $mock;
-       }
-
        private function newEntitySearch() {
                $repo = \Wikibase\Repo\WikibaseRepo::getDefaultInstance();
 
                return new EntitySearchElastic(
                        $repo->getLanguageFallbackChainFactory(),
                        new BasicEntityIdParser(),
-                       $this->getMockLabelDescriptionLookup(),
                        $repo->getContentModelMappings(),
                        $this->getMockRequest(),
                        $repo->getSettings()->getSetting( 'entitySearch' )

-- 
To view, visit https://gerrit.wikimedia.org/r/336911
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I459c619daf8f7e6c47c1a486428f0ca38f7c8faf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to