protected function createSortedQuery(AbstractQuery $query, SearchEventsRequest $request): Query
    {
        switch ($request->get('sort')->getValue()) {
            case SearchEventsSort::DATE_DESC:
                $query = Query::create($query);
                $query->setSort(['occurred_at' => 'desc']);
                break;

            case SearchEventsSort::DATE_ASC:
                $query = Query::create($query);
                $query->setSort(['occurred_at' => 'asc']);
                break;

            default:
                // recency scores higher
                // @link https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html
                $before = $request->get('occurred_before') ?: new \DateTime('now', new \DateTimeZone('UTC'));
                $query = (new FunctionScore())
                    ->setQuery($query)
                    ->addFunction(FunctionScore::DECAY_EXPONENTIAL, [
                        IndexManager::OCCURRED_AT_ISO_FIELD_NAME => [
                            'origin' => $before->format(DateUtils::ISO8601_ZULU),
                            'scale'  => '1w',
                            'offset' => '2m',
                            'decay'  => 0.1,
                        ],
                    ]);
                $query = Query::create($query);
                break;
        }

        return $query;
    }
protected function createSortedQuery(AbstractQuery $query, SearchEventsRequest $request): Query
    {
        switch ($request->get('sort')->getValue()) {
            case SearchEventsSort::DATE_DESC:
                $query = Query::create($query);
                $query->setSort(['occurred_at' => 'desc']);
                break;

            case SearchEventsSort::DATE_ASC:
                $query = Query::create($query);
                $query->setSort(['occurred_at' => 'asc']);
                break;

            default:
                // recency scores higher
                // @link https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html
                $before = $request->get('occurred_before') ?: new \DateTime('now', new \DateTimeZone('UTC'));
                $query = (new FunctionScore())
                    ->setQuery($query)
                    ->addFunction(FunctionScore::DECAY_EXPONENTIAL, [
                        IndexManager::OCCURRED_AT_ISO_FIELD_NAME => [
                            'origin' => $before->format(DateUtils::ISO8601_ZULU),
                            'scale'  => '1w',
                            'offset' => '2m',
                            'decay'  => 0.1,
                        ],
                    ]);
                $query = Query::create($query);
                break;
        }

        return $query;
    }
protected function createSortedQuery ( AbstractQuery $ query , SearchEventsRequest $ request ) : Query { switch ( $ request -> get ( 'sort' ) -> getValue ( ) ) { case SearchEventsSort :: DATE_DESC : $ query = Query :: create ( $ query ) ; $ query -> setSort ( [ 'occurred_at' => 'desc' ] ) ; break ; case SearchEventsSort :: DATE_ASC : $ query = Query :: create ( $ query ) ; $ query -> setSort ( [ 'occurred_at' => 'asc' ] ) ; break ; default : // recency scores higher // @link https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html $ before = $ request -> get ( 'occurred_before' ) ? : new \ DateTime ( 'now' , new \ DateTimeZone ( 'UTC' ) ) ; $ query = ( new FunctionScore ( ) ) -> setQuery ( $ query ) -> addFunction ( FunctionScore :: DECAY_EXPONENTIAL , [ IndexManager :: OCCURRED_AT_ISO_FIELD_NAME => [ 'origin' => $ before -> format ( DateUtils :: ISO8601_ZULU ) , 'scale' => '1w' , 'offset' => '2m' , 'decay' => 0.1 , ] , ] ) ; $ query = Query :: create ( $ query ) ; break ; } return $ query ; }
Applies sorting and scoring to the query and returns the final query object
which will be sent to elastic search.

@param AbstractQuery       $query
@param SearchEventsRequest $request

@return Query
Applies sorting and scoring to the query and returns the final query object which will be sent to elastic search .