mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 06:53:15 +00:00

Removes wrapPrivateFieldConceptQueries(). The goal is to wrap text and concept queries with a single call to wrapPrivateFieldQueries().
110 lines
2.9 KiB
PHP
110 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Alchemy\Phrasea\SearchEngine\Elastic\AST;
|
|
|
|
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryContext;
|
|
use Alchemy\Phrasea\SearchEngine\Elastic\Search\QueryHelper;
|
|
use Alchemy\Phrasea\SearchEngine\Elastic\Structure\Field;
|
|
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\Concept;
|
|
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus\TermInterface;
|
|
|
|
abstract class AbstractTermNode extends Node implements TermInterface
|
|
{
|
|
protected $text;
|
|
protected $context;
|
|
private $concepts = [];
|
|
private $pruned_concepts;
|
|
|
|
public function __construct($text, Context $context = null)
|
|
{
|
|
$this->text = $text;
|
|
$this->context = $context;
|
|
}
|
|
|
|
public function setConcepts(array $concepts)
|
|
{
|
|
$this->pruned_concepts = null;
|
|
$this->concepts = $concepts;
|
|
}
|
|
|
|
private function getPrunedConcepts()
|
|
{
|
|
if ($this->pruned_concepts === null) {
|
|
$this->pruned_concepts = Concept::pruneNarrowConcepts($this->concepts);
|
|
}
|
|
return $this->pruned_concepts;
|
|
}
|
|
|
|
protected function buildConceptQueries(QueryContext $context)
|
|
{
|
|
if (!$this->getPrunedConcepts()) {
|
|
return [];
|
|
}
|
|
|
|
$query_builder = function (array $fields) {
|
|
$concept_queries = $this->buildConceptQueriesForFields($fields);
|
|
$query = null;
|
|
foreach ($concept_queries as $concept_query) {
|
|
$query = QueryHelper::applyBooleanClause($query, 'should', $concept_query);
|
|
}
|
|
return $query;
|
|
};
|
|
|
|
$queries = $this->buildConceptQueriesForFields($context->getUnrestrictedFields());
|
|
|
|
$private_fields = $context->getPrivateFields();
|
|
foreach (QueryHelper::wrapPrivateFieldQueries($private_fields, $query_builder) as $private_field_query) {
|
|
$queries[] = $private_field_query;
|
|
}
|
|
|
|
return $queries;
|
|
}
|
|
|
|
protected function buildConceptQueriesForFields(array $fields)
|
|
{
|
|
$concepts = $this->getPrunedConcepts();
|
|
if (!$concepts) {
|
|
return [];
|
|
}
|
|
|
|
$index_fields = [];
|
|
foreach ($fields as $field) {
|
|
$index_fields[] = $field->getConceptPathIndexField();
|
|
}
|
|
if (!$index_fields) {
|
|
return [];
|
|
}
|
|
|
|
$queries = [];
|
|
foreach ($concepts as $concept) {
|
|
$queries[] = [
|
|
'multi_match' => [
|
|
'fields' => $index_fields,
|
|
'query' => $concept->getPath()
|
|
]
|
|
];
|
|
}
|
|
return $queries;
|
|
}
|
|
|
|
public function getValue()
|
|
{
|
|
return $this->text;
|
|
}
|
|
|
|
public function hasContext()
|
|
{
|
|
return $this->context !== null;
|
|
}
|
|
|
|
public function getContext()
|
|
{
|
|
return $this->context->getValue();
|
|
}
|
|
|
|
public function getTermNodes()
|
|
{
|
|
return [$this];
|
|
}
|
|
}
|