la-haute-societe / craft-elasticsearch

Bring the power of Elasticsearch to your Craft CMS projects
Other
18 stars 14 forks source link

Getting 404 when Using Elasticsearch on Headless CMS #20

Closed teamteak closed 2 years ago

teamteak commented 2 years ago

We are using Craft as a headless CMS that will drive our main site. I'm noticing that there is no place to indicate which URL for elasticsearch to index thus we get a 404's when starting up the index:

Client error:GET https://cms.foo.io/blog/how-to-host-the-ultimate-outdoor-thanksgiving?token=O-cYTmBNz-jmjVdO_VETQkPSMyF18qDHresulted in a404 Not Foundresponse: {"name":"Not Found","message":"Page not found.","code":0,"status":404}

Our cms live on cms.foo.io while our main site (for now) lives on our stating.foo.com site.

Is this possible? Is there a place in the config.php where we can indicate our main site?

nstCactus commented 2 years ago

You should be able to achieve what you want by overriding the elementContentCallback.

You can get some inspiration in the ElementIndexerService::getElementIndexableContent() method.

teamteak commented 2 years ago

Gotcha. Please help me if I am wrong, but the method getElementIndexableContent(Element $element) gets the URL from the passed in element, correct? How then would we then update the $element->url

Would that not have to be updated from where it's being called? Maybe I am missing something.

teamteak commented 2 years ago

So far this is what I have created for that custom method (we don't really care about Assets or Products):

'elementContentCallback' => function(Element $element){
        Craft::debug('Getting element page content : ' . $element->url, __METHOD__);

        $schemaVersion = Craft::$app->getInstalledSchemaVersion();

        if (version_compare($schemaVersion, '3.2.0', '>=')) {
            $token = Craft::$app->getTokens()->createToken(
                [
                    'preview/preview',
                    [
                        'elementType' => get_class($element),
                        'sourceId'    => $element->id,
                        'siteId'      => $element->siteId,
                        'draftId'     => null,
                        'revisionId'  => null,
                    ],
                ]
            );
        } else {
            $token = Craft::$app->getTokens()->createToken(
                [
                    'entries/view-shared-entry',
                    ['entryId' => $element->id, 'siteId' => $element->siteId],
                ]
            );
        }

        // Generate the sharable url based on the previously generated token
        $url = UrlHelper::urlWithToken($element->getUrl(), $token);

        // Request the page content with GuzzleHttp\Client
        $client = new \GuzzleHttp\Client(['connect_timeout' => 10]);
        try {
            $res = $client->request('GET', $url);
            if ($res->getStatusCode() === 200) {
                return $this->extractIndexablePart($res->getBody());
            }
        } catch (\GuzzleHttp\Exception\RequestException $e) {
            Craft::error('Could not get element content: ' . $e->getMessage(), __METHOD__);
            throw new IndexElementException($e->getMessage(), 0, $e);
        } catch (\Exception $e) {
            throw new IndexElementException(
                Craft::t(
                    ElasticsearchPlugin::PLUGIN_HANDLE,
                    'An error occurred while parsing the element page content: {previousExceptionMessage}',
                    ['previousExceptionMessage' => $e->getMessage()]
                ), 0, $e
            );
        }

        return false;
    },
teamteak commented 2 years ago

Figured this out. just had to update the Base URL in site settings.