Skip to content

Commit 474fb2c

Browse files
authored
Merge pull request #29 from aubriehill/add-relevance-score
Issue #28 Added relevance score field
2 parents 52c1cb9 + 2e01d47 commit 474fb2c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Drupal\graphql_search_api\Plugin\GraphQL\Fields;
4+
5+
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7+
use GraphQL\Type\Definition\ResolveInfo;
8+
9+
/**
10+
* A Solr relevance score.
11+
*
12+
* @GraphQLField(
13+
* secure = true,
14+
* parents = {"SearchAPIDocument"},
15+
* id = "search_api_relevance",
16+
* name = "relevance",
17+
* type = "Float",
18+
* )
19+
*/
20+
class SearchAPIRelevance extends FieldPluginBase {
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
26+
yield $value['relevance'];
27+
}
28+
29+
}

src/Plugin/GraphQL/Fields/SearchAPISearch.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,12 @@ private function getSearchResponse($results) {
342342
foreach ($result_items as $id => &$item) {
343343
// Load the response document into the search response array.
344344
$document['item'] = $item->getFields();
345-
$document['index_id'] = $this->index->id();;
345+
$document['index_id'] = $this->index->id();
346346
$document['type'] = str_replace("_", "", ucwords($this->index->id() . "Doc", '_'));
347347

348+
// Set the relevance score.
349+
$document['relevance'] = $item->getScore();
350+
348351
$search_response['SearchAPIDocument'][] = $document;
349352
}
350353

0 commit comments

Comments
 (0)