getgrav / grav-premium-issues

Official Grav Premium Issues repository to report problems or ask questions regarding the Premium products offered.
https://getgrav.org/premium
7 stars 2 forks source link

[algolia-pro] Can't use custom search class on frontend #339

Open Sogl opened 1 year ago

Sogl commented 1 year ago

I use this tutorial to create custom class for my Flex objects: https://getgrav.org/premium/algolia-pro/docs/backend#custom-search-classes-advanced

My getRecord code (I haven't figured out about headers and tags yet):

protected function getRecord(FlexObjectInterface $object): array 
{
    $records = [];
    $record = new \stdClass();
    $counter = 0;

    // Standard bits
    $record->id = md5($object->slug);
    $record->title = $object->title;
    $record->url = $this->getUrl($object);

    $content_chunks = $this->splitHTMLContent($object->description);

    foreach ($content_chunks as $content_chunk) {
        $record->content = $content_chunk['content'];
        $record->objectID = $record->id . '_' . $counter++;
        $records[] = (array) $record;
    }

    return $records;
}

It works for me on the first run. My query results (bin/plugin algolia-pro query радужке) for example:

image

Algolia index created fine:

image

But I can't connect this index to my Learn4-based theme. Steps:

  1. I disabled all indexes except flex:
image
  1. I changed index in base.html.twig:
 {% include 'partials/algolia-pro/instantsearch.html.twig' ignore missing with { index: 'flex' } %}
  1. Error on frontend:
image

During the debug I found that my index is not connected because there is no indexConfiguration method in my search class (AlgoliaProController.php line 137):

image

Do I need to recreate this method in my class? I don't see it in Docs or in the code of the Reviews plugin.

Sogl commented 1 year ago

I copied the entire indexConfiguration method into my class without changing anything.

Now it works but without preview snippet:

image
Sogl commented 1 year ago

I changed getRecord code very much by looking at the GravPageSearch.php file:

https://gist.github.com/Sogl/78dce78b254b39a6678f759b3ff981b7#file-flextherapiessearch-php-L84

Still no prieviews/summary. 😞

Example of Flex object in Algolia with all attributes:

image
Sogl commented 1 year ago

Got it working!

image

First of all, it's a part of default configuration in common-config.yaml:

search_params.attributesToSnippet:
          type: array
          label: PLUGIN_ALGOLIA_PRO.ATTR_TO_SNIPPET
          default: [ 'content:50' ]
          value_only: true
          validate:
            required: true

This default line was used for the new index. summary:50 is missing here, which @rhukster has already talked about in another issue about crawl snippets.

Secondly, since a common template is used with already written Vue variables, then searcheable_fields should be the same:

searchable_fields:
      - title
      - subtitle
      - url
      - taxonomy
      - headers.h1
      - headers.h2
      - headers.h3
      - headers.h4
      - content

As a result, I copied most of the settings from the pages index.

The only thing I haven't figured out yet is the formation of language prefixes for indexes.