Files
Phraseanet/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php
jygaulier a70bf2fc70 PHRAS-1304_AUTO-COMPLETION_MASTER (#2061)
* PHRAS-1304_AUTO-COMPLETION_MASTER
ported from 4.0

* PHRAS-1304_AUTO-COMPLETION_MASTER
fix

* PHRAS-1304_AUTO-COMPLETION_MASTER
fix

* PHRAS-1304_AUTO-COMPLETION_MASTER
bump php version to 5.5.31 (5.5.21 is obsolete in cicleci)

* PHRAS-1304_AUTO-COMPLETION_MASTER
bump php version to 5.5.31 : php.ini moved in circelci

* PHRAS-1304_AUTO-COMPLETION_MASTER
add zmq & date to php for circleci

* PHRAS-1304_AUTO-COMPLETION_MASTER
add zmq

* PHRAS-1304_AUTO-COMPLETION_MASTER
bump amqp

* PHRAS-1304_AUTO-COMPLETION_MASTER
downgrade amqp to 1.2 to test compilation against old librabbit 0.4 (ubuntu)

* PHRAS-1304_AUTO-COMPLETION_MASTER
add amqp.so to php.ini, (re)bump amqp to 1.6

* PHRAS-1304_AUTO-COMPLETION_MASTER
build rabittmq from git

* PHRAS-1304_AUTO-COMPLETION_MASTER
build rabittmq from git again

* PHRAS-1304_AUTO-COMPLETION_MASTER
build rabittmq from git again and again

* PHRAS-1304_AUTO-COMPLETION_MASTER
fix test on media rotation 600*400 -> 400*599 !!!

* PHRAS-1304_AUTO-COMPLETION_MASTER
restore facebook sdk to 4.0.1 due to mistake

* PHRAS-1304_AUTO-COMPLETION_MASTER
deleted unwanted file
2017-02-06 18:26:56 +01:00

138 lines
3.6 KiB
PHP

<?php
namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\QueryException;
use Alchemy\Phrasea\SearchEngine\Elastic\FieldMapping;
use Alchemy\Phrasea\SearchEngine\Elastic\Mapping;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\Field as ASTField;
use Alchemy\Phrasea\SearchEngine\Elastic\AST\Flag;
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Structure;
/**
* @todo Check for private fields and only search on them if allowed
*/
class QueryContext
{
/** @var Structure */
private $structure;
/** @var array */
private $locales;
/** @var string */
private $queryLocale;
/** @var array */
private $fields;
public function __construct(Structure $structure, array $locales, $queryLocale, array $fields = null)
{
$this->structure = $structure;
$this->locales = $locales;
$this->queryLocale = $queryLocale;
$this->fields = $fields;
}
public function narrowToFields(array $fields)
{
if (is_array($this->fields)) {
// Ensure we are not escaping from original fields restrictions
$fields = array_intersect($this->fields, $fields);
if (!$fields) {
throw new QueryException('Query narrowed to non available fields');
}
}
return new static($this->structure, $this->locales, $this->queryLocale, $fields);
}
/**
* @return Field[]
*/
public function getUnrestrictedFields()
{
// TODO Restore search optimization by using "caption_all" field
// (only when $this->fields is null)
return $this->filterFields($this->structure->getUnrestrictedFields());
}
public function getPrivateFields()
{
return $this->filterFields($this->structure->getPrivateFields());
}
public function getHighlightedFields()
{
return $this->filterFields($this->structure->getAllFields());
}
/**
* @param Field[] $fields
* @return Field[]
*/
private function filterFields(array $fields)
{
if ($this->fields !== null) {
$fields = array_intersect_key($fields, array_flip($this->fields));
}
return array_values($fields);
}
public function get($name)
{
if ($name instanceof ASTField) {
$name = $name->getValue();
}
return $this->structure->get($name);
}
public function getFlag($name)
{
if ($name instanceof Flag) {
$name = $name->getName();
}
return $this->structure->getFlagByName($name);
}
public function getMetadataTag($name)
{
return $this->structure->getMetadataTagByName($name);
}
/**
* @todo Maybe we should put this logic in Field class?
*/
public function localizeField(Field $field)
{
$index_field = $field->getIndexField();
if ($field->getType() === FieldMapping::TYPE_STRING) {
return $this->localizeFieldName($index_field);
} else {
return [$index_field];
}
}
private function localizeFieldName($field)
{
$fields = array();
foreach ($this->locales as $locale) {
$boost = ($locale === $this->queryLocale) ? '^5' : '';
$fields[] = sprintf('%s.%s%s', $field, $locale, $boost);
}
// TODO Put generic analyzers on main field instead of "light" sub-field
$fields[] = sprintf('%s.light^10', $field);
return $fields;
}
public function getFields()
{
return $this->fields;
}
}