-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHighlightingSolrConfigEventSubscriber.php
189 lines (163 loc) · 5.6 KB
/
HighlightingSolrConfigEventSubscriber.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Drupal\islandora_hocr\EventSubscriber;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Utility\FieldsHelperInterface;
use Drupal\search_api_solr\Event\PostConfigFilesGenerationEvent;
use Drupal\search_api_solr\Event\PostExtractResultsEvent;
use Drupal\search_api_solr\Event\PreQueryEvent;
use Drupal\search_api_solr\Event\SearchApiSolrEvents;
use Drupal\search_api_solr\SolrBackendInterface;
use Drupal\search_api_solr\Utility\Utility;
use Solarium\QueryType\Select\Query\Query;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Highlighting library event subscriber.
*/
class HighlightingSolrConfigEventSubscriber implements EventSubscriberInterface {
/**
* Path to the library to be added to the Solr config.
*
* @var string
*/
protected string $libraryPath;
/**
* The fields helper service.
*
* @var \Drupal\search_api\Utility\FieldsHelperInterface
*/
protected FieldsHelperInterface $fieldsHelper;
/**
* Constructor.
*/
public function __construct(
string $library_path,
FieldsHelperInterface $fields_helper
) {
$this->libraryPath = $library_path;
$this->fieldsHelper = $fields_helper;
}
/**
* Static factory.
*
* @return self
* An instance of this class.
*/
public static function create() : self {
return new static(
getenv('SOLR_HOCR_PLUGIN_PATH'),
\Drupal::service('search_api.fields_helper')
);
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() {
return [
SearchApiSolrEvents::POST_CONFIG_FILES_GENERATION => 'addLibraryInfo',
SearchApiSolrEvents::PRE_QUERY => 'preQuery',
SearchApiSolrEvents::POST_EXTRACT_RESULTS => 'postExtractResults',
];
}
/**
* Event responder; add library path to the Solr config.
*
* @param \Drupal\search_api_solr\Event\PostConfigFilesGenerationEvent $event
* The event to which we are responding.
*/
public function addLibraryInfo(PostConfigFilesGenerationEvent $event) : void {
if (!isset($this->libraryPath)) {
return;
}
$files = $event->getConfigFiles();
if (!isset($files['solrconfig_extra.xml'])) {
throw new \LogicException('Missing "solrconfig_extra.xml".');
}
$files['solrconfig_extra.xml'] .= <<<EOXML
<lib dir="{$this->libraryPath}" regex=".*\\.jar" />
EOXML;
$event->setConfigFiles($files);
}
/**
* Pre-query event handler; add in OCR highlighting info, if requested.
*
* If the `islandora_hocr_properties` option is populated with an associative
* array of properties mapping to an array (presently, initially expected to
* be empty), we will identify the related fields and attempt to highlight
* them.
*
* @param \Drupal\search_api_solr\Event\PreQueryEvent $event
* The event object.
*/
public function preQuery(PreQueryEvent $event) : void {
$sapi_query = $event->getSearchApiQuery();
$s_query = $event->getSolariumQuery();
if ($sapi_query->getProcessingLevel() < QueryInterface::PROCESSING_FULL) {
return;
}
if (!($s_query instanceof Query)) {
return;
}
$highlight_props = $sapi_query->getOption('islandora_hocr_properties', []);
$highlight_fields = [];
if (!$highlight_props) {
return;
}
$index = $sapi_query->getIndex();
$backend = $index->getServerInstance()->getBackend();
if (!($backend instanceof SolrBackendInterface)) {
return;
}
$language_fields = $backend->getSolrFieldNamesKeyedByLanguage(
$sapi_query->getLanguages(),
$index
);
foreach ($highlight_props as $prop => &$info) {
$info['language_fields'] = $language_fields[$prop];
foreach ($info['language_fields'] as $field) {
$highlight_fields[$field] = TRUE;
}
}
unset($info);
$sapi_query->setOption('islandora_hocr_properties', $highlight_props);
$sapi_query->setOption('islandora_hocr_fields', $highlight_fields);
$s_query->setHandler('select_ocr')
->addParam('hl', 'true')
->addParam('hl.ocr.fl', implode(',', array_keys($highlight_fields)))
// Deal with absolute image coordinates.
->addParam('hl.ocr.absoluteHighlights', 'on')
// We expect OCR per page.
->addParam('hl.ocr.trackPages', 'off');
}
/**
* Post-result extraction event handler; add highlighting info where relevant.
*
* @param \Drupal\search_api_solr\Event\PostExtractResultsEvent $event
* The event object.
*/
public function postExtractResults(PostExtractResultsEvent $event) : void {
$sapi_query = $event->getSearchApiQuery();
$index = $sapi_query->getIndex();
$backend = $index->getServerInstance()->getBackend();
if (!($backend instanceof SolrBackendInterface)) {
return;
}
$result_set = $event->getSearchApiResultSet();
$response = $result_set->getExtraData('search_api_solr_response');
$ocr_hl = $response['ocrHighlighting'] ?? FALSE;
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($result_set as $item) {
// Adapted from https://git.drupalcode.org/project/search_api_solr/-/blob/a5d0557c58f7a13c7cc63d1709fbc3fa5dca39ae/src/Plugin/search_api/backend/SearchApiSolrBackend.php?page=4#L3105-3107
$solr_id = Utility::hasIndexJustSolrDatasources($index) ?
str_replace('solr_document/', '', $item->getId()) :
implode('-', [
$backend->getTargetedSiteHash($index),
$backend->getTargetedIndexId($index),
$item->getId(),
]);
if (empty($ocr_hl[$solr_id])) {
continue;
}
$item->setExtraData('islandora_hocr_highlights', $ocr_hl[$solr_id]);
}
}
}