PHP code example of percipiolondon / craft-typesense

1. Go to this page and download the library: Download percipiolondon/craft-typesense library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

percipiolondon / craft-typesense example snippets


'collections' => [
    // CONTENT
    \percipiolondon\typesense\TypesenseCollectionIndex::create(
        [
            'name' => 'blog',
            'section' => 'blog.blog', //section handle + entry type handle
            'fields' => [
                [
                    'name' => 'title',
                    'type' => 'string',
                    'sort' => true,
                ],
                [
                    'name' => 'slug',
                    'type' => 'string',
                    'facet' => true
                ],
                [
                    'name' => 'handle',
                    'type' => 'string',
                ],
                [
                    'name' => 'post_date_timestamp',
                    'type' => 'int32',
                ],
            ],
            'default_sorting_field' => 'post_date_timestamp', // can only be an integer,
            'resolver' => static function(\craft\elements\Entry $entry) {
                return [
                    'id' => (string)$entry->id,
                    'title' => $entry->title,
                    'handle' => $entry->section->handle,
                    'slug' => $entry->slug,
                    'post_date_timestamp' => (int)$entry->postDate->format('U')
                ];
            }
        ]
    )
        ->elementType(\craft\elements\Entry::class)
        ->criteria(function(\craft\elements\db\EntryQuery $query) {
            return $query->section('blog');
        }),
]