Fix code style on latest query parser feature & add tests

This commit is contained in:
Mathieu Darse
2015-07-02 19:06:44 +02:00
parent 6e52421437
commit 1d0257554b
7 changed files with 52 additions and 51 deletions

View File

@@ -27,9 +27,9 @@
%token except [Ee][Xx][Cc][Ee][Pp][Tt]|[Ss][Aa][Uu][Ff] %token except [Ee][Xx][Cc][Ee][Pp][Tt]|[Ss][Aa][Uu][Ff]
// Rest // Rest
%token database database
%token collection collection %token collection collection
%token recordid recordid %token id id|recordid
%token base base
%token word [^\s()\[\]:<>≤≥=]+ %token word [^\s()\[\]:<>≤≥=]+
// relative order of precedence is NOT > XOR > AND > OR // relative order of precedence is NOT > XOR > AND > OR
@@ -50,12 +50,12 @@ ternary:
quaternary() ( ::space:: ::and:: ::space:: primary() #and )? quaternary() ( ::space:: ::and:: ::space:: primary() #and )?
// Collection / recordid / base matcher // Collection / database / record id matcher
quaternary: quaternary:
::collection:: ::colon:: string() #collection ::database:: ::colon:: string() #database
| ::recordid:: ::colon:: string() #recordid | ::collection:: ::colon:: string() #collection
| ::base:: ::colon:: string() #base | ::id:: ::colon:: string() #id
| quinary() | quinary()
@@ -134,9 +134,9 @@ keyword:
| <except> | <except>
| <and> | <and>
| <or> | <or>
| <database>
| <collection> | <collection>
| <recordid> | <id>
| <base>
symbol: symbol:
<parenthese_> <parenthese_>

View File

@@ -4,19 +4,19 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext; use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
class BaseExpression extends Node class DatabaseExpression extends Node
{ {
private $baseName; private $database;
public function __construct($baseName) public function __construct($database)
{ {
$this->baseName = $baseName; $this->database = $database;
} }
public function buildQuery(QueryContext $context) public function buildQuery(QueryContext $context)
{ {
$query = array(); $query = array();
$query['term']['databox_name'] = $this->baseName; $query['term']['databox_name'] = $this->database;
return $query; return $query;
} }
@@ -28,6 +28,6 @@ class BaseExpression extends Node
public function __toString() public function __toString()
{ {
return sprintf('<base:%s>', $this->baseName); return sprintf('<database:%s>', $this->database);
} }
} }

View File

@@ -4,19 +4,19 @@ namespace Alchemy\Phrasea\SearchEngine\Elastic\AST;
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext; use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
class RecordidExpression extends Node class RecordIdentifierExpression extends Node
{ {
private $recordid; private $record_id;
public function __construct($recordid) public function __construct($record_id)
{ {
$this->recordid = $recordid; $this->record_id = $record_id;
} }
public function buildQuery(QueryContext $context) public function buildQuery(QueryContext $context)
{ {
$query = array(); $query = array();
$query['term']['record_id'] = $this->recordid; $query['term']['record_id'] = $this->record_id;
return $query; return $query;
} }
@@ -28,6 +28,6 @@ class RecordidExpression extends Node
public function __toString() public function __toString()
{ {
return sprintf('<recordid:%s>', $this->recordid); return sprintf('<record_identifier:%s>', $this->record_id);
} }
} }

View File

@@ -71,16 +71,12 @@ class FacetsResponse implements JsonSerializable
{ {
switch($name) { switch($name) {
case 'Collection': case 'Collection':
$r = sprintf('collection:%s', $this->escaper->escapeWord($value)); return sprintf('collection:%s', $this->escaper->escapeWord($value));
break; case 'Base':
case "Base": return sprintf('database:%s', $this->escaper->escapeWord($value));
$r = sprintf('base:%s', $this->escaper->escapeWord($value));
break;
default: default:
$r = sprintf('r"%s" IN %s', $this->escaper->escapeRaw($value), $name); return sprintf('r"%s" IN %s', $this->escaper->escapeRaw($value), $name);
break;
} }
return $r;
} }
private function throwAggregationResponseError() private function throwAggregationResponseError()

View File

@@ -22,8 +22,8 @@ class NodeTypes
const TEXT = '#text'; const TEXT = '#text';
const CONTEXT = '#context'; const CONTEXT = '#context';
const COLLECTION = '#collection'; const COLLECTION = '#collection';
const RECORDID = '#recordid'; const DATABASE = '#database';
const BASE = '#base'; const IDENTIFIER = '#id';
// Token types for leaf nodes // Token types for leaf nodes
const TOKEN_WORD = 'word'; const TOKEN_WORD = 'word';
const TOKEN_QUOTED_STRING = 'quoted'; const TOKEN_QUOTED_STRING = 'quoted';

View File

@@ -82,14 +82,14 @@ class QueryVisitor implements Visit
case NodeTypes::FIELD: case NodeTypes::FIELD:
return new AST\Field($this->visitString($element)); return new AST\Field($this->visitString($element));
case NodeTypes::DATABASE:
return $this->visitDatabaseNode($element);
case NodeTypes::COLLECTION: case NodeTypes::COLLECTION:
return $this->visitCollectionNode($element); return $this->visitCollectionNode($element);
case NodeTypes::RECORDID: case NodeTypes::IDENTIFIER:
return $this->visitRecordidNode($element); return $this->visitIdentifierNode($element);
case NodeTypes::BASE:
return $this->visitBaseNode($element);
default: default:
throw new \Exception(sprintf('Unknown node type "%s".', $element->getId())); throw new \Exception(sprintf('Unknown node type "%s".', $element->getId()));
@@ -259,6 +259,16 @@ class QueryVisitor implements Visit
return implode($tokens); return implode($tokens);
} }
private function visitDatabaseNode(Element $element)
{
if ($element->getChildrenNumber() !== 1) {
throw new \Exception('Base filter can only have a single child.');
}
$baseName = $element->getChild(0)->getValue()['value'];
return new AST\DatabaseExpression($baseName);
}
private function visitCollectionNode(Element $element) private function visitCollectionNode(Element $element)
{ {
if ($element->getChildrenNumber() !== 1) { if ($element->getChildrenNumber() !== 1) {
@@ -269,23 +279,13 @@ class QueryVisitor implements Visit
return new AST\CollectionExpression($collectionName); return new AST\CollectionExpression($collectionName);
} }
private function visitRecordidNode(Element $element) private function visitIdentifierNode(Element $element)
{ {
if ($element->getChildrenNumber() !== 1) { if ($element->getChildrenNumber() !== 1) {
throw new \Exception('Recordid filter can only have a single child.'); throw new \Exception('Identifier filter can only have a single child.');
} }
$recordid = $element->getChild(0)->getValue()['value']; $identifier = $element->getChild(0)->getValue()['value'];
return new AST\RecordidExpression($recordid); return new AST\RecordIdentifierExpression($identifier);
}
private function visitBaseNode(Element $element)
{
if ($element->getChildrenNumber() !== 1) {
throw new \Exception('Base filter can only have a single child.');
}
$baseName = $element->getChild(0)->getValue()['value'];
return new AST\BaseExpression($baseName);
} }
} }

View File

@@ -70,8 +70,13 @@ fooINbar|<text:"fooINbar">
collection:foo|<collection:foo> collection:foo|<collection:foo>
collection:foo AND bar|(<collection:foo> AND <text:"bar">) collection:foo AND bar|(<collection:foo> AND <text:"bar">)
collection:foo bar|<text:"collection:foo bar"> collection:foo bar|<text:"collection:foo bar">
recordid:90|<recordid:90> database:foo|<database:foo>
base:foo|<base:foo> database:foo AND bar|(<database:foo> AND <text:"bar">)
database:foo bar|<text:"database:foo bar">
id:90|<record_identifier:90>
id:90 AND foo|(<record_identifier:90> AND <text:"foo">)
id:90 foo|<text:"id:90 foo">
recordid:90|<record_identifier:90>
# Matcher on unknown name --> fulltext # Matcher on unknown name --> fulltext
foo:bar|<text:"foo:bar"> foo:bar|<text:"foo:bar">
Can't render this file because it contains an unexpected character in line 1 and column 11.