Download the PHP package winter/wn-search-plugin without Composer
On this page you can find all versions of the php package winter/wn-search-plugin. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download winter/wn-search-plugin
More information about winter/wn-search-plugin
Files in winter/wn-search-plugin
Package wn-search-plugin
Short Description Search plugin for Winter CMS
License MIT
Homepage https://github.com/wintercms/wn-search-plugin
Informations about the package wn-search-plugin
Search Plugin
Adds full-text searching capabilities to Winter, built on the foundations of Laravel Scout. The plugin acts primarily as a wrapper for Laravel Scout, and provides its entire suite of functionality within Winter's architecture, but also includes additional capabilities to make its use in Winter even easier.
Requirements
- PHP 8.0 or above
- Winter CMS 1.2.0 or above (due to Laravel 9 requirement)
Getting started
To install the plugin, you may install it through the Winter CMS Marketplace, or you may install it using Composer:
Then, run the migrations to ensure the plugin is enabled:
Configuration
Configuration for this plugin is chiefly done through the search.php
configuration file. You can publish this configuration into your project's config
directory by running the following command:
This will create your own configuration file at config/winter/search/search.php
, in which you will be able to override all default configuration values.
Preparing your models
As this is a wrapper, you can use all the base functionality that Laravel Scout provides. There are only a couple of subtle differences with the Search plugin's implementation:
- Configuration values are stored within the
search
key. Wherever there is mention of ascout
configuration value, you must usesearch
instead. - Soft deleted models are determined by the usage of the
Winter\Storm\Database\Traits\SoftDelete
trait, not the base LaravelSoftDeletes
trait.
To make a particular database model searchable, you simply add the Winter\Search\Behaviors\Searchable
behavior to that model. This behavior will register a model observer that will automatically synchronise the model records to an index:
For Halcyon models, you must instead use the Winter\Search\Behaviors\Halcyon\Searchable
behavior, in order to correctly hook into the unique functionality that Halcyon provides.
As the model is created, updated or deleted, the index will automatically be updated to reflect the state of that model record.
Configuring searchable data
By default, the entire model is converted to an array form and persisted in the search index. If you would like to limit the data that is stored in the index, you can provide a $searchable
property in the model. This property will represent all the model attributes that you would like to store in the index:
If you want even more control over the data, you may override the toSearchableArray
method:
Adding search to third-party models
You can add search functionality to third-party plugins through use of the Dynamic Class Extension functionality in Winter. This can be done through your Plugin.php
registration file, generally within the boot()
method.
When extending a model in this fashion, you will also likely need to specify the searchable data you wish to include in your search index, using the $searchable
property or toSearchableArray()
method specified previously.
Configuring the model ID
Normally, the primary key of the model will act as the model's unique ID that is stored in the search index. If you wish to use another column to act as the identifier for a model, you may override the getSearchKey
and getSearchKeyName
methods to customise this behaviour.
NOTE: Some search providers, such as Meilisearch, enforce restrictions on the characters allowed for IDs. To be safe, we recommend that you restrict IDs to using the following characters:
A-Z
,a-z
,0-9
and dashes and underscores. Any other characters may prevent some search providers from indexing your record.
Registering search handlers
Once your models are prepared for searching capabilities, you may register a search handler that allows the models to be searched by the included components.
Registration of a search handler takes place in your Plugin.php
file by specifying a registerSearchHandlers
method that returns an array.
Each array item should specify a key name that represents the ID of the search handler. The following properties can be specified as part of the handler:
Property | Description |
---|---|
name |
The human-readable name of your search handler. This will be used for grouped results. |
model |
The name of the class you wish to search with this handler. |
record |
A handler for each record returned in the results. See below for more information on the valid configurations for this property. |
Record handler
Each search handler may also provide a result handler to finely tune how you wish to display or filter the results. At its most simplest, the record handler simply expects an array to be returned for each record that contains 3 properties:
title
: The title of the result.description
: Additional context for the result.url
: The URL that the result will point to.
It may also optionally provide the following properties to provide more context:
group
: The group that this result belongs to, when using grouped results.label
: The label of the result, which may provide more context or grouping for results.image
: The path to a corresponding image for the result.
You may, of course, define additional properties in your array.
The record handler can be configured in a number of different ways.
Array map of fields
You can simply return an array with the properties above that map to the corresponding fields within the model.
Array map with callbacks
Similar to the above, you may also specify some or all properties to use a callback method that will be fed two arguments: the model instance of each result, and the original query.
Callback method
You may also make the entire handler go through a callback method. This gives the greatest level of control, as you may also filter records out.
The callback method should always return an array with the main properties defined above, but you may include any additional properties as you wish.
The callback method may also return false
to exclude the record from the results.
Result relevancy
By default, results in the Search plugin are not ordered in any particular way to account for the relevancy of the result. While this may be fine in cases where you are using index engines like Algolia or Meilisearch, which have their own relevancy algorithms (or can be configured as such), this may affect the results when using the Database and Collection index engines, which have no relevancy system.
In order to support a level of relevancy in these engines, results can be post-processed after being retrieved from the index to assign a relevancy score. Relevancy in this system is determined by the order of the property names in the $searchable
definition for the index.
For example, with the below definition:
The title
field will be the most relevant field, followed by the description
and then keywords
. A match in the title
is given greater weight than a match in the description
, which is given more weight than a match in the keywords
.
The relevancy scoring also takes into account the words used in the query. For example, if the query install winter cms
was used, the word install
is given more weight than winter
, which is then given more weight than the word cms
.
To enable result relevancy, you may call the getWithRelevance()
or firstRelevant()
methods following any search:
getWithRelevance()
will retrieve all records, ordered by relevancy score, whilst firstRelevant()
will retrieve only the most relevant record.
If you wish to customise the relevancy scoring, you may also provide a callable to both methods. The callable must accept two arguments, a model instance, and an array of words from the query, and return a score as a float
or an int
, which will be used to order the results descendingly (higher score = more relevant). Each record found in the index will be run through this callable.
All versions of wn-search-plugin with dependencies
composer/installers Version ~1.0
laravel/scout Version ^9.4.5
teamtnt/tntsearch Version ^4.0