Oihana PHP Arango

HasFacetJoinAggregate uses trait:short

Builds the AQL filter fragment for a {@see \oihana\arango\models\enums\Facet::JOIN_AGGREGATE} facet: the key-join counterpart of {@see HasFacetEdgeAggregate} and the aggregate counterpart of {@see HasFacetJoin}. Instead of testing for the mere existence of a joined document, it aggregates a numeric field over ALL joined documents and compares the result to a threshold — `AGG(FOR doc_<key> IN <collection> FILTER doc_<key>.<KEY> == doc.<PROPERTY> RETURN doc_<key>.<field>) <op> @<key>_0`.

The join itself is doc_join.<KEY> == doc.<PROPERTY> (or IN when AQL::ARRAY is set), with AQL::KEY the joined side (default _key) and Facet::PROPERTY the main side (default the facet key). The aggregation logic is shared with HasFacetEdgeAggregate through HasFacetAggregateConditions.

Tags
see
FacetTrait::prepareFacets()

The dispatcher that invokes this builder.

HasFacetAggregateConditions

The shared aggregation logic.

Table of Contents

Methods

prepareAggregateConditions()  : string
Builds an aggregate facet expression.
prepareFacetJoinAggregate()  : string
Prepares a join aggregate facet.

Methods

prepareAggregateConditions()

Builds an aggregate facet expression.

protected prepareAggregateConditions(mixed $value, array<string|int, mixed> $facet, string $forSource, string|null $prefix, string $docRef, string $key, array<string|int, mixed> &$binds) : string

Resolves the aggregator, field, comparator and threshold (URL overriding the definition), then emits AGG(FOR … [FILTER prefix] RETURN related.field) <comparator> @<key>_0.

Parameters
$value : mixed

The facet value: a scalar threshold, or an {agg, field, op, val} object.

$facet : array<string|int, mixed>

The facet definition (Facet::AGG, AQL::FIELDS, Facet::OP).

$forSource : string

The compiled FOR … source (traversal or collection).

$prefix : string|null

An extra condition AND-ed inside the FILTER (e.g. a join match), or null.

$docRef : string

The related-document variable (e.g. doc_comments).

$key : string

The facet key, used to namespace the bind name.

$binds : array<string|int, mixed>

The bind variables, populated by reference.

Tags
throws
BindException
ValidationException

When the aggregator is unknown or a non-count aggregate has no valid field.

Return values
string

The AQL fragment, or an empty string when no threshold is supplied.

prepareFacetJoinAggregate()

Prepares a join aggregate facet.

protected prepareFacetJoinAggregate(string $key, mixed $value, array<string|int, mixed> &$binds, array<string|int, mixed> $facet, string $doc) : string
Parameters
$key : string

The facet key (also the default main-side join property).

$value : mixed

A scalar threshold, or an {agg, field, op, val} object.

$binds : array<string|int, mixed>

The bind variables, populated by reference.

$facet : array<string|int, mixed>

The facet definition (AQL::COLLECTION, AQL::KEY, Facet::PROPERTY, AQL::ARRAY, Facet::AGG, AQL::FIELDS, Facet::OP).

$doc : string

The main document reference.

Tags
throws
BindException
ValidationException
example

Average comment score per article — keep articles rated 4 or more on average :

Arango::FACETS =>
[
    'comments' =>
    [
        Facet::TYPE     => Facet::JOIN_AGGREGATE ,
        AQL::COLLECTION => 'comments' ,  // joined collection
        AQL::KEY        => 'articleId' , // joined side  (default _key)
        Facet::PROPERTY => '_key' ,      // main side    (default the facet key)
        Facet::AGG      => 'avg' ,       // default aggregator
        AQL::FIELDS     => 'score' ,     // default aggregated field
        Facet::OP       => 'ge'          // default threshold comparator
    ]
]
?facets={"comments":{"agg":"avg","field":"score","op":"ge","val":4}}
// AVERAGE(FOR doc_comments IN comments FILTER doc_comments.articleId == doc._key RETURN doc_comments.score) >= @comments_0

?facets={"comments":{"agg":"count","val":3}}      // at least 3 comments
?facets={"comments":{"agg":"min","field":"score","val":3}}  // worst score still >= 3
?facets={"comments":{"agg":"count","op":"lt","val":2}}      // lightly commented (fewer than 2)
Return values
string
On this page

Search results