Files
Phraseanet/lib/Alchemy/Phrasea/SearchEngine/Elastic/Search/QueryContext.php
Mathieu Darse 771aa5b765 Working cross-fields queries with multiple words (without operators)
- Index the full content of a record in a (private_)content_all field
- Handle all fields wide search as a special-case (drastically simplify queries)
- QueryContext doesn't take all allowed fields anymore, but whether private
fields are allowed or not. Since private fields are namespaced, field level
restriction is not needed anymore.
2015-03-24 17:52:30 +01:00

68 lines
1.8 KiB
PHP

<?php
namespace Alchemy\Phrasea\SearchEngine\Elastic\Search;
use Alchemy\Phrasea\SearchEngine\Elastic\Exception\QueryException;
class QueryContext
{
private $locales;
private $queryLocale;
private $fields;
public function __construct(array $locales, $queryLocale, array $fields = null)
{
$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');
}
} else {
$fields = null;
}
return new static($this->locales, $this->queryLocale, $fields);
}
public function getLocalizedFields()
{
// TODO Private fields handling
if ($this->fields === null) {
return $this->localizeField('caption_all');
}
$fields = array();
foreach ($this->fields as $field) {
foreach ($this->localizeField(sprintf('caption.%s', $field)) as $fields[]);
}
return $fields;
}
private function localizeField($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;
}
}