Search

Type
Class
Namespace
craft\services
Inherits
craft\services\Search » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
3.0

Handles search operations.

An instance of the Search service is globally accessible in Craft via Craft::$app->search.

View source

Public Properties

PropertyDescription
behaviorsyii\base\Behavior – List of behaviors attached to this component
maxPostgresKeywordLengthinteger – 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.
minFullTextWordLengthinteger – The minimum word length that keywords must be in order to use a full-text search.

maxPostgresKeywordLength

Type
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. This value is a hard limit to truncate search index data for a single row in Postgres.

View source

minFullTextWordLength

Type
integer

The minimum word length that keywords must be in order to use a full-text search.

View source

Public Methods

MethodDescription
__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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
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.
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.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
trigger()Triggers an event.

filterElementIdsByQuery()

Filters a list of element IDs by a given search query.

View source

Arguments

  • $elementIds (integer[]) – The list of element IDs to filter by the search query.
  • $query (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.
  • $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 as element ID => score.

Returns

array – The filtered list of element IDs.

indexElementAttributes()

Indexes the attributes of a given element defined by its element type.

View source

Arguments

Returns

boolean – Whether the indexing was a success.

Throws

indexElementFields()

Indexes the field values for a given element and site.

View source

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.

View source

Events

Type
craft\events\SearchEvent

The event that is triggered after a search is performed.

Type
craft\events\SearchEvent

The event that is triggered before a search is performed.