mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-23 18:03:17 +00:00
Wrap text and concept queries with a single filtered query
This commit is contained in:
@@ -39,8 +39,9 @@ class TextNode extends AbstractTermNode implements ContextAbleInterface
|
||||
public function buildQuery(QueryContext $context)
|
||||
{
|
||||
$query_builder = function (array $fields) use ($context) {
|
||||
// Full text
|
||||
$index_fields = [];
|
||||
foreach ($fields as $field) {
|
||||
foreach (Field::filterByValueCompatibility($fields, $this->text) as $field) {
|
||||
foreach ($context->localizeField($field) as $f) {
|
||||
$index_fields[] = $f;
|
||||
}
|
||||
@@ -48,7 +49,7 @@ class TextNode extends AbstractTermNode implements ContextAbleInterface
|
||||
if (!$index_fields) {
|
||||
return null;
|
||||
}
|
||||
return [
|
||||
$query = [
|
||||
'multi_match' => [
|
||||
'fields' => $index_fields,
|
||||
'query' => $this->text,
|
||||
@@ -56,31 +57,19 @@ class TextNode extends AbstractTermNode implements ContextAbleInterface
|
||||
'lenient' => true,
|
||||
]
|
||||
];
|
||||
};
|
||||
|
||||
$unrestricted_fields = $context->getUnrestrictedFields();
|
||||
$compatible_unrestricted_fields = Field::filterByValueCompatibility($unrestricted_fields, $this->text);
|
||||
$query = $query_builder($compatible_unrestricted_fields);
|
||||
|
||||
$private_fields = $context->getPrivateFields();
|
||||
$compatible_private_fields = Field::filterByValueCompatibility($private_fields, $this->text);
|
||||
foreach (QueryHelper::wrapPrivateFieldQueries($compatible_private_fields, $query_builder) as $private_field_query) {
|
||||
$query = QueryHelper::applyBooleanClause($query, 'should', $private_field_query);
|
||||
}
|
||||
|
||||
// Concepts handling
|
||||
$concept_queries = $this->buildConceptQueriesForFields($unrestricted_fields);
|
||||
foreach ($concept_queries as $concept_query) {
|
||||
$query = QueryHelper::applyBooleanClause($query, 'should', $concept_query);
|
||||
}
|
||||
$query_builder = function (array $fields) {
|
||||
// Thesaurus
|
||||
$concept_queries = $this->buildConceptQueriesForFields($fields);
|
||||
$query = null;
|
||||
foreach ($concept_queries as $concept_query) {
|
||||
$query = QueryHelper::applyBooleanClause($query, 'should', $concept_query);
|
||||
}
|
||||
return $query;
|
||||
};
|
||||
|
||||
// Unrestricted fields
|
||||
$query = $query_builder($context->getUnrestrictedFields());
|
||||
|
||||
// Private fields
|
||||
$private_fields = $context->getPrivateFields();
|
||||
foreach (QueryHelper::wrapPrivateFieldQueries($private_fields, $query_builder) as $private_field_query) {
|
||||
$query = QueryHelper::applyBooleanClause($query, 'should', $private_field_query);
|
||||
}
|
||||
|
@@ -192,22 +192,6 @@ class TextNodeTest extends \PHPUnit_Framework_TestCase
|
||||
"operator": "and",
|
||||
"lenient": true
|
||||
}
|
||||
}, {
|
||||
"filtered": {
|
||||
"filter": {
|
||||
"terms": {
|
||||
"base_id": [1, 2, 3]
|
||||
}
|
||||
},
|
||||
"query": {
|
||||
"multi_match": {
|
||||
"fields": ["private_caption.bar.fr", "private_caption.bar.en"],
|
||||
"query": "baz",
|
||||
"operator": "and",
|
||||
"lenient": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}, {
|
||||
"multi_match": {
|
||||
"fields": [
|
||||
@@ -223,9 +207,20 @@ class TextNodeTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
},
|
||||
"query": {
|
||||
"multi_match": {
|
||||
"fields": ["concept_path.bar"],
|
||||
"query": "/qux"
|
||||
"bool": {
|
||||
"should": [{
|
||||
"multi_match": {
|
||||
"fields": ["private_caption.bar.fr", "private_caption.bar.en"],
|
||||
"query": "baz",
|
||||
"operator": "and",
|
||||
"lenient": true
|
||||
}
|
||||
}, {
|
||||
"multi_match": {
|
||||
"fields": ["concept_path.bar"],
|
||||
"query": "/qux"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user