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

Using multi indcies/types to search #108

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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 5.5
- 5.6
- 7.0

include:
- php: 5.5
Expand Down
86 changes: 86 additions & 0 deletions src/ElasticquentMultiSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Elasticquent;

use Illuminate\Database\Eloquent\Model;

class ElasticquentMultiSearch extends Model implements ElasticquentInterface
{
use ElasticquentTrait {
getBasicEsParams as TraitGetBasicEsParams;
getIndexName as TraitGetIndexName;
}

protected $types = [];

protected $indices = [];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);

// initialize indices
$this->indices = [$this->TraitGetIndexName()];
}

/**
* Set indcies to search from
* @param mixed $indices
*/
public function setIndexName($indices)
{
$this->indices = $indices;
return $this;
}

public function getIndexName()
{
return $this->indices;
}

/**
* Set types to search from
* @param mixed $types
*/
public function setTypeName($types)
{
$this->types = $types;
return $this;

}

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);
}
}
2 changes: 1 addition & 1 deletion src/ElasticquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function newCollection(array $models = array())
*/
public function getTypeName()
{
return $this->getTable();
return get_class($this);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/ElasticSearchMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -24,15 +24,15 @@ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase
'hits' => [
[
'_index' => 'my_custom_index_name',
'_type' => 'test_table',
'_type' => 'SearchTestModel',
'_score' => 0.7768564,
'_source' => [
'name' => 'foo',
]
],
[
'_index' => 'my_custom_index_name',
'_type' => 'test_table',
'_type' => 'SearchTestModel',
'_score' => 0.5634561,
'_source' => [
'name' => 'bar',
Expand Down
2 changes: 1 addition & 1 deletion tests/ElasticquentTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setup()
*/
public function testTypeNameInferredFromTableName()
{
$this->assertEquals('test_table', $this->model->getTypeName());
$this->assertEquals('TestModel', $this->model->getTypeName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/stubs/parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function basicParameters()
{
return [
'index' => 'my_custom_index_name',
'type' => 'test_table',
'type' => 'SearchTestModel',
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/stubs/results.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ function successfulResults()
'hits' => [
[
'_index' => 'my_custom_index_name',
'_type' => 'test_table',
'_type' => 'SearchTestModel',
'_score' => 0.7768564,
'_source' => [
'name' => 'foo',
]
],
[
'_index' => 'my_custom_index_name',
'_type' => 'test_table',
'_type' => 'SearchTestModel',
'_score' => 0.5634561,
'_source' => [
'name' => 'bar',
Expand Down