Skip to content

Commit

Permalink
Merge "Search: Include display label/description language in results"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Feb 27, 2025
2 parents ffc8dbb + bc3f4f8 commit c9d74d5
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 37 deletions.
33 changes: 28 additions & 5 deletions repo/domains/search/specs/index.fragment.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,41 @@
"items": {
"type": "object",
"properties": {
"id": { "type": "string", "pattern": "^Q[1-9]\\d{0,9}$" },
"label": { "type": "string" },
"description": { "type": "string" }
"id": {
"type": "string",
"pattern": "^Q[1-9]\\d{0,9}$"
},
"label": {
"type": "object",
"properties": {
"language": { "type": "string" },
"value": { "type": "string" }
}
},
"description": {
"type": "object",
"properties": {
"language": { "type": "string" },
"value": { "type": "string" }
}
}
}
}
}
}
},
"example": {
"results": [
{ "id": "Q123", "label": "potato", "description": "staple food" },
{ "id": "Q234", "label": "potato", "description": "species of plant" }
{
"id": "Q123",
"label": { "language": "en", "value": "potato" },
"description": { "language": "en", "value": "staple food" }
},
{
"id": "Q234",
"label": { "language": "en", "value": "potato" },
"description": { "language": "en", "value": "species of plant" }
}
]
}
}
Expand Down
26 changes: 26 additions & 0 deletions repo/domains/search/src/Domain/Model/Description.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\Domains\Search\Domain\Model;

/**
* @license GPL-2.0-or-later
*/
class Description {

private string $languageCode;
private string $text;

public function __construct( string $languageCode, string $text ) {
$this->languageCode = $languageCode;
$this->text = $text;
}

public function getLanguageCode(): string {
return $this->languageCode;
}

public function getText(): string {
return $this->text;
}

}
10 changes: 5 additions & 5 deletions repo/domains/search/src/Domain/Model/ItemSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
class ItemSearchResult {

private ItemId $itemId;
private string $label;
private string $description;
private ?Label $label;
private ?Description $description;

public function __construct( ItemId $itemId, string $label, string $description ) {
public function __construct( ItemId $itemId, ?Label $label, ?Description $description ) {
$this->itemId = $itemId;
$this->label = $label;
$this->description = $description;
Expand All @@ -23,11 +23,11 @@ public function getItemId(): ItemId {
return $this->itemId;
}

public function getLabel(): string {
public function getLabel(): ?Label {
return $this->label;
}

public function getDescription(): string {
public function getDescription(): ?Description {
return $this->description;
}
}
26 changes: 26 additions & 0 deletions repo/domains/search/src/Domain/Model/Label.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\Domains\Search\Domain\Model;

/**
* @license GPL-2.0-or-later
*/
class Label {

private string $languageCode;
private string $text;

public function __construct( string $languageCode, string $text ) {
$this->languageCode = $languageCode;
$this->text = $text;
}

public function getLanguageCode(): string {
return $this->languageCode;
}

public function getText(): string {
return $this->text;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Wikibase\DataModel\Entity\Item;
use Wikibase\DataModel\Entity\ItemId;
use Wikibase\Lib\Store\EntityNamespaceLookup;
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
use Wikibase\Repo\Domains\Search\Domain\Services\ItemSearchEngine;

/**
Expand Down Expand Up @@ -71,12 +73,15 @@ private function convertSearchResults( array $results ): ItemSearchResults {
return new ItemSearchResults(
...array_map(
function ( SearchResult $result ) {
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
$labelData = $result->getLabelData();
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
$descriptionData = $result->getDescriptionData();

return new ItemSearchResult(
new ItemId( $result->getTitle()->getText() ),
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
$result->getLabelData()['value'],
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
$result->getDescriptionData()['value']
$labelData ? new Label( $labelData['language'], $labelData['value'] ) : null,
$descriptionData ? new Description( $descriptionData['language'], $descriptionData['value'] ) : null
);
},
$results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Wikibase\Lib\Store\MatchingTermsLookup;
use Wikibase\Lib\Store\TermIndexSearchCriteria;
use Wikibase\Lib\TermIndexEntry;
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
use Wikibase\Repo\Domains\Search\Domain\Services\ItemSearchEngine;

/**
Expand All @@ -31,11 +33,14 @@ public function __construct( MatchingTermsLookup $matchingTermsLookup, TermLooku

public function searchItemByLabel( string $searchTerm, string $languageCode ): ItemSearchResults {
return new ItemSearchResults( ...array_map(
fn( TermIndexEntry $entry ) => new ItemSearchResult(
new ItemId( (string)$entry->getEntityId() ),
$entry->getTerm()->getText(),
$this->termLookup->getDescription( $entry->getEntityId(), $languageCode ) ?? ''
),
function ( TermIndexEntry $entry ) use ( $languageCode ) {
$description = $this->termLookup->getDescription( $entry->getEntityId(), $languageCode );
return new ItemSearchResult(
new ItemId( (string)$entry->getEntityId() ),
new Label( $entry->getLanguage(), $entry->getText() ),
$description ? new Description( $languageCode, $description ) : null
);
},
$this->matchingTermsLookup->getMatchingTerms(
[ new TermIndexSearchCriteria( [ 'termLanguage' => $languageCode, 'termText' => $searchTerm ] ) ],
TermTypes::TYPE_LABEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ private function formatResults( ItemSearchResults $results ): array {
return array_map(
fn( ItemSearchResult $result ) => [
'id' => $result->getItemId()->getSerialization(),
'label' => $result->getLabel(),
'description' => $result->getDescription(),
'label' => $result->getLabel() ? [
'language' => $result->getLabel()->getLanguageCode(),
'value' => $result->getLabel()->getText(),
] : null,
'description' => $result->getDescription() ? [
'language' => $result->getDescription()->getLanguageCode(),
'value' => $result->getDescription()->getText(),
] : null,
],
iterator_to_array( $results )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ describe( 'Simple item search', () => {
} );

it( 'finds items matching the search term', async () => {
const response = await newSearchRequest( 'en', englishTermMatchingTwoItems )
const language = 'en';
const response = await newSearchRequest( language, englishTermMatchingTwoItems )
.assertValidRequest()
.makeRequest();

Expand All @@ -61,22 +62,32 @@ describe( 'Simple item search', () => {
assert.lengthOf( results, 2 );

const item1Result = results.find( ( { id } ) => id === item1.id );
assert.deepEqual( item1Result, { id: item1.id, label: item1Label, description: item1Description } );
assert.deepEqual( item1Result, {
id: item1.id,
label: { language, value: item1Label },
description: { language, value: item1Description }
} );

const item2Result = results.find( ( { id } ) => id === item2.id );
assert.deepEqual( item2Result, { id: item2.id, label: item2Label, description: item2Description } );
assert.deepEqual( item2Result, {
id: item2.id,
label: { language, value: item2Label },
description: { language, value: item2Description }
} );
} );

it( 'finds items matching the search term in another language', async () => {
const response = await newSearchRequest( 'de', item1GermanLabel )
const language = 'de';
const response = await newSearchRequest( language, item1GermanLabel )
.assertValidRequest()
.makeRequest();

expect( response ).to.have.status( 200 );
assert.deepEqual(
response.body.results,
[ { id: item1.id, label: item1GermanLabel, description: item1GermanDescription } ]
);
assert.deepEqual( response.body.results, [ {
id: item1.id,
label: { language, value: item1GermanLabel },
description: { language, value: item1GermanDescription }
} ] );
} );

it( 'finds nothing if no items match', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Wikibase\Lib\StaticContentLanguages;
use Wikibase\Lib\Store\EntityNamespaceLookup;
use Wikibase\Lib\TermLanguageFallbackChain;
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
use Wikibase\Repo\Domains\Search\Infrastructure\DataAccess\MediaWikiSearchEngine;
use Wikibase\Search\Elastic\EntityResult;

Expand Down Expand Up @@ -73,8 +75,16 @@ public function testSearchItemByLabel( $result ): void {

$this->assertEquals(
new ItemSearchResults(
new ItemSearchResult( new ItemId( self::RESULT1_ITEM_ID ), self::RESULT1_LABEL, self::RESULT1_DESCRIPTION ),
new ItemSearchResult( new ItemId( self::RESULT2_ITEM_ID ), self::RESULT2_LABEL, self::RESULT2_DESCRIPTION ),
new ItemSearchResult(
new ItemId( self::RESULT1_ITEM_ID ),
new Label( 'en', self::RESULT1_LABEL ),
new Description( 'en', self::RESULT1_DESCRIPTION )
),
new ItemSearchResult(
new ItemId( self::RESULT2_ITEM_ID ),
new Label( 'en', self::RESULT2_LABEL ),
new Description( 'en', self::RESULT2_DESCRIPTION )
),
),
$this->newEngine()->searchItemByLabel( $searchTerm, $languageCode )
);
Expand Down
40 changes: 34 additions & 6 deletions repo/rest-api/src/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -33563,10 +33563,26 @@
"pattern": "^Q[1-9]\\d{0,9}$"
},
"label": {
"type": "string"
"type": "object",
"properties": {
"language": {
"type": "string"
},
"value": {
"type": "string"
}
}
},
"description": {
"type": "string"
"type": "object",
"properties": {
"language": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
Expand All @@ -33577,13 +33593,25 @@
"results": [
{
"id": "Q123",
"label": "potato",
"description": "staple food"
"label": {
"language": "en",
"value": "potato"
},
"description": {
"language": "en",
"value": "staple food"
}
},
{
"id": "Q234",
"label": "potato",
"description": "species of plant"
"label": {
"language": "en",
"value": "potato"
},
"description": {
"language": "en",
"value": "species of plant"
}
}
]
}
Expand Down

0 comments on commit c9d74d5

Please sign in to comment.