From 605c943af5cb39cf95fed87acc74cdb797d596a0 Mon Sep 17 00:00:00 2001 From: Mathieu Darse Date: Mon, 15 Jun 2015 16:13:44 +0200 Subject: [PATCH 1/2] Fix IN search regression from 40e2df6c3f8653ff482f3cd8165cfbea9461f7e5 --- .../Elastic/Search/QueryContext.php | 2 +- .../SearchEngine/Search/QueryContextTest.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php index ffeb19c924..a53dc9a570 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php @@ -34,7 +34,7 @@ class QueryContext } } - return new static($this->locales, $this->queryLocale, $fields); + return new static($this->structure, $this->locales, $this->queryLocale, $fields); } public function getRawFields() diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php new file mode 100644 index 0000000000..63d9bec908 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php @@ -0,0 +1,18 @@ +prophesize(Structure::class)->reveal(); + $available_locales = ['ab', 'cd', 'ef']; + $context = new QueryContext($structure, $available_locales, 'fr'); + $narrowed = $context->narrowToFields(['some_field']); + $this->assertEquals(['some_field'], $narrowed->getFields()); + } +} From ea8eb4dc074beb8eaceae30dfa6037cdf61589ca Mon Sep 17 00:00:00 2001 From: Mathieu Darse Date: Mon, 15 Jun 2015 16:13:44 +0200 Subject: [PATCH 2/2] Fix another IN search regression from 40e2df6c3f8653ff482f3cd8165cfbea9461f7e5 Move getIndexFieldName() from `RecordHelper` to `Field`. Also remove an unused injected dependency --- .../Core/Provider/SearchEngineServiceProvider.php | 1 - .../SearchEngine/Elastic/ElasticSearchEngine.php | 6 ++---- .../Phrasea/SearchEngine/Elastic/RecordHelper.php | 9 --------- .../SearchEngine/Elastic/Search/QueryContext.php | 2 +- .../SearchEngine/Elastic/Structure/Field.php | 9 +++++++++ .../SearchEngine/Search/QueryContextTest.php | 15 +++++++++++++++ 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/Alchemy/Phrasea/Core/Provider/SearchEngineServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/SearchEngineServiceProvider.php index 3b4e271561..4216ce1b6b 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/SearchEngineServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/SearchEngineServiceProvider.php @@ -59,7 +59,6 @@ class SearchEngineServiceProvider implements ServiceProviderInterface $app['elasticsearch.client'], $app['elasticsearch.options']['index'], $app['locales.available'], - $app['elasticsearch.record_helper'], $app['elasticsearch.facets_response.factory'] ); }); diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php index b8dbdd383c..895a96b4b5 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticSearchEngine.php @@ -41,15 +41,13 @@ class ElasticSearchEngine implements SearchEngineInterface private $indexName; private $configurationPanel; private $locales; - private $recordHelper; - public function __construct(Application $app, Structure $structure, Client $client, $indexName, array $locales, RecordHelper $recordHelper, Closure $facetsResponseFactory) + public function __construct(Application $app, Structure $structure, Client $client, $indexName, array $locales, Closure $facetsResponseFactory) { $this->app = $app; $this->structure = $structure; $this->client = $client; $this->locales = array_keys($locales); - $this->recordHelper = $recordHelper; $this->facetsResponseFactory = $facetsResponseFactory; if ('' === trim($indexName)) { @@ -384,7 +382,7 @@ class ElasticSearchEngine implements SearchEngineInterface // 2015-05-26 (mdarse) Removed databox filtering. // It was already done by the ACL filter in the query scope, so no // document that shouldn't be displayed can go this far. - $field_name = RecordHelper::getIndexFieldName($field); + $field_name = $field->getIndexFieldName(); $aggs[$name]['terms']['field'] = sprintf('%s.raw', $field_name); } diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/RecordHelper.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/RecordHelper.php index 7e4af313ce..3f26050870 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/RecordHelper.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/RecordHelper.php @@ -85,13 +85,4 @@ class RecordHelper return null; } } - - public static function getIndexFieldName(Field $field) - { - return sprintf( - '%scaption.%s', - $field->isPrivate() ? 'private_' : '', - $field->getName() - ); - } } diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php index a53dc9a570..64c136bb52 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php @@ -94,7 +94,7 @@ class QueryContext return; } // TODO Field label dereferencing (we only want names) - return RecordIndexer::getIndexFieldName($field); + return $field->getIndexFieldName(); } public function getFields() diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php index 4de0385a16..85198d8788 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php @@ -73,6 +73,15 @@ class Field return $this->name; } + public function getIndexFieldName() + { + return sprintf( + '%scaption.%s', + $this->is_private ? 'private_' : '', + $this->name + ); + } + public function getType() { return $this->type; diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php index 63d9bec908..9740f30052 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/Search/QueryContextTest.php @@ -2,7 +2,9 @@ namespace Alchemy\Tests\Phrasea\SearchEngine\Search; +use Alchemy\Phrasea\SearchEngine\Elastic\Mapping; use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext; +use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field; use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure; class QueryContextTest extends \PHPUnit_Framework_TestCase @@ -15,4 +17,17 @@ class QueryContextTest extends \PHPUnit_Framework_TestCase $narrowed = $context->narrowToFields(['some_field']); $this->assertEquals(['some_field'], $narrowed->getFields()); } + + public function testFieldNormalization() + { + $public_field = new Field('foo', Mapping::TYPE_STRING, true, false); + $restricted_field = new Field('bar', Mapping::TYPE_STRING, true, true); + $structure = $this->prophesize(Structure::class); + $structure->get('foo')->willReturn($public_field); + $structure->get('bar')->willReturn($restricted_field); + + $context = new QueryContext($structure->reveal(), [], 'fr'); + $this->assertEquals('caption.foo', $context->normalizeField('foo')); + $this->assertEquals('private_caption.bar', $context->normalizeField('bar')); + } }