malformed URL using FOS elastica in Symfony

I’m struggling with a bug related, I believe, to FOS elastica. I am trying to search for all videos in ES index, but I keep getting an error "Malformed URL" in the twig template. Also, when I look at the symfony profiler, I see that no elastica queries were performed.

Here’s my code:

docker-compose.yml:

  elasticsearch:
    container_name: mysite_elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
    environment:
      - "discovery.type=single-node"
      - "bootstrap.memory_lock=true"
      - "ES_JAVA_OPTS=-Xms1G -Xmx1G"
      - "xpack.security.enabled=false"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
    - 9209:9200

.env:

ELASTICSEARCH_URL=http://localhost:9209/

services.yaml:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        bind:
            $videoFinder: '@fos_elastica.finder.videos'

fos_elastica.yaml:

fos_elastica:
    clients:
        default: { url: '%env(ELASTICSEARCH_URL)%' }
    indexes:
        videos:
            persistence:
                driver: orm
                model: AppEntityVideo
                provider: ~
                finder: ~
            properties:
                title: ~
                duration: ~
#                tags: { boost: 2 }
                createdAt: ~

MainController.php:

<?php

namespace AppController;

use AppServiceConstantService;
use ElasticaQueryBoolQuery;
use ElasticaQueryTerms;
use FOSElasticaBundleFinderTransformedFinder;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;


class MainController extends AbstractController
{

    /**
     * @Route("/")
     * @param Request $request
     * @param TransformedFinder $videoFinder
     * @return Response
     */
        public function index(Request $request, TransformedFinder $videoFinder): Response
        {
            $g = $request->query->get('g', 'banned');

            $boolQuery = new BoolQuery();
            $bannedTagsQuery = new Terms('tags', ConstantService::$BANNED_TAGS);

            if ( $g === 'banned' ) {
                $boolQuery->addMustNot($bannedTagsQuery);

            } else {
                $boolQuery->addMust($bannedTagsQuery);
            }

            $results = $videoFinder->findHybridPaginated($boolQuery, ["limit" => 120]);

        return $this->render("main.html.twig", [
            'videos' => $results
        ]);
    }
}

and the twig part that in pinpointed by profiler:

<div class="grid-container">
{# line below is what "cause" the error according to symfony profiler #}
    {% for video in videos %}
        {{ include('tube/video.html.twig', { video: video.transformed }) }}
    {%  endfor %}
</div>

I’ve search already for hours and hours but can’t find the problem. I’ve modified the URL in .env to check if it could be the problem, but it seems to be good (also, navigating to this URL return a valid ES response)
What I found so far is that "videos" seems to be empty, because there is no ES queries performed. Why? I don’t know…only that the error message is An exception has been thrown during the rendering of a template ("Malformed URL").

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *