protected function AbstractSearchApiSolrMultilingualBackend::preQuery in Search API Multilingual Solr Search 8
Modify the query before it is sent to solr.
Replaces all language unspecific fulltext query fields by language specific ones.
Parameters
\Solarium\Core\Query\QueryInterface $solarium_query: The Solarium select query object.
\Drupal\search_api\Query\QueryInterface $query: The \Drupal\search_api\Query\Query object representing the executed search query.
File
- src/
Plugin/ search_api/ backend/ AbstractSearchApiSolrMultilingualBackend.php, line 183
Class
- AbstractSearchApiSolrMultilingualBackend
- A abstract base class for all multilingual Solr Search API backends.
Namespace
Drupal\search_api_solr_multilingual\Plugin\search_api\backendCode
protected function preQuery(SolariumQueryInterface $solarium_query, QueryInterface $query) {
// Do not modify 'Server index status' queries.
// @see https://www.drupal.org/node/2668852
if ($query
->hasTag('server_index_status')) {
return;
}
parent::preQuery($solarium_query, $query);
$language_ids = $query
->getLanguages();
if (!empty($language_ids)) {
$mlt = $query
->hasTag('mlt');
$edismax = NULL;
$hl = NULL;
$solr_fields = NULL;
$highlighted_fields = [];
if ($mlt) {
/** @var \Solarium\QueryType\MoreLikeThis\Query $solarium_query */
$solr_fields = implode(' ', $solarium_query
->getMltFields());
}
else {
/** @var \Solarium\QueryType\Select\Query\Query $solarium_query */
$edismax = $solarium_query
->getEDisMax();
$solr_fields = $edismax
->getQueryFields();
$hl = $solarium_query
->getHighlighting();
$highlighted_fields = $hl
->getFields();
}
$index = $query
->getIndex();
$fulltext_fields = $this
->getQueryFulltextFields($query);
$field_names = $this
->getSolrFieldNames($index);
foreach ($fulltext_fields as $fulltext_field) {
$field_name = $field_names[$fulltext_field];
$boost = '';
if (preg_match('@' . $field_name . '(\\^[\\d.]+)@', $solr_fields, $matches)) {
$boost = $matches[1];
}
$language_specific_fields = [];
$language_specific_fields_boosted = [];
foreach ($language_ids as $language_id) {
$language_specific_field = SearchApiSolrUtility::encodeSolrName(Utility::getLanguageSpecificSolrDynamicFieldNameForSolrDynamicFieldName($field_name, $language_id));
$language_specific_fields[] = $language_specific_field;
$language_specific_fields_boosted[] = $language_specific_field . $boost;
}
$solr_fields = str_replace($field_name . $boost, implode(' ', array_unique($language_specific_fields_boosted)), $solr_fields);
if (isset($highlighted_fields[$field_name])) {
foreach ($language_specific_fields as $language_specific_field) {
// Copy the already set highlighting options over to the langauge
// specific fields.
$highlighted_field = $hl
->getField($language_specific_field);
$highlighted_field
->setOptions($highlighted_fields[$field_name]
->getOptions());
$highlighted_field
->setName($language_specific_field);
}
$hl
->removeField($field_name);
}
}
if ($mlt) {
$solarium_query
->setMltFields(explode(' ', $solr_fields));
}
else {
$edismax
->setQueryFields($solr_fields);
}
if (empty($this->configuration['retrieve_data'])) {
// We need the language to be part of the result to modify the result
// accordingly in extractResults().
$solarium_query
->addField($field_names[SEARCH_API_LANGUAGE_FIELD_NAME]);
}
}
}