Skip to content

Commit

Permalink
[BUGFIX] Explain is not rendred with Apache Solr 8.2
Browse files Browse the repository at this point in the history
Add's a testcase and the recursive lookup in the rootline for a node with a given fieldname.

Fixes: #7
  • Loading branch information
timohund committed Oct 22, 2019
1 parent d3880e0 commit ce4a1c2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
4 changes: 1 addition & 3 deletions Classes/Domain/Result/Explanation/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected function parseChildNodes($contextContent, \ArrayObject $collection, $p
preg_match_all('~((?<=^)|(?<=\n))(?<token>[0-9].*?\n)((?=[0-9])|(?=$))~s',$contextContent,$matches);

if( array_key_exists('token',$matches)) {

foreach($matches['token'] as $tokenKey => $tokenContent) {
$nodeParts = explode(PHP_EOL,$tokenContent);
$nodeContent = trim(array_shift($nodeParts));
Expand All @@ -107,8 +108,6 @@ protected function parseChildNodes($contextContent, \ArrayObject $collection, $p
}
}
}

$collection;
}

/**
Expand Down Expand Up @@ -175,7 +174,6 @@ protected function getQueryAttribute($content) {
protected function getFieldNameFromNodeName($nodeName) {
$result = '';
$matches = array();

if (mb_strpos($nodeName,'weight(Synonym(') !== false ) {
preg_match('~weight\(Synonym\((?<fieldname>[^\):]*)~', $nodeName, $matches);
} elseif(mb_strpos($nodeName,'weight(') !== false ){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class SummarizeFieldImpacts implements ExplainNodeVisitorInterface {

/**
* @var float
* @var array
*/
protected $sums = array();

Expand All @@ -22,8 +22,7 @@ class SummarizeFieldImpacts implements ExplainNodeVisitorInterface {
public function visit(Explain $node) {
if($node->getNodeType() == $node::NODE_TYPE_LEAF) {
if($node->getParent() != null) {
$fieldName = $node->getParent()->getFieldName();

$fieldName = $this->getClosestFieldName($node);
if(trim($fieldName) === '') {
return;
}
Expand All @@ -38,6 +37,25 @@ public function visit(Explain $node) {
}
}

/**
* Returns the closest fieldname in the parent root line and and empty string when none is present.
*
* @param $node
* @return string
*/
protected function getClosestFieldName($node) {
while(!is_null($node->getParent())) {
$parent = $node->getParent();
if ($parent->getFieldName() !== '') {
return $parent->getFieldName();
}

$node = $parent;
}

return '';
}

/**
* Returns the fieldnames that have been relevant during the explain.
*
Expand Down
20 changes: 20 additions & 0 deletions Tests/Domain/Result/Explanation/ExplainServiceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,24 @@ public function testCanParseSynonymeNodes() {

$this->assertEquals($expectedResult, $result);
}

/**
* @test
*/
public function testCanParse82Response() {
$content = $this->getFixtureContent('8.2.001.txt');
$result = ExplainService::getFieldImpactsFromRawContent(
$content,
'foo',
'bar'
);

$expectedResult = [
'content' => 85.44380986095436,
'tagsH2H3' => 4.056216176545581,
'title' => 10.499972051284612
];

$this->assertEquals($expectedResult, $result);
}
}
38 changes: 38 additions & 0 deletions Tests/Domain/Result/Explanation/Fixtures/8.2.001.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
44.474316 = sum of:
44.474316 = max plus 0.5 times others of:
3.6079488 = weight(tagsH2H3:page in 45) [SchemaSimilarity], result of:
3.6079488 = score(freq=1.0), product of:
3.0 = boost
1.6405284 = idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:
9 = n, number of documents containing term
48 = N, total number of documents with field
0.73308676 = tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:
1.0 = freq, occurrences of term within document
1.2 = k1, term saturation parameter
0.75 = b, length normalization parameter
1.0 = dl, length of field
14.041667 = avgdl, average length of field
9.3395815 = weight(title:page in 45) [SchemaSimilarity], result of:
9.3395815 = score(freq=1.0), product of:
5.0 = boost
3.2884018 = idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:
2 = n, number of documents containing term
66 = N, total number of documents with field
0.56803167 = tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:
1.0 = freq, occurrences of term within document
1.2 = k1, term saturation parameter
0.75 = b, length normalization parameter
1.0 = dl, length of field
1.9545455 = avgdl, average length of field
38.00055 = weight(content:page in 45) [SchemaSimilarity], result of:
38.00055 = score(freq=4.0), product of:
40.0 = boost
1.0761395 = idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:
22 = n, number of documents containing term
65 = N, total number of documents with field
0.882798 = tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:
4.0 = freq, occurrences of term within document
1.2 = k1, term saturation parameter
0.75 = b, length normalization parameter
72.0 = dl, length of field (approximate)
280.46155 = avgdl, average length of field

0 comments on commit ce4a1c2

Please sign in to comment.