Skip to content
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
6 changes: 6 additions & 0 deletions config/schema/advanced_search.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ advanced_search.settings:
facet_truncate:
type: string
label: Truncate Facet
query_fields:
type: sequence
label: Query Fields
sequence:
type: string
label: Field identifier

block.settings.search_block:
type: block_settings
Expand Down
37 changes: 27 additions & 10 deletions src/AdvancedSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,19 @@ public function alterQuery(Request $request, SolariumQueryInterface &$solarium_q
$query_fields = [];

if ($isSearchAllFields) {
foreach ($field_mapping as $key => $field) {
foreach ($field as $f => $item) {
// Get configured query fields from settings.
$configured_query_fields = $config->get(SettingsForm::QUERY_FIELDS) ?: [];

// field_mapping structure: [field_id => [language => solr_field_name]]
foreach ($field_mapping as $field_id => $languages) {
foreach ($languages as $lang => $solr_field_name) {
// bs_ are boolean fields, do not work well with text search.
if (substr($item, 0, 3) !== "bs_") {
array_push($query_fields, $item);
if (substr($solr_field_name, 0, 3) !== "bs_") {

// If query_fields is configured, only include those fields.
if (empty($configured_query_fields) || in_array($field_id, $configured_query_fields)) {
array_push($query_fields, $solr_field_name);
}
}
}
}
Expand All @@ -222,18 +230,27 @@ public function alterQuery(Request $request, SolariumQueryInterface &$solarium_q
$dismax->setQueryFields($query_fields);
}

// if all fields are searched, use the query_fields for highlighting.
if ($isSearchAllFields && isset($query_fields)) {
// Convert back to an array.
$highlight_source_fields = explode(" ", $query_fields);
}
else {
$highlight_source_fields = $fields_list;
}

if ($backend->getConfiguration()['highlight_data']) {
// Just highlight string and text fields to avoid Solr exceptions.
$highlighted_fields = array_filter(array_unique($fields_list), function ($v) {
return preg_match('/^t.*?[sm]_/', $v) || preg_match('/^s[sm]_/', $v);
// Exclude tm_X3b_*_fulltext_title
$highlighted_fields = array_filter(array_unique($highlight_source_fields), function ($v) {
return !empty($v) && (preg_match('/^t.*?[sm]_/', $v) || preg_match('/^s[sm]_/', $v)) && !preg_match('/^tm_X3b_.*_fulltext_title$/', $v);
});

if (empty($highlighted_fields)) {
$highlighted_fields = ['*'];
// Check if there are any valid fields for highlighting.
if (!empty($highlighted_fields)) {
$this->setHighlighting($solarium_query, $search_api_query, $highlighted_fields);
}

$this->setHighlighting($solarium_query, $search_api_query, $highlighted_fields);

// The Search API Highlight processor checks if the 'keys' field of
// the Search API Query is non-empty before creating an excerpt.
// Since we are getting the highlighting result from Solr instead
Expand Down
88 changes: 78 additions & 10 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
/**
* Config form for Islandora Advanced Search settings.
*/
class SettingsForm extends ConfigFormBase {
class SettingsForm extends ConfigFormBase
{

use GetConfigTrait;

Expand All @@ -28,51 +29,81 @@ class SettingsForm extends ConfigFormBase {
const DISPLAY_LIST_FLAG = 'list_on_off';
const DISPLAY_GRID_FLAG = 'grid_on_off';
const DISPLAY_DEFAULT = 'default-display-mode';
const QUERY_FIELDS = 'query_fields';

/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
*/
final public function __construct(ConfigFactoryInterface $config_factory) {
final public function __construct(ConfigFactoryInterface $config_factory)
{
$this->setConfigFactory($config_factory);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container)
{
return new static($container->get('config.factory'));
}

/**
* {@inheritdoc}
*/
public function getFormId() {
public function getFormId()
{
return 'advanced_search_settings_form';
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
protected function getEditableConfigNames()
{
return [
self::CONFIG_NAME,
];
}

/**
* Get available fields from all Search API indexes.
*
* @return array
* An associative array of field identifiers to field labels.
*/
protected function getAvailableFields()
{
$field_options = [];

// Load all Search API indexes.
$index_storage = \Drupal::entityTypeManager()->getStorage('search_api_index');
$indexes = $index_storage->loadMultiple();

foreach ($indexes as $index) {
$fields = $index->getFields();
foreach ($fields as $field_id => $field) {
$field_options[$field_id] = $field->getLabel() . " (" . $field_id . ")";
}
}

return $field_options;
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state)
{
$form['eDisMax'] = [
'#type' => 'fieldset',
'#title' => $this->t('Advanced Search Block'),
'#weight' => -1,
];
$form['eDisMax']['advanced-search-block-description'] = [
'#markup' => $this->t("Advanced Search Blocks are available in the Blocks interface for each Search API view. When placing an Advanced Search Block, you can configure the fields that are used for field-based search and whether a recursive search is available. The following settings apply to all Advanced Search blocks."),
'#markup' => $this->t('Advanced Search Blocks are available in the Blocks interface for each Search API view. When placing an Advanced Search Block, you can configure the fields that are used for field-based search and whether a "recursive" search is available. The following settings apply to all Advanced Search blocks.'),
'#weight' => -2,
];

Expand Down Expand Up @@ -105,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
->t('Enable searching all fields'),
'#description' => $this->t('<ul>
<li>This makes an additional option visible in all Advanced Search Blocks, which searches across all fields. Its label is configured below.</li>
<li>This setting must be enabled for the Simple Search Block to function.</li>
<li>This setting must be enabled for the "Simple Search Block" to function.</li>
</ul>'),
'#default_value' => self::getConfig(self::SEARCH_ALL_FIELDS_FLAG, 0),
];
Expand All @@ -116,6 +147,39 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => self::getConfig(self::EDISMAX_SEARCH_LABEL, "Keyword"),
];

// Query Fields Block
$form['query_fields_block'] = [
'#type' => 'fieldset',
'#title' => $this->t('Query Fields'),
'#weight' => -0.5,
];

$field_options = $this->getAvailableFields();

$base_url = \Drupal::request()->getSchemeAndHttpHost();
$query_fields_description = $this->t('Select which fields should be queried when searching all fields label (keyword) chosen. If no fields are selected, <a href="@fields_url" target="_blank">all fields will be searched</a>.', [
'@fields_url' => $base_url . '/admin/config/search/search-api/index/default_solr_index_islandora_lite/fields',
]);

$form['query_fields_block']['query_fields_description'] = [
'#type' => 'item',
'#markup' => '<div style=" background: #f0f0f0; border-left: 4px solid #0074bd;">' . $query_fields_description . '</div>',
];

$form['query_fields_block']['query_fields_wrapper'] = [
'#type' => 'container',
'#attributes' => [
'style' => 'max-height: 350px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background: #f9f9f9;',
],
];

$form['query_fields_block']['query_fields_wrapper'][self::QUERY_FIELDS] = [
'#type' => 'checkboxes',
'#title' => $this->t('Select fields to query'),
'#options' => $field_options,
'#default_value' => self::getConfig(self::QUERY_FIELDS, []),
];

$form['display-mode'] = [
'#type' => 'fieldset',
'#title' => $this->t("Pager Block"),
Expand Down Expand Up @@ -198,6 +262,10 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Filter out unchecked checkboxes (value = '0') from query_fields.
$query_fields = $form_state->getValue(self::QUERY_FIELDS);
$query_fields = is_array($query_fields) ? array_filter($query_fields) : [];

$config = $this->configFactory->getEditable(self::CONFIG_NAME);
$config
->set(self::SEARCH_QUERY_PARAMETER, $form_state->getValue(self::SEARCH_QUERY_PARAMETER))
Expand All @@ -211,8 +279,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
->set(self::DISPLAY_LIST_FLAG, $form_state->getValue(self::DISPLAY_LIST_FLAG))
->set(self::DISPLAY_GRID_FLAG, $form_state->getValue(self::DISPLAY_GRID_FLAG))
->set(self::DISPLAY_DEFAULT, $form_state->getValue(self::DISPLAY_DEFAULT))
->set(self::QUERY_FIELDS, $query_fields)
->save();
parent::submitForm($form, $form_state);
}

}
}