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. |
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. |
filterElementIdsByQuery() | Filters a list of element IDs by a given search query. |
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. |
indexElementFields() | Indexes the field values for a given element and site. |
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. |
trigger() | Triggers an event. |
deleteOrphanedIndexes()
​
- Since
- 3.2.10
Deletes any search indexes that belong to elements that don’t exist anymore.
filterElementIdsByQuery()
​
DEPRECATED
Deprecated in 3.7.14. Use searchElements() instead.
Filters a list of element IDs by a given search query.
Arguments ​
$elementIds
(integer[]) – The list of element IDs to filter by the search query.$searchQuery
(string, array, craft\search\SearchQuery) – The search query (either a string or a SearchQuery instance)$scoreResults
(boolean) – Whether to order the results based on how closely they match the query. (No longer checked.)$siteId
(integer, integer[], null) – The site ID(s) to filter by.$returnScores
(boolean) – Whether the search scores should be included in the results. If true, results will be returned aselement ID => score
.$customFields
(craft\base\FieldInterface[], null) – The custom fields involved in the query.
Returns ​
array – The filtered list of element IDs.
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.
Throws ​
indexElementFields()
​
DEPRECATED
Deprecated in 3.4.0. Use indexElementAttributes() instead.
Indexes the field values for a given element and site.
Arguments ​
$elementId
(integer) – The ID of the element getting indexed.$siteId
(integer) – The site ID of the content getting indexed.$fields
(array) – The field values, indexed by field ID.
Returns ​
boolean – Whether the indexing was a success.
Throws ​
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
- 3.8.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 – The filtered list of element IDs.
Events ​
EVENT_AFTER_SEARCH ​
The event that is triggered after a search is performed.
EVENT_BEFORE_SEARCH ​
The event that is triggered before a search is performed.