Jump to content

SearchEngine


teppo

Recommended Posts

Hello,

first of: thank you for this great module ?

I have implemented it more or less successfully. But I've hit a wall. I've tried generating the code with JSON, manually or with the default setup, where the module handles everything. And no matter what I try, I don't get a summary to work. I will receive title and url, but no summary. I am using Multilanguage (with de and en) and most fields are RepeaterMatrix.

German and Englisch works fine, Multilanguage works on the titles and urls.

But I don't receive any kind of summary in the code.

I've checked and rechecked the documentation, but I didn't find a solution for it.

Link to comment
Share on other sites

15 minutes ago, GedankenSchmiede said:

But I don't receive any kind of summary in the code.

I've checked and rechecked the documentation, but I didn't find a solution for it.

The "summary", just in case this was overlooked, isn't (entirely) an in-built feature for the module. It needs to be told what field in your template(s) will be used for the search result summary when rendered. From the documentation on the Modules page, under the "Options" heading, check the render_args property of the module's config, and look for the below:

// Summary of each result (in the search results list) is the value of this field.
'result_summary_field' => 'summary',

In the config, the "result_summary_field" points to the field used in your instance of ProcessWire this module is being used in that will be used to render the search result template's summary. So if in your templates you either don't have a summary field, or the field you use to define a summary is named differently, you'd need to use whatever value you have for your template(s). If maybe you used something like "short_description" as a page summary field, go with that. If you don't have a summary, you could use a "body" or "content" field, and in the render template use some form of string truncation, such as sanitizer()->truncate($your_summary_field, 80).

If you're using JSON, slightly further down is a different section for that:

// These settings define the fields used when search results are rendered as JSON.
'results_json_fields' => [
    'title' => 'title',
    'desc' => 'summary',
    'url' => 'url',
],

Does that help at all?

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

This is really great and I appreciate the module and the approach!

Question: Is it possible to manually add content that should be indexed? My use case is fields that exist within RockPageBuilder and I'd like to update the index with field content using a hook.

Thanks!

Link to comment
Share on other sites

Hi @FireWire,

before @Teppo gives you the right answer and trick ? i can already answer, yes it is possible

i have a website that uses a lot data coming from a totally different db for some pages and i wanted those contents and pages to be indexed as well, here is the trick
- i've created a field (textarea) named extcont (for external content but of course, name it as you want ? ) and added this field to the indexable fields for the template that have it
- and then, i used a hook in the ready.php file to fill the module index field this way

$this->addHookAfter('Pages::saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    $template = $page->get('template');
    require_once('templates/_dbc.php'); // my connectionh to the external db needed by the class method i use below
	require_once('templates/_func.php'); // same thing for some functions i need in that same method
    if ( $template == 'an_edition' )
    {
        require_once('classes/myVictimPage.php');
        $id_ed = $page->id_ed; // a "normal" field in the page to get the... edition id :)
        $ext = $page->get_ext($id_ed); // a method in the template class that returns all the content i need to index in raw form
        $page->extcont = $ext[0]; // for the default language
        $page->extcont->setLanguageValue('en', $ext[1]); // guess, it's a multilingual website :) and here i add data to the field
    }
    //... and some more for the other templates that need it
}

and it works like a charm for many different templates, a program, history, etc;, in your case i think you may just have to add RockPageBuilder returned content but here is the kind of hook you can use to add some extra content to the the field indexed by the module
little piece of advcie is add only raw text content without any html

hope it may help

have a nice day

  • Thanks 1
Link to comment
Share on other sites

@virtualgadjo I was able to find a similar conversation on a RPB module support thread and put something together. I did take an idea from you using the Pages::saveReady event rather than the Pages::saved event in that example which I like better. Same setup with the extra field as you have done. In case this helps anyone else, here's what I came up with. It may be useful outside of the context of RockPageBuilder where field types matter.

/**
 * Adds any content in RockPageBuilder field blocks to the dedicated indexable search_engine_block
 * field that it then added to the search index
 */
$wire->addHookBefore('Pages::saveReady', function($event) {
    $page = $event->arguments(0);

    // Get only RPB fields if they exist
    $rpbFields = array_filter($page->fields->getArray(), function(Field $field) {
        return $field->type instanceof FieldTypeRockPageBuilder;
    });

    if (!$rpbFields) {
        return;
    }

    // Map RPB fields with values from getSearchIndexValues method if it exists on the child block
    $indexableContent = array_map(function(Field $rpbField) use ($page) {
        $blocks = $page->getFormatted($rpbField->name)->getArray();

        // Merge content for each block within a field into a single array
        return array_reduce($blocks, function($values, $block) {
            if (!method_exists($block, 'getSearchIndexValues')) {
                return $values;
            }

            return $values = [...$values, ...$block->getSearchIndexValues()];
        }, []);

    }, $rpbFields);

    // Flatten array of arrays containing index-prepared content
    $indexableContent = array_merge(...$indexableContent);

    if ($indexableContent) {
        // This is where it may be improved to make use of a SearchEngine method
        $page->search_index_blocks = implode(' ... ', $indexableContent);
    }
});

The last comment is right above where I think it would be useful to see if there's a way to make use of the SearchEngine object to index content. My implode() method is mimicking the format of the search_index field with ' ... ' but deferring that rather than mimicking it would be great. It's not a dealbreaker but it would help keep my code from knowing too much about how SearchEngine works internally.

Thanks @virtualgadjo for sharing!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...