PHP code example of distantnative / search-for-kirby

1. Go to this page and download the library: Download distantnative/search-for-kirby 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/ */

    

distantnative / search-for-kirby example snippets


'search' => [
    'entries' => [
        'pages' => 'site.find("projects").index', 
        'files' => false,
        // users will remain the default 
    ]
]

'search' => [
    'fields' => [
        'pages' => [
            'title'
        ],
        'files' => [
            'filename'
        ],
        'users' => [
            'email',
            'name'
        ]
    ]
]

// Simply add the field
'title',

// Add the field, but run it through a field method first
'text' => 'kirbytext',

// Pass parameters to the field method
'text' => ['short', 50],

// Use a callback function
'text' => function ($model) { 
    return strip_tags($model->text()->kt());
}

// Turn string field value into number (e.g. for Algolia filters)
'myNumberField' => function ($model) { 
    return $model->myNumberField()->toFloat();
}

'myVirtualNumberField' => function ($model) { 
    return $model->myNumberField()->toInt() + 5;
}

'myDate' => function ($model) { 
    return $model->anyDateField()->toTimestamp();
}

'search' => [
    'templates' => [
        'pages' => function ($model) {
             return $model->id() !== 'home' && $model->id() !== 'error';
         },
        'files' => null,
        'users' => null
    ]
]
``

If the value is null or an empty array, all templates are allowed. If it is false, all templates are excluded. There are several other options:


$site->search('query', $options = []);

$page->children()->listed()->filterBy('template', 'project')->search('query', $options = []);

search(string $query, $options = [], $collection = null)

collection('notes')->search($query, [
    'operator' => 'AND',
    'limit' => 100
]);

// site/config/config.php

'search' => [
    'provider' => 'sqlite',
    'sqlite' => [
        // Enabled for all fields
        'fuzzy' => true,

        // Disabled completely
        'fuzzy' => false,

        // Only for selected fields
        'fuzzy' => [
            'pages' => ['title'],
            'files' => ['caption', 'credits']
        ],
    ]
]

// site/config/config.php

'search' => [
    'provider' => 'sqlite',
    'sqlite' => [
        'weights' => [
            'title'   => 10,
            'caption' => 5
        ],
    ]
]

// site/config/config.php

'search' => [
    'provider' => 'algolia',
    'algolia' => [
        'app'     => ...,     // Algolia App ID
        'key'     => ...,     // Algolia private admin key (not just read-only!)
        'index'   => 'kirby'  // name of the index to use/create
        'options' => []       // options to pass to Algolia at search
    ]
]

'search' => [
  'hooks' => false
]