src/ElasticEngine.php
public function buildSearchQueryPayloadCollection(Builder $builder, array $options = [])
    {
        $payloadCollection = collect();

        if ($builder instanceof SearchBuilder) {
            $searchRules = $builder->rules ?: $builder->model->getSearchRules();

            foreach ($searchRules as $rule) {
                $payload = new TypePayload($builder->model);

                if (is_callable($rule)) {
                    $payload->setIfNotEmpty('body.query.bool', call_user_func($rule, $builder));
                } else {
                    /** @var SearchRule $ruleEntity */
                    $ruleEntity = new $rule($builder);

                    if ($ruleEntity->isApplicable()) {
                        $payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload());

                        if ($options['highlight'] ?? true) {
                            $payload->setIfNotEmpty('body.highlight', $ruleEntity->buildHighlightPayload());
                        }
                    } else {
                        continue;
                    }
                }

                $payloadCollection->push($payload);
            }
        } else {
            $payload = (new TypePayload($builder->model))
                ->setIfNotEmpty('body.query.bool.must.match_all', new stdClass());

            $payloadCollection->push($payload);
        }

        return $payloadCollection->map(function (TypePayload $payload) use ($builder, $options) {
            $payload
                ->setIfNotEmpty('body._source', $builder->select)
                ->setIfNotEmpty('body.collapse.field', $builder->collapse)
                ->setIfNotEmpty('body.sort', $builder->orders)
                ->setIfNotEmpty('body.explain', $options['explain'] ?? null)
                ->setIfNotEmpty('body.profile', $options['profile'] ?? null)
                ->setIfNotNull('body.from', $builder->offset)
                ->setIfNotNull('body.size', $builder->limit);

            foreach ($builder->wheres as $clause => $filters) {
                $clauseKey = 'body.query.bool.filter.bool.'.$clause;

                $clauseValue = array_merge(
                    $payload->get($clauseKey, []),
                    $filters
                );

                $payload->setIfNotEmpty($clauseKey, $clauseValue);
            }

            return $payload->get();
        });
    }
public function buildSearchQueryPayloadCollection(Builder $builder, array $options = [])
    {
        $payloadCollection = collect();

        if ($builder instanceof SearchBuilder) {
            $searchRules = $builder->rules ?: $builder->model->getSearchRules();

            foreach ($searchRules as $rule) {
                $payload = new TypePayload($builder->model);

                if (is_callable($rule)) {
                    $payload->setIfNotEmpty('body.query.bool', call_user_func($rule, $builder));
                } else {
                    /** @var SearchRule $ruleEntity */
                    $ruleEntity = new $rule($builder);

                    if ($ruleEntity->isApplicable()) {
                        $payload->setIfNotEmpty('body.query.bool', $ruleEntity->buildQueryPayload());

                        if ($options['highlight'] ?? true) {
                            $payload->setIfNotEmpty('body.highlight', $ruleEntity->buildHighlightPayload());
                        }
                    } else {
                        continue;
                    }
                }

                $payloadCollection->push($payload);
            }
        } else {
            $payload = (new TypePayload($builder->model))
                ->setIfNotEmpty('body.query.bool.must.match_all', new stdClass());

            $payloadCollection->push($payload);
        }

        return $payloadCollection->map(function (TypePayload $payload) use ($builder, $options) {
            $payload
                ->setIfNotEmpty('body._source', $builder->select)
                ->setIfNotEmpty('body.collapse.field', $builder->collapse)
                ->setIfNotEmpty('body.sort', $builder->orders)
                ->setIfNotEmpty('body.explain', $options['explain'] ?? null)
                ->setIfNotEmpty('body.profile', $options['profile'] ?? null)
                ->setIfNotNull('body.from', $builder->offset)
                ->setIfNotNull('body.size', $builder->limit);

            foreach ($builder->wheres as $clause => $filters) {
                $clauseKey = 'body.query.bool.filter.bool.'.$clause;

                $clauseValue = array_merge(
                    $payload->get($clauseKey, []),
                    $filters
                );

                $payload->setIfNotEmpty($clauseKey, $clauseValue);
            }

            return $payload->get();
        });
    }
public function buildSearchQueryPayloadCollection ( Builder $ builder , array $ options = [ ] ) { $ payloadCollection = collect ( ) ; if ( $ builder instanceof SearchBuilder ) { $ searchRules = $ builder -> rules ? : $ builder -> model -> getSearchRules ( ) ; foreach ( $ searchRules as $ rule ) { $ payload = new TypePayload ( $ builder -> model ) ; if ( is_callable ( $ rule ) ) { $ payload -> setIfNotEmpty ( 'body.query.bool' , call_user_func ( $ rule , $ builder ) ) ; } else { /** @var SearchRule $ruleEntity */ $ ruleEntity = new $ rule ( $ builder ) ; if ( $ ruleEntity -> isApplicable ( ) ) { $ payload -> setIfNotEmpty ( 'body.query.bool' , $ ruleEntity -> buildQueryPayload ( ) ) ; if ( $ options [ 'highlight' ] ?? true ) { $ payload -> setIfNotEmpty ( 'body.highlight' , $ ruleEntity -> buildHighlightPayload ( ) ) ; } } else { continue ; } } $ payloadCollection -> push ( $ payload ) ; } } else { $ payload = ( new TypePayload ( $ builder -> model ) ) -> setIfNotEmpty ( 'body.query.bool.must.match_all' , new stdClass ( ) ) ; $ payloadCollection -> push ( $ payload ) ; } return $ payloadCollection -> map ( function ( TypePayload $ payload ) use ( $ builder , $ options ) { $ payload -> setIfNotEmpty ( 'body._source' , $ builder -> select ) -> setIfNotEmpty ( 'body.collapse.field' , $ builder -> collapse ) -> setIfNotEmpty ( 'body.sort' , $ builder -> orders ) -> setIfNotEmpty ( 'body.explain' , $ options [ 'explain' ] ?? null ) -> setIfNotEmpty ( 'body.profile' , $ options [ 'profile' ] ?? null ) -> setIfNotNull ( 'body.from' , $ builder -> offset ) -> setIfNotNull ( 'body.size' , $ builder -> limit ) ; foreach ( $ builder -> wheres as $ clause => $ filters ) { $ clauseKey = 'body.query.bool.filter.bool.' . $ clause ; $ clauseValue = array_merge ( $ payload -> get ( $ clauseKey , [ ] ) , $ filters ) ; $ payload -> setIfNotEmpty ( $ clauseKey , $ clauseValue ) ; } return $ payload -> get ( ) ; } ) ; }
Build the payload collection.

@param \Laravel\Scout\Builder $builder
@param array $options
@return \Illuminate\Support\Collection
Build the payload collection .