Skip to content

Commit

Permalink
Add optional index name to use when performing Elasticsearch operatio…
Browse files Browse the repository at this point in the history
…ns (#27)
  • Loading branch information
brendt authored and freekmurze committed Dec 18, 2017
1 parent aac4ee5 commit f35b3c5
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/SearchIndexHandlers/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public function setIndexName($indexName)
*
* @param Searchable|array|Traversable $subject
*/
public function upsertToIndex($subject)
public function upsertToIndex($subject, $indexName = null)
{
$indexName = $this->resolveIndexName($indexName);

if ($subject instanceof Searchable) {
$subject = [$subject];
}
Expand All @@ -56,13 +58,13 @@ public function upsertToIndex($subject)
throw new InvalidArgumentException();
}
})
->flatMap(function ($item) {
->flatMap(function ($item) use($indexName) {
return
[
[
'index' => [
'_id' => $item->getSearchableId(),
'_index' => $this->indexName,
'_index' => $indexName,
'_type' => $item->getSearchableType(),
],
],
Expand All @@ -87,11 +89,13 @@ public function upsertToIndex($subject)
*
* @param Searchable $subject
*/
public function removeFromIndex(Searchable $subject)
public function removeFromIndex(Searchable $subject, $indexName = null)
{
$indexName = $this->resolveIndexName($indexName);

$this->elasticsearch->delete(
[
'index' => $this->indexName,
'index' => $indexName,
'type' => $subject->getSearchableType(),
'id' => $subject->getSearchableId(),
]
Expand All @@ -104,11 +108,13 @@ public function removeFromIndex(Searchable $subject)
* @param string $type
* @param int $id
*/
public function removeFromIndexByTypeAndId($type, $id)
public function removeFromIndexByTypeAndId($type, $id, $indexName = null)
{
$indexName = $this->resolveIndexName($indexName);

$this->elasticsearch->delete(
[
'index' => $this->indexName,
'index' => $indexName,
'type' => $type,
'id' => $id,
]
Expand All @@ -120,9 +126,11 @@ public function removeFromIndexByTypeAndId($type, $id)
*
* @return mixed
*/
public function clearIndex()
public function clearIndex($indexName = null)
{
$this->elasticsearch->indices()->delete(['index' => $this->indexName]);
$indexName = $this->resolveIndexName($indexName);

$this->elasticsearch->indices()->delete(['index' => $indexName]);
}

/**
Expand All @@ -146,4 +154,13 @@ public function getClient()
{
return $this->elasticsearch;
}

protected function resolveIndexName($indexName = null)
{
if (!$indexName) {
$indexName = $this->indexName;
}

return $indexName;
}
}

0 comments on commit f35b3c5

Please sign in to comment.