Elasticsearch throwing http method exceptions. Symfony 4.4 on Platform.sh

445 Views Asked by At

I'm using the FOS Elasticsearch bundle on Symfony 4.4 on Platform.sh and any attempt to populate results in error.

Relevant config pieces:

composer.json

"require": {
    "php": ">=7.3.0",
    ...
    "friendsofsymfony/elastica-bundle": "^5.1",

platform/services.yml

searchelastic77:
    type: elasticsearch:7.7
    disk: 1024

.platform.app.yml

relationships:
    ...
    elasticsearch: "searchelastic77:elasticsearch"

config/packages/fos_elastica.yml

fos_elastica:
    clients:
        default: { url: '%env(ELASTICSEARCH_URL)%' }
    indexes:
        app_post:
            types:
                post:
                    properties:
                        title: ~
                        excerpt: ~
                        content: ~
                        author: ~
                    persistence:
                        driver: orm
                        model: App\Entity\Post
                        provider: ~
                        finder: ~

.env

###> friendsofsymfony/elastica-bundle ###
ELASTICSEARCH_URL=http://localhost:9200/
###< friendsofsymfony/elastica-bundle ###

In the platform.sh settings for this branch, I have the following variable configured:

env:ELASTICSEARCH_URL = http://elasticsearch.internal:9200

running platform:relationships on this branch yields:

elasticsearch:
    -
        service: searchelastic77
        ip: ***
        cluster: ***
        host: elasticsearch.internal
        rel: elasticsearch
        scheme: http
        port: 9200
        url: 'http://elasticsearch.internal:9200'

The Error seems to have to do with incorrect HTTP methods. Attempting to populate the index on the live server by running the command bin/console fos:elastica:populate yields the following exception:

Resetting app_post

In Http.php line 182:
                                                                                    
  Incorrect HTTP method for uri [/] and method [PUT], allowed: [GET, DELETE, HEAD]  
                                                                                    

fos:elastica:populate [--index [INDEX]] [--type [TYPE]] [--no-reset] [--no-delete] [--sleep SLEEP] [--ignore-errors] [--no-overwrite-format] [--first-page FIRST-PAGE] [--last-page LAST-PAGE] [--max-per-page MAX-PER-PAGE] [--pager-persister PAGER-PERSISTER] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>

Attempting to update post entity on the production server also yields this error:

request.CRITICAL: Uncaught PHP Exception Elastica\Exception\ResponseException: "Incorrect HTTP method for uri [/] and method [POST], allowed: [GET, DELETE, HEAD]" at /app/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php line 182 {"exception":"[object] (Elastica\\Exception\\ResponseException(code: 0): Incorrect HTTP method for uri [/] and method [POST], allowed: [GET, DELETE, HEAD] at /app/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php:182)"} []

I have tried various combinations of Elastic search versions (5.2, 6.5 and now 7.7) and all are throwing the same errors.

1

There are 1 best solutions below

0
T. Dylan On

I recently encountered a similar issue when configuring FosElasticaBundle with Platform.sh, and I've found a solution. I had the same problem as you, where the ELASTICSEARCH_URL on Platform.sh did not end with a "/".

In a typical Symfony setup, the following configuration works fine:

fos_elastica:
    clients:
        default:
            url: '%env(ELASTICSEARCH_URL)%'

However, on Platform.sh, the ELASTICSEARCH_URL does not end with a "/".

To resolve this issue on Platform.sh, you should use the following configuration instead:

fos_elastica:
    clients:
        default:
            host: '%env(ELASTICSEARCH_HOST)%'
            port: '%env(ELASTICSEARCH_PORT)%'

By specifying the host and port separately, you can accommodate the different format of the Elasticsearch URL on Platform.sh.

I hope this information is helpful and serves as a potential solution for anyone facing a similar issue.