From 78ab5b31dc18c566ffc8f939d4c9bb20f1c84ce5 Mon Sep 17 00:00:00 2001 From: Mathieu Darse Date: Thu, 16 Jul 2015 19:06:46 +0200 Subject: [PATCH] Fix search when narrowed to private fields only --- .../SearchEngine/Elastic/AST/QuotedTextNode.php | 4 +++- .../Phrasea/SearchEngine/Elastic/AST/RawNode.php | 4 +++- .../Phrasea/SearchEngine/Elastic/AST/TextNode.php | 4 +++- .../SearchEngine/Elastic/Search/QueryHelper.php | 10 +++++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/QuotedTextNode.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/QuotedTextNode.php index 02a8eca564..2be81d11e9 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/QuotedTextNode.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/QuotedTextNode.php @@ -26,7 +26,9 @@ class QuotedTextNode extends Node ]; }; - $query = $query_builder($context->getLocalizedFields()); + $fields = $context->getLocalizedFields(); + $query = $fields ? $query_builder($fields) : null; + foreach (QueryHelper::buildPrivateFieldQueries($context, $query_builder) as $private_field_query) { $query = QueryHelper::applyBooleanClause($query, 'should', $private_field_query); } diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/RawNode.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/RawNode.php index 0555a7b5e2..be5b23a00a 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/RawNode.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/RawNode.php @@ -41,7 +41,9 @@ class RawNode extends Node return $query; }; - $query = $query_builder($context->getRawFields()); + $fields = $context->getRawFields(); + $query = count($fields) ? $query_builder($fields) : null; + foreach (QueryHelper::buildPrivateFieldQueries($context, $query_builder) as $private_field_query) { $query = QueryHelper::applyBooleanClause($query, 'should', $private_field_query); } diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TextNode.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TextNode.php index 55ec90cfaa..d282560810 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TextNode.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/AST/TextNode.php @@ -47,7 +47,9 @@ class TextNode extends AbstractTermNode implements ContextAbleInterface ]; }; - $query = $query_builder($context->getLocalizedFields()); + $fields = $context->getLocalizedFields(); + $query = count($fields) ? $query_builder($fields) : null; + foreach (QueryHelper::buildPrivateFieldQueries($context, $query_builder) as $private_field_query) { $query = QueryHelper::applyBooleanClause($query, 'should', $private_field_query); } diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryHelper.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryHelper.php index 2c83239fc8..3ad5cf825c 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryHelper.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryHelper.php @@ -86,12 +86,20 @@ class QueryHelper * @param array $sub_query Clause query * @return array Resulting query */ - public static function applyBooleanClause(array $query, $type, array $clause) + public static function applyBooleanClause($query, $type, array $clause) { if (!in_array($type, ['must', 'should'])) { throw new \InvalidArgumentException(sprintf('Type must be either "must" or "should", "%s" given', $type)); } + if ($query === null) { + return $clause; + } + + if (!is_array($query)) { + throw new \InvalidArgumentException(sprintf('Query must be either an array or null, "%s" given', gettype($query))); + } + if (!isset($query['bool'])) { // Wrap in a boolean query $bool = [];