Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue https://github.com/Islandora/advanced_search/issues/53 #54

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
86 changes: 18 additions & 68 deletions src/AdvancedSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
});

Expand Down