case sensitive option

This commit is contained in:
Natkeeran 2022-05-02 14:08:03 -04:00
parent 3e3b68c72d
commit 1e698ce6f8
2 changed files with 43 additions and 6 deletions

View file

@ -162,20 +162,31 @@ class AdvancedSearchQuery {
}
$q = implode(' ', $q);
$case_insensitive_field = $this::getConfig(SettingsForm::SOLR_CASE_INSENSITIVE_FIELD_PREFIX, '');
/** @var Solarium\QueryType\Select\Query\Query $solarium_query */
if ((strpos($q, "*") !== false || strpos($q, "?") !== false) || (strpos(trim($q), ' ') === false) ) {
if ((strpos($q, "*") !== false || strpos($q, "?") !== false)) {
// if the query string contain '*', '?', OR is a single world, enable wildcard
$tmp = str_replace('"', "", trim($q));
$query_fields = [];
foreach ($field_mapping as $key => $field) {
foreach ($field as $f => $item) {
if (strpos($item, 'sticky') === false && !in_array($item, ['score', 'random', 'boost_document'])
// bs_ are boolean fields, do not work well with text search
if (substr($item, 0, 3) !== "bs_" && !in_array($item, ['score', 'random', 'boost_document'])
&& ((strpos( $item, "sm_" ) === 0) || (strpos( $item, "tm_" ) === 0) || (strpos($item, "sort_ss_") === 0) || (strpos($item, "ts_") === 0)
|| (strpos($item, "ss_") === 0)
)){
array_push($query_fields, '('.$item. ':'. $tmp .')');
// Add case insensitive fields in the query search fields
if ($case_insensitive_field !== '') {
$item_prefix = substr($item, 0, 3);
if ($item_prefix == "ss_" || $item_prefix == "sm_") {
$case_insensitive_item = str_replace($item_prefix,$case_insensitive_field, $item);
array_push($query_fields, '('.$case_insensitive_item. ':'. $tmp .')');
}
}
}
}
}
@ -190,14 +201,30 @@ class AdvancedSearchQuery {
$query_fields = [];
foreach ($field_mapping as $key => $field) {
foreach ($field as $f => $item) {
if (strpos($item, 'sticky') === false) {
// bs_ are boolean fields, do not work well with text search
if (substr($item, 0, 3) !== "bs_") {
array_push($query_fields, $item);
if ($case_insensitive_field !== '') {
$item_prefix = substr($item, 0, 3);
if ($item_prefix == "ss_" || $item_prefix == "sm_") {
$case_insensitive_item = str_replace($item_prefix,$case_insensitive_field, $item);
array_push($query_fields, $case_insensitive_item);
}
}
}
}
}
$query_fields = implode(" ", array_unique($query_fields));
$dismax->setQueryFields($query_fields);
// Looks like, somewhere in the previous steps, double quotes get added, we are removing them!
// ToDo: Look into where the original quotes are being added, and remove it there!
$q = trim($q);
$q = substr($q, 1);
$q = substr($q, 0, -1);
}
$solarium_query->setQuery($q);
}
}

View file

@ -22,7 +22,7 @@ class SettingsForm extends ConfigFormBase {
const SEARCH_ADD_OPERATOR = 'search_add_operator';
const SEARCH_REMOVE_OPERATOR = 'search_remove_operator';
const FACET_TRUNCATE = 'facet_truncate';
const SOLR_CASE_INSENSITIVE_FIELD_PREFIX = "case_insensitive_solr_field_prefix";
const LUCENE_SEARCH_FLAG = 'lucene_on_off';
const LUCENE_SEARCH_LABEL = 'lucene_label';
@ -103,6 +103,16 @@ class SettingsForm extends ConfigFormBase {
'#min' => 1,
],
],
'solr_case_insensitive_field_prefix' => [
'#type' => 'fieldset',
'#title' => $this->t('Case Insensitive Search'),
self::SOLR_CASE_INSENSITIVE_FIELD_PREFIX => [
'#type' => 'textfield',
'#title' => $this->t('Prefix for Solr Case Insensitive Field'),
'#description' => $this->t('If you have configured case insenstive fields, please specify the prefix here (i.e sss_lowercase_)'),
'#default_value' => self::getConfig(self::SOLR_CASE_INSENSITIVE_FIELD_PREFIX, ""),
],
],
];
$form['lucene'] = [
@ -150,7 +160,7 @@ 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::SOLR_CASE_INSENSITIVE_FIELD_PREFIX, $form_state->getValue(self::SOLR_CASE_INSENSITIVE_FIELD_PREFIX))
->set(self::LUCENE_SEARCH_FLAG, $form_state->getValue(self::LUCENE_SEARCH_FLAG))
->set(self::LUCENE_SEARCH_LABEL, $form_state->getValue(self::LUCENE_SEARCH_LABEL))