Skip to content

Commit

Permalink
Merge pull request #76 from matchish/elasticsearch-7
Browse files Browse the repository at this point in the history
Add Elasticsearch 7 support
  • Loading branch information
matchish authored Nov 18, 2019
2 parents 8feba2f + c1e6191 commit 103129e
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build:
nodes:
analysis:
services:
elasticsearch: 6.6.2
elasticsearch: 7.4.2
mysql: 5.7
tests:
override:
Expand Down
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
- mysql

env:
- ELASTICSEARCH_HOST=localhost:9200 DB_DATABASE=scout_database DB_USERNAME=travis DB_PASSWORD=""
- ELASTICSEARCH_BUILD=7.1.1-amd64.deb ELASTICSEARCH_HOST=localhost:9200 DB_DATABASE=scout_database DB_USERNAME=travis DB_PASSWORD=""

php:
- 7.1
Expand All @@ -19,10 +19,17 @@ cache:
matrix:
allow_failures:
- php: nightly
- env: ELASTICSEARCH_BUILD=7.1.1

before_script:
- mysql -e "create database $DB_DATABASE;"
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.deb && sudo dpkg -i --force-confnew elasticsearch-6.6.2.deb && sudo service elasticsearch restart
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$ELASTICSEARCH_BUILD
- sudo dpkg -i --force-confnew elasticsearch-$ELASTICSEARCH_BUILD
- sudo sed -i.old 's/-Xms1g/-Xms128m/' /etc/elasticsearch/jvm.options
- sudo sed -i.old 's/-Xmx1g/-Xmx128m/' /etc/elasticsearch/jvm.options
- echo -e '-XX:+DisableExplicitGC\n-Djdk.io.permissionsUseCanonicalPath=true\n-Dlog4j.skipJansi=true\n-server\n' | sudo tee -a /etc/elasticsearch/jvm.options
- sudo chown -R elasticsearch:elasticsearch /etc/default/elasticsearch
- sudo service elasticsearch restart
- sleep 30
- curl -X GET "localhost:9200/_cluster/health?wait_for_status=yellow&timeout=50s"
- travis_retry composer self-update
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [3.0.0] - 2019-11-17
### Added
- Elasticsearch 7 support
- Added interface binding for HitsIteratorAggregate for custom implementation

## [2.1.0] - 2019-11-13
### Added
- Import source factory
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ help: ## Show this help

up: ## Start all containers (in background) for development
sudo sysctl -w vm.max_map_count=262144
$(docker_compose_bin) up --no-recreate -d
$(docker_compose_bin) up -d

down: ## Stop all started for development containers
$(docker_compose_bin) down
Expand Down
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ great features, and at the same time leverage the complete set of ElasticSearch

If you need any help, [stack overflow](https://stackoverflow.com/questions/tagged/laravel-scout%20laravel%20elasticsearch) is the preferred and recommended way to ask support questions.

## :two_hearts: Features
## :two_hearts: Features
Don't forget to :star: the package if you like it. :pray:

- [Search amongst multiple models](#search-amongst-multiple-models)
- [**Zero downtime** reimport](#zero-downtime-reimport) - it’s a breeze to import data in production.
- [Eager load relations](#eager-load) - speed up your import.
- Elasticsearch **7.0** ready - Use [elasticsearch-7](https://github.com/matchish/laravel-scout-elasticsearch/tree/elasticsearch-7) branch instead.
- Elasticsearch **7.0** ready
- Import all searchable models at once.
- A fully configurable mapping for each model.
- Full power of ElasticSearch in your queries.
Expand All @@ -39,7 +40,11 @@ If you need any help, [stack overflow](https://stackoverflow.com/questions/tagge

- PHP version >= 7.1.3
- Laravel Framework version >= 5.6
- Elasticsearch version >= 6

| Elasticsearch version | ElasticsearchDSL version |
| --------------------- | --------------------------- |
| >= 7.0 | >= 3.0.0 |
| >= 6.0, < 7.0 | < 3.0.0 |

## :rocket: Installation

Expand Down Expand Up @@ -190,7 +195,11 @@ Mixed::search('title:Barcelona or to:Barcelona')
In this example you will get collection of `Ticket` and `Book` models where ticket's arrival city or
book title is `Barcelona`

>Don't forget to :star: the package if you like it. :pray:
### Working with results
Often your response isn't collection of models but aggregations or models with higlights an so on.
In this case you need to implement your own implementation of `HitsIteratorAggregate` and bind it in your service provider

[Here is a case](https://github.com/matchish/laravel-scout-elasticsearch/issues/28)

## :free: License
Scout ElasticSearch is an open-sourced software licensed under the [MIT license](LICENSE.md).
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "This package extends Laravel Scout adding full power of ElasticSearch",
"version": "2.1.0",
"version": "3.0.0",
"keywords": [
"laravel",
"scout",
Expand All @@ -18,9 +18,9 @@
],
"require": {
"php": "^7.1.3",
"elasticsearch/elasticsearch": "~6.0",
"elasticsearch/elasticsearch": ">=7.2",
"laravel/scout": "^6.1.1|^7.0",
"ongr/elasticsearch-dsl": "^6.0"
"ongr/elasticsearch-dsl": "^7.0"
},
"require-dev": {
"fzaninotto/faker": "^1.8",
Expand Down
8 changes: 3 additions & 5 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
'indices' => [
'mappings' => [
'default' => [
'_doc' => [
'properties' => [
'id' => [
'type' => 'keyword',
],
'properties' => [
'id' => [
'type' => 'keyword',
],
],
],
Expand Down
12 changes: 5 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,13 @@ services:
networks:
- default
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.6.0
image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1
user: "1000:1000"
volumes:
- elasticsearch-data:/usr/share/elasticsearch/data
environment:
- cluster.name=docker-cluster
- cluster.routing.allocation.disk.threshold_enabled=true
- cluster.routing.allocation.disk.watermark.flood_stage=200mb
- cluster.routing.allocation.disk.watermark.low=500mb
- cluster.routing.allocation.disk.watermark.high=300mb
- discovery.type=single-node
- cluster.routing.allocation.disk.threshold_enabled=false
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
Expand All @@ -60,7 +57,8 @@ services:
image: lmenezes/cerebro
ports:
- "9002:9000"

networks:
- default
networks:
default:
driver: bridge
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ parameters:
-
path: %currentWorkingDirectory%/src/Engines/ElasticSearchEngine.php
message: '#message.*ServerErrorResponseException constructor expects string, string|false given.#'
-
path: %currentWorkingDirectory%/src/Engines/ElasticSearchEngine.php
message: '#Parameter \#2 \$search of function array_key_exists expects array, array\|\(callable\) given\.#'
-
path: %currentWorkingDirectory%/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php
message: '#Argument of an invalid type array\|\(callable\) supplied for foreach, only iterables are supported\.#'
10 changes: 10 additions & 0 deletions src/ElasticSearch/HitsIteratorAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Matchish\ScoutElasticSearch\ElasticSearch;

interface HitsIteratorAggregate extends \IteratorAggregate
{
public function __construct(array $results, callable $callback = null);

public function getIterator();
}
5 changes: 5 additions & 0 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function register(): void
$this->app->bind(Client::class, function () {
return ClientBuilder::create()->setHosts([config('elasticsearch.host')])->build();
});

$this->app->bind(
'Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate',
'Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate'
);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Laravel\Scout\Builder as BaseBuilder;
use Laravel\Scout\Engines\Engine;
use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Search as SearchParams;
Expand All @@ -28,7 +28,7 @@ final class ElasticSearchEngine extends Engine
/**
* Create a new engine instance.
*
* @param \Elasticsearch\Client $elasticsearch
* @param \Elasticsearch\Client $elasticsearch
* @return void
*/
public function __construct(\Elasticsearch\Client $elasticsearch)
Expand Down Expand Up @@ -107,7 +107,10 @@ public function mapIds($results)
*/
public function map(BaseBuilder $builder, $results, $model)
{
$hits = new EloquentHitsIteratorAggregate($results, $builder->queryCallback);
$hits = app()->makeWith(HitsIteratorAggregate::class,
['results' => $results,
'callback' => $builder->queryCallback,
]);

return new Collection($hits);
}
Expand All @@ -117,7 +120,7 @@ public function map(BaseBuilder $builder, $results, $model)
*/
public function getTotalCount($results)
{
return $results['hits']['total'];
return $results['hits']['total']['value'];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/Stages/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public function handle(Client $elasticsearch): void
$source = $this->source;
$params = GetAliasParams::anyIndex($source->searchableAs());
try {
$response = $elasticsearch->indices()->getAliases($params->toArray());
/** @var array $response */
$response = $elasticsearch->indices()->getAlias($params->toArray());
} catch (Missing404Exception $e) {
$response = [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(Client $elasticsearch): void
{
$source = $this->source;
$params = Get::anyIndex($source->searchableAs());
$response = $elasticsearch->indices()->getAliases($params->toArray());
$response = $elasticsearch->indices()->getAlias($params->toArray());

$params = new Update();
foreach ($response as $indexName => $alias) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FlushCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function test_clear_index(): void
];

$response = $this->elasticsearch->search($params);
$this->assertEquals(0, $response['hits']['total']);
$this->assertEquals(0, $response['hits']['total']['value']);
}
}
8 changes: 4 additions & 4 deletions tests/Feature/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function test_import_entites(): void
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals($productsAmount, $response['hits']['total']);
$this->assertEquals($productsAmount, $response['hits']['total']['value']);
}

public function test_import_entites_in_queue(): void
Expand All @@ -65,7 +65,7 @@ public function test_import_entites_in_queue(): void
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals($productsAmount, $response['hits']['total']);
$this->assertEquals($productsAmount, $response['hits']['total']['value']);
}

public function test_import_all_pages(): void
Expand All @@ -89,7 +89,7 @@ public function test_import_all_pages(): void
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals($productsAmount, $response['hits']['total']);
$this->assertEquals($productsAmount, $response['hits']['total']['value']);
}

public function test_import_with_custom_key_all_pages(): void
Expand All @@ -115,7 +115,7 @@ public function test_import_with_custom_key_all_pages(): void
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals($booksAmount, $response['hits']['total']);
$this->assertEquals($booksAmount, $response['hits']['total']['value']);
}

public function test_remove_old_index_after_switching_to_new(): void
Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Elasticsearch/HitsIteratorAggregateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Matchish\ScoutElasticSearch;

use App\Library\CustomHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\HitsIteratorAggregate;
use Tests\TestCase;

class HitsIteratorAggregateTest extends TestCase
{
public function test_hits_iterator_aggregate_binds_to_eloquent_implementation()
{
$iteratorAggregate = $this->app->makeWith(HitsIteratorAggregate::class, [
'results' => [],
'callback' => null,
]);

$this->assertEquals(EloquentHitsIteratorAggregate::class, get_class($iteratorAggregate));
}

public function test_override_bind_for_custom_iterator_aggregate_implementation()
{
$this->app->bind(HitsIteratorAggregate::class, CustomHitsIteratorAggregate::class);

$iteratorAggregate = $this->app->makeWith(HitsIteratorAggregate::class, [
'results' => [],
'callback' => null,
]);

$this->assertEquals(CustomHitsIteratorAggregate::class, get_class($iteratorAggregate));
}
}
6 changes: 3 additions & 3 deletions tests/Integration/Engines/ElasticSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_update()
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals($models->count(), $response['hits']['total']);
$this->assertEquals($models->count(), $response['hits']['total']['value']);
foreach ($response['hits']['hits'] as $doc) {
$this->assertEquals('Scout', $doc['_source']['title']);
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public function test_delete()
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals(1, $response['hits']['total']);
$this->assertEquals(1, $response['hits']['total']['value']);
foreach ($response['hits']['hits'] as $doc) {
$this->assertEquals($shouldBeNotDeleted->getScoutKey(), $doc['_id']);
}
Expand All @@ -115,7 +115,7 @@ public function test_flush()
],
];
$response = $this->elasticsearch->search($params);
$this->assertEquals(0, $response['hits']['total']);
$this->assertEquals(0, $response['hits']['total']['value']);
}

public function test_map_with_custom_key_name()
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Jobs/Stages/CreateWriteIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_create_write_index(): void
$elasticsearch = $this->app->make(Client::class);
$stage = new CreateWriteIndex(DefaultImportSourceFactory::from(Product::class), Index::fromSource(DefaultImportSourceFactory::from(Product::class)));
$stage->handle($elasticsearch);
$response = $elasticsearch->indices()->getAliases(['index' => '*', 'name' => 'products']);
$response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products']);
$this->assertTrue($this->containsWriteIndex($response, 'products'));
}

Expand Down
Loading

0 comments on commit 103129e

Please sign in to comment.