Search ​
- Type
- Class
- Namespace
- craft\services
- Inherits
- craft\services\Search » yii\base\Component » yii\base\BaseObject
- Implements
- yii\base\Configurable
- Since
- 3.0.0
Search service.
An instance of the service is available via Craft::$app->search
.
Public Properties ​
Property | Description |
---|---|
behaviors | yii\base\Behavior – List of behaviors attached to this component. |
maxPostgresKeywordLength | integer – Because the keywords column in the search index table is a B-TREE index on Postgres, you can get an "index row size exceeds maximum for index" error with a lot of data. |
minFullTextWordLength | integer, null – The minimum word length that keywords must be in order to use a full-text search (MySQL only). |
useFullText | boolean – Whether fulltext searches should be used ever. |
maxPostgresKeywordLength
​
- Type
- integer
- Default value
2450
Because the keywords
column in the search index table is a B-TREE index on Postgres, you can get an "index row size exceeds maximum for index" error with a lot of data. This value is a hard limit to truncate search index data for a single row in Postgres.
minFullTextWordLength
​
The minimum word length that keywords must be in order to use a full-text search (MySQL only).
useFullText
​
- Type
- boolean
- Default value
true
- Since
- 3.4.10
Whether fulltext searches should be used ever. (MySQL only.)
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__clone() | This method is called after the object is created by cloning an existing one. |
__construct() | Constructor. |
__get() | Returns the value of a component property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets the value of a component property. |
__unset() | Sets a component property to be null. |
attachBehavior() | Attaches a behavior to this component. |
attachBehaviors() | Attaches a list of behaviors to the component. |
behaviors() | Returns a list of behaviors that this component should behave as. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
createDbQuery() | Returns a database query which will fetch results for a given search query. |
deleteOrphanedIndexes() | Deletes any search indexes that belong to elements that don’t exist anymore. |
detachBehavior() | Detaches a behavior from the component. |
detachBehaviors() | Detaches all behaviors from the component. |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. |
getBehavior() | Returns the named behavior object. |
getBehaviors() | Returns all behaviors attached to this component. |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined for this component. |
indexElementAttributes() | Indexes the attributes of a given element defined by its element type. |
init() | Initializes the object. |
normalizeSearchQuery() | Normalizes a search param into a craft\search\SearchQuery object. |
off() | Detaches an existing event handler from this component. |
on() | Attaches an event handler to an event. |
searchElements() | Searches for elements that match the given element query. |
shouldCallSearchElements() | Returns whether we should search for the resulting elements up front via searchElements(), rather than supply a subquery which should be applied to the main element query via createDbQuery(). |
trigger() | Triggers an event. |
createDbQuery()
​
- Since
- 4.6.0
Returns a database query which will fetch results for a given search query.
Arguments ​
$searchQuery
(string, array, craft\search\SearchQuery) – The search term to filter the resulting elements by.$elementQuery
(craft\elements\db\ElementQuery) – The element query being executed
Returns ​
deleteOrphanedIndexes()
​
- Since
- 3.2.10
Deletes any search indexes that belong to elements that don’t exist anymore.
indexElementAttributes()
​
Indexes the attributes of a given element defined by its element type.
Arguments ​
$element
(craft\base\ElementInterface)$fieldHandles
(string[], null) – The field handles that should be indexed, ornull
if all fields should be indexed.
Returns ​
boolean – Whether the indexing was a success.
init()
​
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
normalizeSearchQuery()
​
- Since
- 4.4.0
Normalizes a search
param into a craft\search\SearchQuery object.
Arguments ​
$searchQuery
(string, array, craft\search\SearchQuery)
Returns ​
searchElements()
​
- Since
- 3.7.14
Searches for elements that match the given element query.
Arguments ​
$elementQuery
(craft\elements\db\ElementQuery) – The element query being executed
Returns ​
array<string,int>
– The element scores (descending) indexed by element ID and site ID (e.g. '100-1'
).
shouldCallSearchElements()
​
- Since
- 4.8.0
Returns whether we should search for the resulting elements up front via searchElements(), rather than supply a subquery which should be applied to the main element query via createDbQuery().
If the element query is being ordered by score
, searchElements() will be called regardless of what this returns.
Arguments ​
$elementQuery
(craft\elements\db\ElementQuery)
Returns ​
Events ​
EVENT_AFTER_SEARCH ​
The event that is triggered after a search is performed.
Any modifications to craft\events\SearchEvent::$scores will be respected.
EVENT_BEFORE_INDEX_KEYWORDS ​
- Type
- craft\events\IndexKeywordsEvent
- Since
- 4.2.0
The event that is triggered before keywords are indexed for an element attribute or field.
You may set craft\events\CancelableEvent::$isValid to false
to prevent the attribute/field’s keywords from being indexed.
EVENT_BEFORE_SCORE_RESULTS ​
- Type
- craft\events\SearchEvent
- Since
- 4.3.0
The event that is triggered before the results are scored.
Any modifications to craft\events\SearchEvent::$results will be respected when results are scored.
Event handlers can set craft\events\SearchEvent::$scores to override the resulting element scores returned by searchElements().
EVENT_BEFORE_SEARCH ​
The event that is triggered before a search is performed.