From 0213a0aa66631ba7f80b1d320e8a441f3002f61b Mon Sep 17 00:00:00 2001 From: Kyle Huynh <7862086+kylehuynh205@users.noreply.github.com> Date: Fri, 5 Jul 2024 06:58:03 -0400 Subject: [PATCH] Update AdvancedSearchQuery.php Removed the old code which handle wildcard search with dismax, it's not needed with edismax --- src/AdvancedSearchQuery.php | 86 ++++++++----------------------------- 1 file changed, 18 insertions(+), 68 deletions(-) diff --git a/src/AdvancedSearchQuery.php b/src/AdvancedSearchQuery.php index af40fb7..2df0e25 100644 --- a/src/AdvancedSearchQuery.php +++ b/src/AdvancedSearchQuery.php @@ -151,7 +151,8 @@ public function alterQuery(Request $request, SolariumQueryInterface &$solarium_q $field_mapping = $backend->getSolrFieldNamesKeyedByLanguage($language_ids, $index); // Disable for Lucene and wildcard - // $q[] = "{!boost b=boost_document}"; + //$q[] = "{!boost b=boost_document}"; + // Create a flag for active/inactive dismax. $config = \Drupal::config(SettingsForm::CONFIG_NAME); $isDismax = $config->get(SettingsForm::EDISMAX_SEARCH_FLAG); @@ -197,84 +198,33 @@ public function alterQuery(Request $request, SolariumQueryInterface &$solarium_q // Limit extra processing if Luncene Search is enable. if ($isDismax) { - - if ((strpos($q, "*") !== FALSE || strpos($q, "?") !== FALSE)) { - // If the query string contain '*','?',a single world,enable wildcard. - $tmp = str_replace('"', "", trim($q)); - $query_fields = []; - - if ($isSearchAllFields) { - foreach ($field_mapping as $key => $field) { - foreach ($field as $f => $item) { - // 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 . ')'); - } - } - } - } - else { - foreach ($fields_list as $f) { - $parts = explode(" ", $f); - foreach ($parts as $p) { - array_push($query_fields, '(' . $p . ':' . $tmp . ')'); + // Enable dismax search query option. + /** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */ + $dismax = $solarium_query->getEDisMax(); + $dismax->setQueryParser('edismax'); + $query_fields = []; + + if ($isSearchAllFields) { + foreach ($field_mapping as $key => $field) { + foreach ($field as $f => $item) { + // bs_ are boolean fields, do not work well with text search. + if (substr($item, 0, 3) !== "bs_") { + array_push($query_fields, $item); } } } - $q = implode(" ", array_unique($query_fields)); } else { + $query_fields = $fields_list; - // Enable dismax search query option. - /** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */ - $dismax = $solarium_query->getEDisMax(); - $dismax->setQueryParser('edismax'); - $query_fields = []; - - if ($isSearchAllFields) { - foreach ($field_mapping as $key => $field) { - foreach ($field as $f => $item) { - // bs_ are boolean fields, do not work well with text search. - if (substr($item, 0, 3) !== "bs_") { - array_push($query_fields, $item); - } - } - } - } - else { - $query_fields = $fields_list; - } - - // Get the indexed fields from /admin/config/search/search-api/index/..../fields - $boostedFields = []; - foreach ($index->getFields() as $field_id => $field) { - $boostedFields[$field_id] = $field->getBoost(); - } - - $str_fields_with_boost = ""; - // Adding a boost number for each field) - foreach($query_fields as $solr_field) { - foreach($boostedFields as $indexed_field => $boostnum) { - if(strpos($str_fields_with_boost, $indexed_field) == false && strpos($solr_field, $indexed_field) !== false) { - $str_fields_with_boost .= $solr_field . "^" . $boostnum . " "; - } - } - } - - $dismax->setQueryFields($str_fields_with_boost); } + $query_fields = implode(" ", array_unique($query_fields)); + $dismax->setQueryFields($query_fields); } 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) { + $highlighted_fields = array_filter(array_unique($fields_list), function ($v) { return preg_match('/^t.*?[sm]_/', $v) || preg_match('/^s[sm]_/', $v); });