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

Paginated list extras #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/SilverStripe/Elastica/ReindexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function run($request) {
print(\Director::is_cli() ? "$content\n" : "<p>$content</p>");
};

$message('Defining the mappings');
$this->service->define();
// $message('Defining the mappings');
// $this->service->define();

$message('Refreshing the index');
$this->service->refresh();
Expand Down
18 changes: 17 additions & 1 deletion src/SilverStripe/Elastica/ResultList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ResultList extends \ViewableData implements \SS_Limitable, \SS_List {

private $index;
private $query;
private $search = null;

public function __construct(Index $index, Query $query) {
$this->index = $index;
Expand All @@ -36,11 +37,22 @@ public function getQuery() {
return $this->query;
}

/**
* @return Elastica search result (cached)
*/
public function getSearch() {
if(!$this->search) {
$this->search = $this->index->search($this->query);
}

return $this->search;
}

/**
* @return array
*/
public function getResults() {
return $this->index->search($this->query)->getResults();
return $this->getSearch()->getResults();
}

public function getIterator() {
Expand Down Expand Up @@ -145,6 +157,10 @@ public function count() {
return count($this->toArray());
}

public function getTotalHits() {
return $this->getSearch()->getTotalHits();
}

/**
* @ignore
*/
Expand Down
8 changes: 7 additions & 1 deletion src/SilverStripe/Elastica/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ public function getElasticaDocument() {
* Updates the record in the search index.
*/
public function onAfterWrite() {
$this->service->index($this->owner);
if($this->owner->ShowInSearch && $this->owner->isPublished()) {
$this->service->index($this->owner);
} else {
try {
$this->service->remove($this->owner);
} catch (\Elastica\Exception\NotFoundException $e) { }
}
}

/**
Expand Down