Skip to content

More generous search #944

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 44 additions & 16 deletions src/DataTables/Filters/PartSearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,16 @@
/** @var bool Use Internal Part number for searching */
protected bool $ipn = true;

/** @var int Helper variable for hacky array_map variable injection */
protected int $it = 0;

public function __construct(
/** @var string The string to query for */
protected string $keyword
)
{
// Transform keyword and trim excess spaces
$keyword = trim(str_replace('+', ' ', $keyword));
}

protected function getFieldsToSearch(): array
Expand Down Expand Up @@ -125,26 +130,50 @@
return;
}

//Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string {
if ($this->regex) {
if($this->regex) {
//Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string {
return sprintf("REGEXP(%s, :search_query) = TRUE", $field);
}

return sprintf("ILIKE(%s, :search_query) = TRUE", $field);
}, $fields_to_search);
}, $fields_to_search);

//Add Or concatenation of the expressions to our query
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(...$expressions)
);
//Add Or concatenation of the expressions to our query
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(...$expressions)
);

//For regex, we pass the query as is, for like we add % to the start and end as wildcards
if ($this->regex) {
//For regex, we pass the query as is, save html special chars
$queryBuilder->setParameter('search_query', $this->keyword);
} else {
$queryBuilder->setParameter('search_query', '%' . $this->keyword . '%');
return;
}

//Split keyword on spaces, but limit token count to not blow up the DB
$tokens = explode(' ', $this->keyword, 5);

Check warning on line 150 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L150

Added line #L150 was not covered by tests

$params = new \Doctrine\Common\Collections\ArrayCollection();

Check warning on line 152 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L152

Added line #L152 was not covered by tests

//Perform search of every single token in every selected field, AND the where clauses
for ($i = 0; $i < sizeof($tokens); $i++) {
$this->it = $i;
$tokens[$i] = trim($tokens[$i]);

Check warning on line 157 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L155-L157

Added lines #L155 - L157 were not covered by tests

//Skip empty words (e.g. because of multiple spaces)
if ($tokens[$i] === '') continue;

Check warning on line 160 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L160

Added line #L160 was not covered by tests

//Convert the fields to search to a list of expressions
$expressions = array_map(function (string $field): string {
return sprintf("ILIKE(%s, :search_query%u) = TRUE", $field, $this->it);
}, $fields_to_search);

Check warning on line 165 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L163-L165

Added lines #L163 - L165 were not covered by tests

//Aggregate the parameters for consolidated commission
$params[] = new \Doctrine\ORM\Query\Parameter('search_query' . $i, '%' . $tokens[$i] . '%');

Check failure on line 168 in src/DataTables/Filters/PartSearchFilter.php

View workflow job for this annotation

GitHub Actions / Static analysis

Doctrine\Common\Collections\ArrayCollection<*NEVER*, *NEVER*> does not accept Doctrine\ORM\Query\Parameter.

Check warning on line 168 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L168

Added line #L168 was not covered by tests

//Add Or concatenation of the expressions to our query
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(...$expressions)
);

Check warning on line 173 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L171-L173

Added lines #L171 - L173 were not covered by tests
}
$queryBuilder->setParameters($params);

Check warning on line 175 in src/DataTables/Filters/PartSearchFilter.php

View check run for this annotation

Codecov / codecov/patch

src/DataTables/Filters/PartSearchFilter.php#L175

Added line #L175 was not covered by tests

}

public function getKeyword(): string
Expand Down Expand Up @@ -301,5 +330,4 @@
return $this;
}


}
Loading