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

Added parameter to allow filtering using ORM filters #68

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions src/Service/AlgoliaIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ public function exportAttributesFromObject($item)
'objectCreated' => $item->dbObject('Created')->getTimestamp()
];

if ($item->hasMethod('AbsoluteLink')) {
if ($item->hasMethod('AbsoluteLink') && !empty($item->AbsoluteLink())) {
$toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->AbsoluteLink());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to call AbsoluteLink twice for every record indexing. While that can be a quick method it may also do quite a few look ups (subsite etc). Can we save the value to a value after the hasMethod $link = $item->AbsoluteLink(); and use that $link on the line below. Likewise for link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated now

} elseif (method_exists($item, 'Link') && !empty($item->Link())) {
$toIndex['objectLink'] = str_replace(['?stage=Stage', '?stage=Live'], '', $item->Link());
}

if ($item && $item->hasMethod('exportObjectToAlgolia')) {
Expand Down Expand Up @@ -282,7 +284,7 @@ public function exportAttributesFromRelationship($item, $relationship, $attribut
try {
$data = [];

$related = $item->{$relationship}();
$related = $item->relObject($relationship);

if (!$related || !$related->exists()) {
return;
Expand Down
7 changes: 6 additions & 1 deletion src/Service/AlgoliaQuerier.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class AlgoliaQuerier
* @param string $selectedIndex
* @param string $query
* @param array $searchParameters
* @param array $ORMFilters filter ORM objects prior to returning the results as a PaginatedList
*
* @return PaginatedList
*/
public function fetchResults($selectedIndex = null, $query = '', $searchParameters = [])
public function fetchResults($selectedIndex = null, $query = '', $searchParameters = [], $ORMFilters = [])
{
$service = Injector::inst()->get(AlgoliaService::class);
$results = false;
Expand Down Expand Up @@ -78,6 +79,10 @@ function array_key_first(array $arr)

$this->lastResult = $results;

if (!empty($ORMFilters)) {
$records = $records->filter($ORMFilters);
}

$output = PaginatedList::create($records);

if ($results) {
Expand Down