Update AdvancedSearchQueryTerm.php

This commit is contained in:
Kyle Huynh 2023-01-25 15:42:06 -05:00 committed by GitHub
parent f7e14711ea
commit c6d6072031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -285,19 +285,69 @@ class AdvancedSearchQueryTerm {
$query_helper = \Drupal::service('solarium.query_helper'); $query_helper = \Drupal::service('solarium.query_helper');
$value = $query_helper->escapePhrase(trim($this->value)); $value = $query_helper->escapePhrase(trim($this->value));
// Added to handle exact matches keyword (surrounded by "")
if (preg_match('#^(\'|").+\1$#', $value) == 1) {
trim($value);
$value = str_replace('"\\', '', $value);
$value = str_replace('\\"', '', $value);
}
if ($this->field === "all") { if ($this->field === "all") {
// Case 1: if keyword contains one word or a phrase
if(strpos(trim($value), ' ') !== false) {
// phrase
// add Or for the search case "scarborough bulletin" show no results
if (substr_count($value, '\"') == 2) {
$value = str_replace('\"', "", trim($value));
return $value; return $value;
} }
else { else {
return $value . " OR " . str_replace('"', "", trim($value));
}
}
if (!$this->getInclude()) {
$value = "!" . str_replace('"', "", trim($value));
}
else {
// one word
$value = str_replace('"', "", trim($value));
}
return $value;
}
else {
$isTitleSearch = false;
foreach ($solr_field_mapping[$this->field] as $field) { foreach ($solr_field_mapping[$this->field] as $field) {
// if field fulltext title is selected
if (strpos($field, "fulltext_title") !== false) {
$isTitleSearch = true;
if (strpos(trim($value), " AND " ) !== false) {
//When you type 'Orientation AND games' into the title search, you get one result.
// When you do the same search but add a search box, you get a lot more results.
// (Recreation: Add a search box, set both search criteria to 'Title' and keep the operator to 'and'.
// Type 'orientation' in one box and 'games' in the second box and click seach.)
$keyword = str_replace('"', '', $value);
$keys = explode(" AND ", $keyword);
$str = "(";
$i = 0;
foreach ($keys as $key) {
if ($i != count($keys)-1)
$str .= $field . ':"' .$key . '" AND ';
else
$str .= $field . ':"' .$key . '")';
$i++;
}
$terms[] = $str;
}
else {
if ($isTitleSearch) {
$terms[] = 'tm_lowercase_title:'. $value;
}
else {
$terms[] = "$field:$value"; $terms[] = "$field:$value";
} }
}
}
else {
$terms[] = "$field:$value";
}
}
} }
$terms = implode(' ', $terms); $terms = implode(' ', $terms);
return $this->include ? "($terms)" : "-($terms)"; return $this->include ? "($terms)" : "-($terms)";