From a9a109f25142084e0f8e47fa498f2c4518c9e00a Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 21 Aug 2016 08:16:33 +0800 Subject: [PATCH 1/3] Using multi indcies/types to search --- src/ElasticquentMultiSearch.php | 84 +++++++++++++++++++++++++++++++++ src/ElasticquentTrait.php | 2 +- 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/ElasticquentMultiSearch.php diff --git a/src/ElasticquentMultiSearch.php b/src/ElasticquentMultiSearch.php new file mode 100644 index 0000000..2c74109 --- /dev/null +++ b/src/ElasticquentMultiSearch.php @@ -0,0 +1,84 @@ +indices = [$this->TraitGetIndexName()]; + } + + /** + * Set indcies to search from + * @param mixed $indices + */ + public function setIndexName($indices) + { + $this->indices = $indices; + } + + public function getIndexName() + { + return $this->indices; + } + + /** + * Set types to search from + * @param mixed $types + */ + public function setTypeName($types) + { + $this->types = $types; + + } + + public function getTypeName() + { + return $this->types; + } + + /** + * Create a elacticquent result collection of models from plain arrays. + * + * Use _type to instantiate models + * + * @param array $items + * @param array $meta + * @return \Elasticquent\ElasticquentResultCollection + */ + public static function hydrateElasticquentResult(array $items, $meta = null) + { + // Cache instances + $instances = []; + + $results = []; + + foreach ($items as $item) { + $className = $item['_type']; + if (!class_exists($className)) { + continue; + } + if (!isset($instances[$className])) { + $instances[$className] = new $className; + } + $results[] = $instances[$className]->newFromHitBuilder($item); + } + + return (new static)->newElasticquentResultCollection($results, $meta); + } +} diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index a23e7c4..c08429c 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -71,7 +71,7 @@ public function newCollection(array $models = array()) */ public function getTypeName() { - return $this->getTable(); + return get_class($this); } /** From 4e011fe63eef92012a8b5b9000aeaa5c50499c4b Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 21 Aug 2016 08:37:28 +0800 Subject: [PATCH 2/3] chaining method --- src/ElasticquentMultiSearch.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ElasticquentMultiSearch.php b/src/ElasticquentMultiSearch.php index 2c74109..ee3cbd7 100644 --- a/src/ElasticquentMultiSearch.php +++ b/src/ElasticquentMultiSearch.php @@ -30,6 +30,7 @@ public function __construct(array $attributes = []) public function setIndexName($indices) { $this->indices = $indices; + return $this; } public function getIndexName() @@ -44,6 +45,7 @@ public function getIndexName() public function setTypeName($types) { $this->types = $types; + return $this; } From de24b6ec334380ff32b22ae54e07487ce8e9140e Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 21 Aug 2016 09:10:04 +0800 Subject: [PATCH 3/3] testing --- .travis.yml | 1 + tests/ElasticSearchMethodsTest.php | 10 +++++----- tests/ElasticquentTraitTest.php | 2 +- tests/stubs/parameters.php | 2 +- tests/stubs/results.php | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index cdc78f4..014685e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 5.5 - 5.6 + - 7.0 include: - php: 5.5 diff --git a/tests/ElasticSearchMethodsTest.php b/tests/ElasticSearchMethodsTest.php index 2fbd0a9..0ac565e 100644 --- a/tests/ElasticSearchMethodsTest.php +++ b/tests/ElasticSearchMethodsTest.php @@ -12,9 +12,9 @@ * specifically returns results consistent with the ElasticSearch PHP client version * 2.0 documentation. * - * The Elasticquent method will then format the response and we test that the resulting - * Elasticquent results collection methods return the results we expect to verify this. - */ + * The Elasticquent method will then format the response and we test that the resulting + * Elasticquent results collection methods return the results we expect to verify this. + */ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase { @@ -24,7 +24,7 @@ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase 'hits' => [ [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.7768564, '_source' => [ 'name' => 'foo', @@ -32,7 +32,7 @@ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase ], [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.5634561, '_source' => [ 'name' => 'bar', diff --git a/tests/ElasticquentTraitTest.php b/tests/ElasticquentTraitTest.php index 5939857..6d57b68 100644 --- a/tests/ElasticquentTraitTest.php +++ b/tests/ElasticquentTraitTest.php @@ -20,7 +20,7 @@ public function setup() */ public function testTypeNameInferredFromTableName() { - $this->assertEquals('test_table', $this->model->getTypeName()); + $this->assertEquals('TestModel', $this->model->getTypeName()); } /** diff --git a/tests/stubs/parameters.php b/tests/stubs/parameters.php index 557b1b9..5c7d567 100644 --- a/tests/stubs/parameters.php +++ b/tests/stubs/parameters.php @@ -8,7 +8,7 @@ function basicParameters() { return [ 'index' => 'my_custom_index_name', - 'type' => 'test_table', + 'type' => 'SearchTestModel', ]; } diff --git a/tests/stubs/results.php b/tests/stubs/results.php index 6283a83..744e971 100644 --- a/tests/stubs/results.php +++ b/tests/stubs/results.php @@ -20,7 +20,7 @@ function successfulResults() 'hits' => [ [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.7768564, '_source' => [ 'name' => 'foo', @@ -28,7 +28,7 @@ function successfulResults() ], [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.5634561, '_source' => [ 'name' => 'bar',