Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jena-text searches and alphabetical index restrictions by language #958

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions model/sparql/JenaTextSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class JenaTextSparql extends GenericSparql
*
* @param string $term search term
* @param string $property property to search (e.g. 'skos:prefLabel'), or '' for default
* @param string @$searchLang for language code
* @param string $langClause jena-text clause to limit search by language code
* @return string SPARQL text search clause
*/

private function createTextQueryCondition($term, $property = '', $searchLang = '')
private function createTextQueryCondition($term, $property = '', $langClause = '')
{
// construct the lucene search term for jena-text

Expand All @@ -51,23 +51,19 @@ private function createTextQueryCondition($term, $property = '', $searchLang = '

$maxResults = self::MAX_N;

$langClause = '';
if ($searchLang) {
$langClause = "?langparam";
}

return "(?s ?score ?match) text:query ($property '$term' $maxResults $langClause) .";
}

/**
* Generate jena-text search condition for matching labels in SPARQL
* @param string $term search term
* @param string $langClause language clause used for matching labels (null means any language)
* @param string $searchLang language code used for matching labels (null means any language)
* @return string sparql query snippet
*/
protected function generateConceptSearchQueryCondition($term, $langClause)
protected function generateConceptSearchQueryCondition($term, $searchLang)
{
# make text query clauses
$langClause = $searchLang ? '?langParam' : '';
$textcond = $this->createTextQueryCondition($term, '?prop', $langClause);

if ($this->isDefaultEndpoint()) {
Expand Down Expand Up @@ -129,8 +125,9 @@ public function generateAlphabeticalListQuery($letter, $lang, $limit = null, $of

# make text query clause
$lcletter = mb_strtolower($letter, 'UTF-8'); // convert to lower case, UTF-8 safe
$textcondPref = $this->createTextQueryCondition($letter . '*', 'skos:prefLabel', $lang);
$textcondAlt = $this->createTextQueryCondition($letter . '*', 'skos:altLabel', $lang);
$langClause = $this->generateLangClause($lang);
$textcondPref = $this->createTextQueryCondition($letter . '*', 'skos:prefLabel', $langClause);
$textcondAlt = $this->createTextQueryCondition($letter . '*', 'skos:altLabel', $langClause);
$orderbyclause = $this->formatOrderBy("LCASE(?match)", $lang) . " STR(?s) LCASE(STR(?qualifier))";

$qualifierClause = $qualifier ? "OPTIONAL { ?s <" . $qualifier->getURI() . "> ?qualifier }" : "";
Expand Down
35 changes: 35 additions & 0 deletions tests/GenericSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ public function testQueryConceptsAlphabeticalNoResults() {
$this->assertEquals($expected, $actual);
}

/**
* @covers GenericSparql::queryConceptsAlphabetical
* @covers GenericSparql::generateAlphabeticalListQuery
* @covers GenericSparql::formatFilterConditions
* @covers GenericSparql::transformAlphabeticalListResults
*/
public function testQueryConceptsAlphabeticalMatchLanguage() {
$actual = $this->sparql->queryConceptsAlphabetical('e', 'en');
// there are two prefLabels starting with E, "Eel"@en and "Europa"@nb
// checking that only the first one is returned
$this->assertEquals(1, sizeof($actual));
$this->assertEquals('Eel', $actual[0]['prefLabel']);
}

/**
* @covers GenericSparql::queryConceptsAlphabetical
* @covers GenericSparql::generateAlphabeticalListQuery
Expand Down Expand Up @@ -589,6 +603,27 @@ public function testQueryConcepts()
$this->assertEquals('Bass', $actual[0]['prefLabel']);
}

/**
* @covers GenericSparql::queryConcepts
* @covers GenericSparql::generateConceptSearchQueryCondition
* @covers GenericSparql::generateConceptSearchQueryInner
* @covers GenericSparql::generateConceptSearchQuery
* @covers GenericSparql::formatFilterGraph
* @covers GenericSparql::transformConceptSearchResults
* @covers GenericSparql::transformConceptSearchResult
* @covers GenericSparql::shortenUri
*/
public function testQueryConceptsMatchLanguage()
{
$voc = $this->model->getVocabulary('test');
$this->params->method('getSearchTerm')->will($this->returnValue('e*'));
$this->params->method('getVocabIds')->will($this->returnValue(array('test')));
$this->params->method('getSearchLang')->will($this->returnValue('en'));
$actual = $this->sparql->queryConcepts(array($voc), null, null, $this->params);
$this->assertEquals(1, sizeof($actual));
$this->assertEquals('Eel', $actual[0]['prefLabel']);
}

/**
* @covers GenericSparql::queryConcepts
* @covers GenericSparql::generateConceptSearchQueryCondition
Expand Down
27 changes: 27 additions & 0 deletions tests/JenaTextSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ public function testQueryConceptsAlphabeticalNoResults() {
$this->assertEquals($expected, $actual);
}

/**
* @covers JenaTextSparql::generateAlphabeticalListQuery
*/
public function testQueryConceptsAlphabeticalMatchLanguage() {
$actual = $this->sparql->queryConceptsAlphabetical('e', 'en');
// there are two prefLabels starting with E, "Eel"@en and "Europa"@nb
// checking that only the first one is returned
$this->assertEquals(1, sizeof($actual));
$this->assertEquals('Eel', $actual[0]['prefLabel']);
}

/**
* @covers JenaTextSparql::generateAlphabeticalListQuery
*/
Expand Down Expand Up @@ -275,6 +286,22 @@ public function testQueryConcepts()
$this->assertEquals('Bass', $actual[0]['prefLabel']);
}

/**
* @covers JenaTextSparql::createTextQueryCondition
* @covers JenaTextSparql::generateConceptSearchQueryCondition
* @covers JenaTextSparql::generateConceptSearchQueryInner
*/
public function testQueryConceptsMatchLanguage()
{
$voc = $this->model->getVocabulary('test');
$this->params->method('getSearchTerm')->will($this->returnValue('e*'));
$this->params->method('getVocabIds')->will($this->returnValue(array('test')));
$this->params->method('getSearchLang')->will($this->returnValue('en'));
$actual = $this->sparql->queryConcepts(array($voc), null, null, $this->params);
$this->assertEquals(1, sizeof($actual));
$this->assertEquals('Eel', $actual[0]['prefLabel']);
}

/**
* @covers JenaTextSparql::createTextQueryCondition
* @covers JenaTextSparql::generateConceptSearchQueryCondition
Expand Down