Convert dates to date ranges on timestamp keys

This commit is contained in:
Mathieu Darse
2015-11-13 19:28:59 +01:00
parent 0f0c007e19
commit 6913d67e9a
3 changed files with 71 additions and 12 deletions

View File

@@ -4,6 +4,7 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\SearchEngine\Elastic\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\Exception;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryHelper;
use Hoa\Compiler\Llk\TreeNode;
use Hoa\Visitor\Element;
use Hoa\Visitor\Visit;
@@ -176,10 +177,29 @@ class QueryVisitor implements Visit
private function visitEqualNode(TreeNode $node)
{
return $this->handleBinaryExpression($node, function($left, $right) {
if ($this->isDateKey($left)) {
try {
// Try to create a range for incomplete dates
$range = QueryHelper::getRangeFromDateString($right);
return new AST\KeyValue\RangeExpression(
$left,
$range['from'], true,
$range['to'], false
);
} catch (\InvalidArgumentException $e) {
// Fall back to equal expression
}
}
return new AST\KeyValue\EqualExpression($left, $right);
});
}
private function isDateKey(AST\KeyValue\Key $key)
{
return $key instanceof AST\KeyValue\TimestampKey;
}
private function visitTerm(Element $element)
{
$words = array();