Seperated detection of dismax and field search

Exact match: need to change full text field to String to be effective
This commit is contained in:
Kyle Huynh 2022-05-04 13:27:07 +00:00
parent 51028bdf48
commit 4d3a4df647
2 changed files with 74 additions and 48 deletions

View file

@ -152,16 +152,34 @@ class AdvancedSearchQuery {
// disable for Lucene and wildcard // disable for Lucene and wildcard
//$q[] = "{!boost b=boost_document}"; //$q[] = "{!boost b=boost_document}";
// create a flag for active/inactive dismax
$isDismax = false;
// To support negative queries we must first bring in all documents. // To support negative queries we must first bring in all documents.
$q[] = $this->negativeQuery($terms) ? "*:*" : ""; $q[] = $this->negativeQuery($terms) ? "*:*" : "";
$term = array_shift($terms); $term = array_shift($terms);
$q[] = $term->toSolrQuery($field_mapping); $q[] = $term->toSolrQuery($field_mapping);
// set dismax is enabled if the field set to "all"
if ($term->getField() === "all") {
$isDismax = true;
}
// for multiple conditions
foreach ($terms as $term) { foreach ($terms as $term) {
$q[] = $term->getConjunction(); $q[] = $term->getConjunction();
$q[] = $term->toSolrQuery($field_mapping); $q[] = $term->toSolrQuery($field_mapping);
// set dismax is enabled if the field set to "all"
if ($term->getField() === "all") {
$isDismax = true;
} }
}
$q = implode(' ', $q); $q = implode(' ', $q);
// Limit extra processing if Luncene Search is enable
if ($isDismax) {
$case_insensitive_field = $this::getConfig(SettingsForm::SOLR_CASE_INSENSITIVE_FIELD_PREFIX, ''); $case_insensitive_field = $this::getConfig(SettingsForm::SOLR_CASE_INSENSITIVE_FIELD_PREFIX, '');
/** @var Solarium\QueryType\Select\Query\Query $solarium_query */ /** @var Solarium\QueryType\Select\Query\Query $solarium_query */
@ -217,14 +235,8 @@ class AdvancedSearchQuery {
} }
$query_fields = implode(" ", array_unique($query_fields)); $query_fields = implode(" ", array_unique($query_fields));
$dismax->setQueryFields($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); $solarium_query->setQuery($q);
} }
} }

View file

@ -284,6 +284,13 @@ class AdvancedSearchQueryTerm {
$terms = []; $terms = [];
$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") {
return $value; return $value;
} }
@ -295,5 +302,12 @@ class AdvancedSearchQueryTerm {
$terms = implode(' ', $terms); $terms = implode(' ', $terms);
return $this->include ? "($terms)" : "-($terms)"; return $this->include ? "($terms)" : "-($terms)";
} }
/**
* Get Field search
*/
public function getField() {
return $this->field;
}
} }