Skip to content

Commit c9d74d5

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "Search: Include display label/description language in results"
2 parents ffc8dbb + bc3f4f8 commit c9d74d5

File tree

10 files changed

+177
-37
lines changed

10 files changed

+177
-37
lines changed

repo/domains/search/specs/index.fragment.json

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,41 @@
4646
"items": {
4747
"type": "object",
4848
"properties": {
49-
"id": { "type": "string", "pattern": "^Q[1-9]\\d{0,9}$" },
50-
"label": { "type": "string" },
51-
"description": { "type": "string" }
49+
"id": {
50+
"type": "string",
51+
"pattern": "^Q[1-9]\\d{0,9}$"
52+
},
53+
"label": {
54+
"type": "object",
55+
"properties": {
56+
"language": { "type": "string" },
57+
"value": { "type": "string" }
58+
}
59+
},
60+
"description": {
61+
"type": "object",
62+
"properties": {
63+
"language": { "type": "string" },
64+
"value": { "type": "string" }
65+
}
66+
}
5267
}
5368
}
5469
}
5570
}
5671
},
5772
"example": {
5873
"results": [
59-
{ "id": "Q123", "label": "potato", "description": "staple food" },
60-
{ "id": "Q234", "label": "potato", "description": "species of plant" }
74+
{
75+
"id": "Q123",
76+
"label": { "language": "en", "value": "potato" },
77+
"description": { "language": "en", "value": "staple food" }
78+
},
79+
{
80+
"id": "Q234",
81+
"label": { "language": "en", "value": "potato" },
82+
"description": { "language": "en", "value": "species of plant" }
83+
}
6184
]
6285
}
6386
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace Wikibase\Repo\Domains\Search\Domain\Model;
4+
5+
/**
6+
* @license GPL-2.0-or-later
7+
*/
8+
class Description {
9+
10+
private string $languageCode;
11+
private string $text;
12+
13+
public function __construct( string $languageCode, string $text ) {
14+
$this->languageCode = $languageCode;
15+
$this->text = $text;
16+
}
17+
18+
public function getLanguageCode(): string {
19+
return $this->languageCode;
20+
}
21+
22+
public function getText(): string {
23+
return $this->text;
24+
}
25+
26+
}

repo/domains/search/src/Domain/Model/ItemSearchResult.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
class ItemSearchResult {
1111

1212
private ItemId $itemId;
13-
private string $label;
14-
private string $description;
13+
private ?Label $label;
14+
private ?Description $description;
1515

16-
public function __construct( ItemId $itemId, string $label, string $description ) {
16+
public function __construct( ItemId $itemId, ?Label $label, ?Description $description ) {
1717
$this->itemId = $itemId;
1818
$this->label = $label;
1919
$this->description = $description;
@@ -23,11 +23,11 @@ public function getItemId(): ItemId {
2323
return $this->itemId;
2424
}
2525

26-
public function getLabel(): string {
26+
public function getLabel(): ?Label {
2727
return $this->label;
2828
}
2929

30-
public function getDescription(): string {
30+
public function getDescription(): ?Description {
3131
return $this->description;
3232
}
3333
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace Wikibase\Repo\Domains\Search\Domain\Model;
4+
5+
/**
6+
* @license GPL-2.0-or-later
7+
*/
8+
class Label {
9+
10+
private string $languageCode;
11+
private string $text;
12+
13+
public function __construct( string $languageCode, string $text ) {
14+
$this->languageCode = $languageCode;
15+
$this->text = $text;
16+
}
17+
18+
public function getLanguageCode(): string {
19+
return $this->languageCode;
20+
}
21+
22+
public function getText(): string {
23+
return $this->text;
24+
}
25+
26+
}

repo/domains/search/src/Infrastructure/DataAccess/MediaWikiSearchEngine.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Wikibase\DataModel\Entity\Item;
1111
use Wikibase\DataModel\Entity\ItemId;
1212
use Wikibase\Lib\Store\EntityNamespaceLookup;
13+
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
1314
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
1415
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
16+
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
1517
use Wikibase\Repo\Domains\Search\Domain\Services\ItemSearchEngine;
1618

1719
/**
@@ -71,12 +73,15 @@ private function convertSearchResults( array $results ): ItemSearchResults {
7173
return new ItemSearchResults(
7274
...array_map(
7375
function ( SearchResult $result ) {
76+
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
77+
$labelData = $result->getLabelData();
78+
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
79+
$descriptionData = $result->getDescriptionData();
80+
7481
return new ItemSearchResult(
7582
new ItemId( $result->getTitle()->getText() ),
76-
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
77-
$result->getLabelData()['value'],
78-
// @phan-suppress-next-line PhanUndeclaredMethod - phan does not know about WikibaseCirrusSearch
79-
$result->getDescriptionData()['value']
83+
$labelData ? new Label( $labelData['language'], $labelData['value'] ) : null,
84+
$descriptionData ? new Description( $descriptionData['language'], $descriptionData['value'] ) : null
8085
);
8186
},
8287
$results

repo/domains/search/src/Infrastructure/DataAccess/SqlTermStoreSearchEngine.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use Wikibase\Lib\Store\MatchingTermsLookup;
1010
use Wikibase\Lib\Store\TermIndexSearchCriteria;
1111
use Wikibase\Lib\TermIndexEntry;
12+
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
1213
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
1314
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
15+
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
1416
use Wikibase\Repo\Domains\Search\Domain\Services\ItemSearchEngine;
1517

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

3234
public function searchItemByLabel( string $searchTerm, string $languageCode ): ItemSearchResults {
3335
return new ItemSearchResults( ...array_map(
34-
fn( TermIndexEntry $entry ) => new ItemSearchResult(
35-
new ItemId( (string)$entry->getEntityId() ),
36-
$entry->getTerm()->getText(),
37-
$this->termLookup->getDescription( $entry->getEntityId(), $languageCode ) ?? ''
38-
),
36+
function ( TermIndexEntry $entry ) use ( $languageCode ) {
37+
$description = $this->termLookup->getDescription( $entry->getEntityId(), $languageCode );
38+
return new ItemSearchResult(
39+
new ItemId( (string)$entry->getEntityId() ),
40+
new Label( $entry->getLanguage(), $entry->getText() ),
41+
$description ? new Description( $languageCode, $description ) : null
42+
);
43+
},
3944
$this->matchingTermsLookup->getMatchingTerms(
4045
[ new TermIndexSearchCriteria( [ 'termLanguage' => $languageCode, 'termText' => $searchTerm ] ) ],
4146
TermTypes::TYPE_LABEL,

repo/domains/search/src/RouteHandlers/SimpleItemSearchRouteHandler.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,14 @@ private function formatResults( ItemSearchResults $results ): array {
7676
return array_map(
7777
fn( ItemSearchResult $result ) => [
7878
'id' => $result->getItemId()->getSerialization(),
79-
'label' => $result->getLabel(),
80-
'description' => $result->getDescription(),
79+
'label' => $result->getLabel() ? [
80+
'language' => $result->getLabel()->getLanguageCode(),
81+
'value' => $result->getLabel()->getText(),
82+
] : null,
83+
'description' => $result->getDescription() ? [
84+
'language' => $result->getDescription()->getLanguageCode(),
85+
'value' => $result->getDescription()->getText(),
86+
] : null,
8187
],
8288
iterator_to_array( $results )
8389
);

repo/domains/search/tests/mocha/api-testing/SimpleItemSearchTest.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ describe( 'Simple item search', () => {
5151
} );
5252

5353
it( 'finds items matching the search term', async () => {
54-
const response = await newSearchRequest( 'en', englishTermMatchingTwoItems )
54+
const language = 'en';
55+
const response = await newSearchRequest( language, englishTermMatchingTwoItems )
5556
.assertValidRequest()
5657
.makeRequest();
5758

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

6364
const item1Result = results.find( ( { id } ) => id === item1.id );
64-
assert.deepEqual( item1Result, { id: item1.id, label: item1Label, description: item1Description } );
65+
assert.deepEqual( item1Result, {
66+
id: item1.id,
67+
label: { language, value: item1Label },
68+
description: { language, value: item1Description }
69+
} );
6570

6671
const item2Result = results.find( ( { id } ) => id === item2.id );
67-
assert.deepEqual( item2Result, { id: item2.id, label: item2Label, description: item2Description } );
72+
assert.deepEqual( item2Result, {
73+
id: item2.id,
74+
label: { language, value: item2Label },
75+
description: { language, value: item2Description }
76+
} );
6877
} );
6978

7079
it( 'finds items matching the search term in another language', async () => {
71-
const response = await newSearchRequest( 'de', item1GermanLabel )
80+
const language = 'de';
81+
const response = await newSearchRequest( language, item1GermanLabel )
7282
.assertValidRequest()
7383
.makeRequest();
7484

7585
expect( response ).to.have.status( 200 );
76-
assert.deepEqual(
77-
response.body.results,
78-
[ { id: item1.id, label: item1GermanLabel, description: item1GermanDescription } ]
79-
);
86+
assert.deepEqual( response.body.results, [ {
87+
id: item1.id,
88+
label: { language, value: item1GermanLabel },
89+
description: { language, value: item1GermanDescription }
90+
} ] );
8091
} );
8192

8293
it( 'finds nothing if no items match', async () => {

repo/domains/search/tests/phpunit/Infrastructure/DataAccess/MediaWikiSearchEngineTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Wikibase\Lib\StaticContentLanguages;
1717
use Wikibase\Lib\Store\EntityNamespaceLookup;
1818
use Wikibase\Lib\TermLanguageFallbackChain;
19+
use Wikibase\Repo\Domains\Search\Domain\Model\Description;
1920
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResult;
2021
use Wikibase\Repo\Domains\Search\Domain\Model\ItemSearchResults;
22+
use Wikibase\Repo\Domains\Search\Domain\Model\Label;
2123
use Wikibase\Repo\Domains\Search\Infrastructure\DataAccess\MediaWikiSearchEngine;
2224
use Wikibase\Search\Elastic\EntityResult;
2325

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

7476
$this->assertEquals(
7577
new ItemSearchResults(
76-
new ItemSearchResult( new ItemId( self::RESULT1_ITEM_ID ), self::RESULT1_LABEL, self::RESULT1_DESCRIPTION ),
77-
new ItemSearchResult( new ItemId( self::RESULT2_ITEM_ID ), self::RESULT2_LABEL, self::RESULT2_DESCRIPTION ),
78+
new ItemSearchResult(
79+
new ItemId( self::RESULT1_ITEM_ID ),
80+
new Label( 'en', self::RESULT1_LABEL ),
81+
new Description( 'en', self::RESULT1_DESCRIPTION )
82+
),
83+
new ItemSearchResult(
84+
new ItemId( self::RESULT2_ITEM_ID ),
85+
new Label( 'en', self::RESULT2_LABEL ),
86+
new Description( 'en', self::RESULT2_DESCRIPTION )
87+
),
7888
),
7989
$this->newEngine()->searchItemByLabel( $searchTerm, $languageCode )
8090
);

repo/rest-api/src/openapi.json

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33563,10 +33563,26 @@
3356333563
"pattern": "^Q[1-9]\\d{0,9}$"
3356433564
},
3356533565
"label": {
33566-
"type": "string"
33566+
"type": "object",
33567+
"properties": {
33568+
"language": {
33569+
"type": "string"
33570+
},
33571+
"value": {
33572+
"type": "string"
33573+
}
33574+
}
3356733575
},
3356833576
"description": {
33569-
"type": "string"
33577+
"type": "object",
33578+
"properties": {
33579+
"language": {
33580+
"type": "string"
33581+
},
33582+
"value": {
33583+
"type": "string"
33584+
}
33585+
}
3357033586
}
3357133587
}
3357233588
}
@@ -33577,13 +33593,25 @@
3357733593
"results": [
3357833594
{
3357933595
"id": "Q123",
33580-
"label": "potato",
33581-
"description": "staple food"
33596+
"label": {
33597+
"language": "en",
33598+
"value": "potato"
33599+
},
33600+
"description": {
33601+
"language": "en",
33602+
"value": "staple food"
33603+
}
3358233604
},
3358333605
{
3358433606
"id": "Q234",
33585-
"label": "potato",
33586-
"description": "species of plant"
33607+
"label": {
33608+
"language": "en",
33609+
"value": "potato"
33610+
},
33611+
"description": {
33612+
"language": "en",
33613+
"value": "species of plant"
33614+
}
3358733615
}
3358833616
]
3358933617
}

0 commit comments

Comments
 (0)