Add Lucene Search

This commit is contained in:
Kyle Huynh 2021-09-16 17:42:30 -04:00
parent 5b06be16f1
commit 2afe41ce79
4 changed files with 91 additions and 3 deletions

View file

@ -158,7 +158,22 @@ class AdvancedSearchQuery {
$q[] = $term->toSolrQuery($field_mapping);
}
$q = implode(' ', $q);
/** @var Solarium\QueryType\Select\Query\Query $solarium_query */
// enable dismax search query option
/** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */
$dismax = $solarium_query->getDisMax();
$dismax->setQueryParser('edismax');
$query_fields = [];
foreach ($field_mapping as $key => $field) {
foreach ($field as $f => $item) {
if (strpos($item, 'sticky') === false) {
array_push($query_fields, $item);
}
}
}
$query_fields = implode(" ", array_unique($query_fields));
$dismax->setQueryFields($query_fields);
$solarium_query->setQuery($q);
}
}

View file

@ -284,8 +284,13 @@ class AdvancedSearchQueryTerm {
$terms = [];
$query_helper = \Drupal::service('solarium.query_helper');
$value = $query_helper->escapePhrase(trim($this->value));
foreach ($solr_field_mapping[$this->field] as $field) {
$terms[] = "$field:$value";
if ($this->field === "all") {
return $value;
}
else {
foreach ($solr_field_mapping[$this->field] as $field) {
$terms[] = "$field:$value";
}
}
$terms = implode(' ', $terms);
return $this->include ? "($terms)" : "-($terms)";

View file

@ -103,6 +103,26 @@ class AdvancedSearchForm extends FormBase {
return self::getConfig(SettingsForm::SEARCH_REMOVE_OPERATOR, self::DEFAULT_REMOVE_OP);
}
/**
* Get if Lucene Search checkbox is enabled or disable
*
* @return boolean
*
*/
public static function getLuceneSearch() {
return self::getConfig(SettingsForm::LUCENE_SEARCH_FLAG, 0);
}
/**
* Get the character to use for removing a facet from the query.
*
* @return string
* The character to use for removing an facet to the query.
*/
public static function getLuceneSearchLabel() {
return self::getConfig(SettingsForm::LUCENE_SEARCH_LABEL, "All");
}
/**
* Convert the list of fields to select options.
*
@ -243,7 +263,7 @@ class AdvancedSearchForm extends FormBase {
],
];
$options = $this->fieldOptions($fields);
$options = (self::getLuceneSearch()) ? ["all" => self::getLuceneSearchLabel()] + $this->fieldOptions($fields) : $this->fieldOptions($fields);
$term_default_values = $this->defaultTermValues($options);
list($recursive, $term_values) = $this->processInput($form_state, $term_default_values);
$i = 0;

View file

@ -23,6 +23,9 @@ class SettingsForm extends ConfigFormBase {
const SEARCH_REMOVE_OPERATOR = 'search_remove_operator';
const FACET_TRUNCATE = 'facet_truncate';
const LUCENE_SEARCH_FLAG = 'lucene_on_off';
const LUCENE_SEARCH_LABEL = 'lucene_label';
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
@ -101,6 +104,38 @@ class SettingsForm extends ConfigFormBase {
],
],
];
$form['lucene'] = [
'#type' => 'fieldset',
'#title' => $this->t(" Solr's Standard Query parser (also known as 'lucene')"),
];
$form['lucene'][self::LUCENE_SEARCH_FLAG] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable Lucene Search.'),
'#default_value' => self::getConfig(self::LUCENE_SEARCH_FLAG, 0),
'#ajax' => [
'callback' => '::LuceneSearchEnableDisableCallback',
'wrapper' => 'lucene-container',
'effect' => 'fade',
],
];
$form['lucene']['textfields_container'] = [
'#type' => 'container',
'#attributes' => ['id' => 'lucene-container'],
];
if (self::getConfig(self::LUCENE_SEARCH_FLAG, "All") === 1
|| $form_state->getValue(self::LUCENE_SEARCH_FLAG) === 1) {
$form['lucene']['textfields_container'][self::LUCENE_SEARCH_LABEL] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#description' => $this->t('This label will be appear in Search Terms dropdown of Advanced Search form block if Lucene Search is enabled.'),
'#default_value' => self::getConfig(self::LUCENE_SEARCH_LABEL, "All"),
];
}
return parent::buildForm($form, $form_state);
}
@ -115,8 +150,21 @@ class SettingsForm extends ConfigFormBase {
->set(self::SEARCH_ADD_OPERATOR, $form_state->getValue(self::SEARCH_ADD_OPERATOR))
->set(self::SEARCH_REMOVE_OPERATOR, $form_state->getValue(self::SEARCH_REMOVE_OPERATOR))
->set(self::FACET_TRUNCATE, $form_state->getValue(self::FACET_TRUNCATE))
->set(self::LUCENE_SEARCH_FLAG, $form_state->getValue(self::LUCENE_SEARCH_FLAG))
->set(self::LUCENE_SEARCH_LABEL, $form_state->getValue(self::LUCENE_SEARCH_LABEL))
->save();
parent::submitForm($form, $form_state);
}
/**
* Callback for ajax_example_autotextfields.
*
* Selects the piece of the form we want to use as replacement markup and
* returns it as a form (renderable array).
*/
public function LuceneSearchEnableDisableCallback($form, FormStateInterface $form_state) {
return $form['lucene']['textfields_container'];
}
}