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

Overriding index setting configuration bug #921

Closed
wants to merge 14 commits into from
Closed
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ php:
- 7.4
env:
global:
- ES_VERSION=6.2.3 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}.tar.gz
- ES_VERSION=7.5.2 ES_DOWNLOAD_URL=https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz
matrix:
- SYMFONY="^4.4"
- SYMFONY="^5.0"
Expand All @@ -17,7 +17,7 @@ jobs:
env: SYMFONY="^5.0"
install:
- wget ${ES_DOWNLOAD_URL}
- tar -xzf elasticsearch-${ES_VERSION}.tar.gz
- tar -xzf elasticsearch-${ES_VERSION}-linux-x86_64.tar.gz
- ./elasticsearch-${ES_VERSION}/bin/elasticsearch -d
before_script:
- composer require --no-update symfony/framework-bundle:${SYMFONY}
Expand All @@ -28,4 +28,4 @@ script:
- vendor/bin/phpunit --coverage-clover=coverage.clover
- vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/,Tests/app/,var/cache ./
after_script:
- travis_retry php vendor/bin/coveralls
- travis_retry bash <(curl -s https://codecov.io/bash)
8 changes: 0 additions & 8 deletions Annotation/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,6 @@ final class Index extends AbstractAnnotation

public $numberOfReplicas = 1;

/**
* We strongly recommend to not use this parameter in the index annotation. By default it will be set as `_doc`
* type name. Eventually it will be removed.
*
* @deprecated will be removed in v7 since there will be no more types in the indexes.
*/
public $typeName = '_doc';

/**
* You can select one of your indexes to be default. Useful for cli commands when you don't
* need to define an alias name. If default is not set the first index found will be set as default one.
Expand Down
26 changes: 17 additions & 9 deletions DependencyInjection/Compiler/MappingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,23 @@ private function handleDirectoryMapping(ContainerBuilder $container, string $dir
$indexMetadata = $parser->getIndexMetadata($class);

if (!empty($indexMetadata)) {
$indexMetadata['settings'] = array_filter(array_merge_recursive(
$indexMetadata['settings'] ?? [],
[
'number_of_replicas' => $document->numberOfReplicas,
'number_of_shards' => $document->numberOfShards,
],
$indexesOverride[$namespace]['settings'] ?? []
));
$indexMetadata['settings'] = array_filter(
array_replace_recursive(
$indexMetadata['settings'] ?? [],
[
'number_of_replicas' => $document->numberOfReplicas,
'number_of_shards' => $document->numberOfShards,
],
$indexesOverride[$namespace]['settings'] ?? []
),
function ($value) {
if (0 === $value) {
return true;
}

return (bool)$value;
}
);

$indexSettings = new Definition(
IndexSettings::class,
Expand All @@ -93,7 +102,6 @@ private function handleDirectoryMapping(ContainerBuilder $container, string $dir
$indexMetadata,
$indexesOverride[$namespace]['hosts'] ?? $document->hosts,
$indexesOverride[$namespace]['default'] ?? $document->default,
$indexesOverride[$namespace]['type'] ?? $document->typeName
]
);

Expand Down
36 changes: 1 addition & 35 deletions Mapping/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ public function getIndexAnnotation(\ReflectionClass $class)
return $document;
}

/**
* @deprecated will be deleted in v7. Types are deleted from elasticsearch.
*/
public function getTypeName(\ReflectionClass $class): string
{
/** @var Index $document */
$document = $this->reader->getClassAnnotation($class, Index::class);

return $document->typeName ?? '_doc';
}

public function getIndexMetadata(\ReflectionClass $class): array
{
if ($class->isTrait()) {
Expand All @@ -102,34 +91,11 @@ public function getIndexMetadata(\ReflectionClass $class): array
return array_filter(array_map('array_filter', [
'settings' => $settings,
'mappings' => [
$this->getTypeName($class) => [
'properties' => array_filter($this->getClassMetadata($class))
]
'properties' => array_filter($this->getClassMetadata($class))
]
]));
}

public function getDocumentNamespace(string $indexAlias): ?string
{
if ($this->cache->contains(Configuration::ONGR_INDEXES)) {
$indexes = $this->cache->fetch(Configuration::ONGR_INDEXES);

if (isset($indexes[$indexAlias])) {
return $indexes[$indexAlias];
}
}

return null;
}

public function getParsedDocument(\ReflectionClass $class): Index
{
/** @var Index $document */
$document = $this->reader->getClassAnnotation($class, Index::class);

return $document;
}

private function getClassMetadata(\ReflectionClass $class): array
{
$mapping = [];
Expand Down
22 changes: 1 addition & 21 deletions Mapping/IndexSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,20 @@ class IndexSettings
private $hosts;
private $defaultIndex = false;



/**
* @deprecated will be removed in the v7
*/
private $type;

public function __construct(
string $namespace,
string $indexName,
string $alias,
array $indexMetadata = [],
array $hosts = [],
bool $defaultIndex = false,
$type = null
bool $defaultIndex = false
) {
$this->namespace = $namespace;
$this->indexName = $indexName;
$this->alias = $alias;
$this->indexMetadata = $indexMetadata;
$this->hosts = $hosts;
$this->defaultIndex = $defaultIndex;
$this->type = $type;
}

public function getNamespace()
Expand Down Expand Up @@ -109,15 +100,4 @@ public function setDefaultIndex(bool $defaultIndex): self
$this->defaultIndex = $defaultIndex;
return $this;
}

public function getType()
{
return $this->type;
}

public function setType($type): self
{
$this->type = $type;
return $this;
}
}
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ONGR Elasticsearch Bundle

[![Build Status](https://travis-ci.org/ongr-io/ElasticsearchBundle.svg?branch=master)](https://travis-ci.org/ongr-io/ElasticsearchBundle)
[![Latest Stable Version](https://poser.pugx.org/ongr/elasticsearch-bundle/v/stable)](https://packagist.org/packages/ongr/elasticsearch-bundle)
[![codecov](https://codecov.io/gh/ongr-io/ElasticsearchBundle/branch/master/graph/badge.svg)](https://codecov.io/gh/ongr-io/ElasticsearchBundle)
[![Total Downloads](https://poser.pugx.org/ongr/elasticsearch-bundle/downloads)](https://packagist.org/packages/ongr/elasticsearch-bundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchBundle/?branch=master)


Elasticsearch Bundle was created in order to serve the need for
professional [Elasticsearch][1] integration with enterprise level Symfony
applications. This bundle is:
Expand All @@ -21,18 +28,17 @@ Technical goodies:
If you need any help, [stack overflow][3] is the preferred way to get answers.
is the preferred and recommended way to ask questions about ONGR bundles and libraries.

If you like this library, help me to develop it by buying a cup of coffee

[![Build Status](https://travis-ci.org/ongr-io/ElasticsearchBundle.svg?branch=master)](https://travis-ci.org/ongr-io/ElasticsearchBundle)
[![Latest Stable Version](https://poser.pugx.org/ongr/elasticsearch-bundle/v/stable)](https://packagist.org/packages/ongr/elasticsearch-bundle)
[![Total Downloads](https://poser.pugx.org/ongr/elasticsearch-bundle/downloads)](https://packagist.org/packages/ongr/elasticsearch-bundle)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ongr-io/ElasticsearchBundle/?branch=master)
<a href="https://www.buymeacoffee.com/zIKBXRc" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>


## Version matrix

| Elasticsearch version | ElasticsearchBundle version |
| --------------------- | -------------------------------- |
| >= 6.0 | ~6.x |
| >= 7.0 | ~7.x |
| >= 6.0, < 7.0 | ~6.x |
| >= 5.0, < 5.0 | ~5.x, ~6.x (indexes with 1 type) |
| >= 2.0, < 5.0 | >=1.0, < 5.0 |
| >= 1.0, < 2.0 | >= 0.10, < 1.0 |
Expand Down
6 changes: 2 additions & 4 deletions Resources/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ So here's a simple example how the data looks like:

[
{"count":2,"date":"2015-10-25T14:46:21+0200"},
{"_type":"content","_id":"15","_source":{"id":"15","title":"About","content":"Sample ONGR about page..","urls":[{"url":"about\/","key":""}]}},
{"_type":"content","_id":"37","_source":{"id":"37","title":"Home Page","content":"<div class=\"jumbotron\">\r\n <h1>Welcome to ONGR demo site!<\/h1>\r\n <p>Enterprise E-commerce Accelerator.<\/p><\/div>","urls":[{"url":"home-page\/","key":""}]}}
{"_id":"15","_source":{"id":"15","title":"About","content":"Sample ONGR about page..","urls":[{"url":"about\/","key":""}]}},
{"_id":"37","_source":{"id":"37","title":"Home Page","content":"<div class=\"jumbotron\">\r\n <h1>Welcome to ONGR demo site!<\/h1>\r\n <p>Enterprise E-commerce Accelerator.<\/p><\/div>","urls":[{"url":"home-page\/","key":""}]}}
]

```

Every line of file is `JSON` object. First line must specify `count`, how many lines are in the files except first and the file timestamp in `date`.

There is one document per line. There could be different types defined in a single file, basically with a single file you can import the whole index. There are 3 required keys:
* `_type` which specifies elasticsearch type name (not an ElasticsearchBundle class document)
* `_id` is optional, if not specified (provided `null`) elasticsearch will create a random id for that document.
* `_source`: document array encoded to json object, where all fields are equal to the elasticsearch type field names.

Expand All @@ -78,7 +77,6 @@ Exports data from Elasticsearch index in a json format.
|:-----------:|:----------------------------:|:--------------------------------------------------------------------------------------:|
| `--manager` | *Manager name. e.g.* `default` | Used to select manager to create index for. If not specified, default manager is used. |
| `--chunk` | *Chunk size, default 500* | Specifies the size of each chunk to be received from Elasticsearch. This can be changed for performance reasons.
| `--types` | *Elasticsearch index type names* | Selected types to export, if no specified will export all index.
| `--split` | *Lines number* | This option indicates how many lines can be in single exported file.

> Index export generates the same `JSON` format as specified in the import chapter.
Expand Down
4 changes: 2 additions & 2 deletions Result/AbstractResultsIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function __construct(
if (isset($rawData['hits']['hits'])) {
$this->documents = $rawData['hits']['hits'];
}
if (isset($rawData['hits']['total'])) {
$this->count = $rawData['hits']['total'];
if (isset($rawData['hits']['total']['value'])) {
$this->count = $rawData['hits']['total']['value'];
}
}

Expand Down
4 changes: 2 additions & 2 deletions Service/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function exportIndex(
$counter = 0;
}

$doc = array_intersect_key($data, array_flip(['_id', '_type', '_source']));
$doc = array_intersect_key($data, array_flip(['_id', '_source']));
$writer->push($doc);
$progress->advance();
$counter++;
Expand All @@ -99,7 +99,7 @@ public function exportIndex(
*/
protected function getFilePath($filename): string
{
if ($filename{0} == '/' || strstr($filename, ':') !== false) {
if ($filename[0] == '/' || strstr($filename, ':') !== false) {
return $filename;
}

Expand Down
2 changes: 1 addition & 1 deletion Service/ImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function importIndex(
*/
protected function getFilePath($filename)
{
if ($filename{0} == '/' || strstr($filename, ':') !== false) {
if ($filename[0] == '/' || strstr($filename, ':') !== false) {
return $filename;
}

Expand Down
18 changes: 2 additions & 16 deletions Service/IndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ public function getNamespace(): string
return $this->namespace;
}

/**
* @deprecated will be removed in v7 since there will be no more types in the indexes.
*/
public function getTypeName(): string
{
return $this->indexSettings->getType();
}

public function getIndexSettings()
{
return $this->indexSettings;
Expand Down Expand Up @@ -189,7 +181,6 @@ public function find($id, $params = [])
{
$requestParams = [
'index' => $this->getIndexName(),
'type' => $this->getTypeName(),
'id' => $id,
];

Expand Down Expand Up @@ -319,7 +310,6 @@ public function getIndexDocumentCount(): int
{
$body = [
'index' => $this->getIndexName(),
'type' => $this->getTypeName(),
'body' => [],
];

Expand All @@ -332,7 +322,6 @@ public function remove($id, $routing = null)
{
$params = [
'index' => $this->getIndexName(),
'type' => $this->getTypeName(),
'id' => $id,
];

Expand All @@ -358,7 +347,6 @@ public function update($id, array $fields = [], $script = null, array $params =
[
'id' => $id,
'index' => $this->getIndexName(),
'type' => $this->getTypeName(),
'body' => $body,
],
$params
Expand All @@ -371,7 +359,6 @@ public function search(array $query, array $params = []): array
{
$requestParams = [
'index' => $this->getIndexName(),
'type' => $this->getTypeName(),
'body' => $query,
];

Expand All @@ -397,11 +384,10 @@ public function search(array $query, array $params = []): array
public function bulk(string $operation, array $data = [], $autoCommit = true): array
{
$bulkParams = [
'_type' => $this->getTypeName(),
'_id' => $data['_id'] ?? null,
];

unset($data['_index'], $data['_type'], $data['_id']);
unset($data['_index'], $data['_id']);

$this->eventDispatcher->dispatch(
new BulkEvent($operation, $bulkParams, $data),
Expand Down Expand Up @@ -439,7 +425,7 @@ public function persist($document): void
$this->bulk('index', $documentArray);
}

public function commit($commitMode = 'flush', array $params = []): array
public function commit($commitMode = 'refresh', array $params = []): array
{
$bulkResponse = [];
if (!empty($this->bulkQueries)) {
Expand Down
3 changes: 1 addition & 2 deletions Service/Json/JsonReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ protected function readLine()
protected function configureResolver(OptionsResolver $resolver)
{
$resolver
->setRequired(['_id', '_type', '_source'])
->setRequired(['_id', '_source'])
->setDefaults(['_score' => null, 'fields' => []])
->addAllowedTypes('_id', ['integer', 'string'])
->addAllowedTypes('_type', 'string')
->addAllowedTypes('_source', 'array')
->addAllowedTypes('fields', 'array');
}
Expand Down
6 changes: 0 additions & 6 deletions Tests/Functional/Annotation/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

class DocumentTest extends AbstractElasticsearchTestCase
{
public function testDocumentTypeName()
{
$index = $this->getIndex(DummyDocumentInTheEntityDirectory::class, false);
$this->assertEquals('_doc', $index->getTypeName());
}

public function testDocumentIndexName()
{
$index = $this->getIndex(DummyDocument::class, false);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Annotation/PropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testPropertyTypeName()
],
'type' => 'text',
],
$meta['mappings']['_doc']['properties']['title']
$meta['mappings']['properties']['title']
);
}
}
Loading