Skip to content

Commit

Permalink
Indices: allow a different internal index name
Browse files Browse the repository at this point in the history
See #33
  • Loading branch information
dieterve committed Jan 15, 2016
1 parent 0c20186 commit a9d5524
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/Abstracts/AbstractIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public function __construct()
$this->setup();
}

/**
* The name used to communicate with the elasticsearch server. Can be used to
* add a prefix but still use the name to refer to it.
*
* @return string
*/
public function getInternalName()
{
return $this->getName();
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Abstracts/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function searchInIndices(array $indices)
foreach ($indices as $index) {
$index = $this->searcher->indicesManager()->getRegistered($index);

$this->indices[] = $index->getName();
$this->indices[] = $index->getInternalName();
}

// Remove doubles.
Expand Down
10 changes: 5 additions & 5 deletions src/Managers/DocumentsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function index($indexName, $type, array $data)
$index = $this->elasticSearcher->indicesManager()->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'body' => $data
];
Expand Down Expand Up @@ -63,7 +63,7 @@ public function delete($indexName, $type, $id)
$index = $this->elasticSearcher->indicesManager()->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'id' => $id
];
Expand All @@ -86,7 +86,7 @@ public function update($indexName, $type, $id, array $data)
$index = $this->elasticSearcher->indicesManager()->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'id' => $id,
'body' => ['doc' => $data]
Expand All @@ -107,7 +107,7 @@ public function exists($indexName, $type, $id)
$index = $this->elasticSearcher->indicesManager()->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'id' => $id,
];
Expand Down Expand Up @@ -146,7 +146,7 @@ public function get($indexName, $type, $id)
$index = $this->elasticSearcher->indicesManager()->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'id' => $id,
];
Expand Down
16 changes: 8 additions & 8 deletions src/Managers/IndicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function get($indexName)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName()
'index' => $index->getInternalName()
];

return $this->elasticSearcher->getClient()->indices()->getMapping($params);
Expand All @@ -128,7 +128,7 @@ public function getType($indexName, $type)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type
];

Expand All @@ -143,7 +143,7 @@ public function create($indexName)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'body' => $index->getBody()
];

Expand All @@ -162,7 +162,7 @@ public function update($indexName)

foreach ($index->getTypes() as $type => $typeBody) {
$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type,
'body' => [$type => $typeBody]
];
Expand All @@ -179,7 +179,7 @@ public function delete($indexName)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName()
'index' => $index->getInternalName()
];

$this->elasticSearcher->getClient()->indices()->delete($params);
Expand All @@ -194,7 +194,7 @@ public function deleteType($indexName, $type)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type
];

Expand All @@ -211,7 +211,7 @@ public function exists($indexName)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName()
'index' => $index->getInternalName()
];

return $this->elasticSearcher->getClient()->indices()->exists($params);
Expand All @@ -228,7 +228,7 @@ public function existsType($indexName, $type)
$index = $this->getRegistered($indexName);

$params = [
'index' => $index->getName(),
'index' => $index->getInternalName(),
'type' => $type
];

Expand Down
20 changes: 20 additions & 0 deletions tests/Managers/DocumentsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use ElasticSearcher\Managers\DocumentsManager;
use ElasticSearcher\Dummy\Indexes\MoviesIndex;
use ElasticSearcher\Dummy\Indexes\BooksIndex;

class DocumentsManagerTest extends ElasticSearcherTestCase
{
Expand All @@ -17,6 +18,7 @@ public function setUp()
// Create our example index.
$indicesManager = $this->getElasticSearcher()->indicesManager();
$indicesManager->register(new MoviesIndex());
$indicesManager->register(new BooksIndex());

$this->documentsManager = $this->getElasticSearcher()->documentsManager();
}
Expand Down Expand Up @@ -122,4 +124,22 @@ public function testUpdateOrIndex()
$document = $this->documentsManager->get('movies', 'movies', $id);
$this->assertEquals('2015', $document['_source']['year']);
}

public function testWithPrefixedIndex()
{
$id = 12345;
$data = [
'id' => $id,
'name' => 'Harry Potter and the wizards',
];

// Make sure the document doesn't exist.
if ($this->documentsManager->exists('books', 'books', $id)) {
$this->documentsManager->delete('books', 'books', $id);
}

$this->documentsManager->index('books', 'books', $data);
$this->assertTrue($this->documentsManager->exists('books', 'books', $id));
$this->assertEquals($data, $this->documentsManager->get('books', 'books', $id)['_source']);
}
}
19 changes: 19 additions & 0 deletions tests/Managers/IndicesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use ElasticSearcher\Managers\IndicesManager;
use ElasticSearcher\Dummy\Indexes\AuthorsIndex;
use ElasticSearcher\Dummy\Indexes\MoviesIndex;
use ElasticSearcher\Dummy\Indexes\BooksIndex;

class IndicesManagerTest extends ElasticSearcherTestCase
{
Expand Down Expand Up @@ -75,4 +76,22 @@ public function testDeleting()

$this->assertFalse($this->indicesManager->exists('authors'));
}

public function testWithPrefixedIndex()
{
$booksIndex = new BooksIndex();
$this->indicesManager->register($booksIndex);
if ($this->indicesManager->exists('books')) {
$this->indicesManager->delete('books');
}

$this->indicesManager->create('books');
$this->assertTrue($this->indicesManager->exists('books'));

$expectedIndex = ['prefix_books' => ['mappings' => $booksIndex->getTypes()]];
$this->assertEquals($expectedIndex, $this->indicesManager->get('books'));

$this->indicesManager->delete('books');
$this->assertFalse($this->indicesManager->exists('books'));
}
}
45 changes: 38 additions & 7 deletions tests/Queries/QueryTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use ElasticSearcher\Dummy\Indexes\BooksIndex;
use ElasticSearcher\Dummy\Indexes\MoviesIndex;
use ElasticSearcher\Dummy\Queries\MoviesFrom2014Query;
use ElasticSearcher\Dummy\Queries\MoviesFromXYearQuery;
use ElasticSearcher\Dummy\Queries\MovieWithIDXQuery;
use ElasticSearcher\Dummy\Queries\CountMoviesFrom2014Query;
use ElasticSearcher\Dummy\Queries\BooksFrom2014Query;

class QueryTest extends ElasticSearcherTestCase
{
Expand All @@ -15,10 +17,15 @@ public function setUp()
// Create our example index.
$indicesManager = $this->getElasticSearcher()->indicesManager();
$indicesManager->register(new MoviesIndex());
$indicesManager->register(new BooksIndex());
if ($indicesManager->exists('movies')) {
$indicesManager->delete('movies');
}
$indicesManager->create('movies');
if ($indicesManager->exists('books')) {
$indicesManager->delete('books');
}
$indicesManager->create('books');

// Index some test data.
$documentsManager = $this->getElasticSearcher()->documentsManager();
Expand All @@ -34,7 +41,7 @@ public function setUp()
public function testData()
{
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$data = ['year' => 2014, 'type' => 'dvd'];
$data = ['year' => 2014, 'type' => 'dvd'];
$query->addData($data);

$this->assertEquals($data, $query->getData());
Expand All @@ -51,8 +58,8 @@ public function testQueryBuilding()

$expectedQuery = [
'index' => 'movies',
'type' => 'movies',
'body' => [
'type' => 'movies',
'body' => [
'query' => [
'filtered' => [
'filter' => [
Expand All @@ -73,8 +80,8 @@ public function testQueryBuildingWithData()

$expectedQuery = [
'index' => 'movies',
'type' => 'movies',
'body' => [
'type' => 'movies',
'body' => [
'query' => [
'filtered' => [
'filter' => [
Expand All @@ -95,8 +102,8 @@ public function testQueryBuildingWithNestedFragments()

$expectedQuery = [
'index' => 'movies',
'type' => 'movies',
'body' => [
'type' => 'movies',
'body' => [
'query' => [
'filtered' => [
'filter' => [
Expand Down Expand Up @@ -135,4 +142,28 @@ public function testSettingSearchType()
$this->assertArrayHasKey('search_type', $raw);
$this->assertEquals('count', $raw['search_type']);
}

public function testQueryBuildingWithPrefixedIndex()
{
$query = new BooksFrom2014Query($this->getElasticSearcher());
$query->run(); // Needed because this calls setUp inside the query.

$this->assertEquals(['prefix_books'], $query->getIndices());
$this->assertEquals(['books'], $query->getTypes());

$expectedQuery = [
'index' => 'prefix_books',
'type' => 'books',
'body' => [
'query' => [
'bool' => [
'filter' => [
['term' => ['year' => 2014]]
]
]
]
]
];
$this->assertEquals($expectedQuery, $query->getRawQuery());
}
}
30 changes: 30 additions & 0 deletions tests/dummy/Indexes/BooksIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ElasticSearcher\Dummy\Indexes;

use ElasticSearcher\Abstracts\AbstractIndex;

class BooksIndex extends AbstractIndex
{
public function getName()
{
return 'books';
}

public function getInternalName()
{
return 'prefix_'.$this->getName();
}

public function setup()
{
$this->setTypes([
'books' => [
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
]
]
]);
}
}
16 changes: 16 additions & 0 deletions tests/dummy/Queries/BooksFrom2014Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ElasticSearcher\Dummy\Queries;

use ElasticSearcher\Abstracts\AbstractQuery;
use ElasticSearcher\Fragments\Queries\TermQuery;

class BooksFrom2014Query extends AbstractQuery
{
public function setup()
{
$this->searchIn('books', 'books');

$this->set('query.bool.filter', [new TermQuery('year', 2014)]);
}
}

0 comments on commit a9d5524

Please sign in to comment.