Drupal 8  8.0.2
SearchQuery Class Reference
Inheritance diagram for SearchQuery:

Public Member Functions

 searchExpression ($expression, $type)
 
 prepareAndNormalize ()
 
 preExecute (SelectInterface $query=NULL)
 
 addScore ($score, $arguments=array(), $multiply=FALSE)
 
 execute ()
 
 countQuery ()
 
 getStatus ()
 
- Public Member Functions inherited from SelectExtender
 __construct (SelectInterface $query, Connection $connection)
 
 uniqueIdentifier ()
 
 nextPlaceholder ()
 
 addTag ($tag)
 
 hasTag ($tag)
 
 hasAllTags ()
 
 hasAnyTag ()
 
 addMetaData ($key, $object)
 
 getMetaData ($key)
 
 condition ($field, $value=NULL, $operator= '=')
 
conditions ()
 
 arguments ()
 
 where ($snippet, $args=array())
 
 compile (Connection $connection, PlaceholderInterface $queryPlaceholder)
 
 compiled ()
 
 havingCondition ($field, $value=NULL, $operator= '=')
 
havingConditions ()
 
 havingArguments ()
 
 having ($snippet, $args=array())
 
 havingCompile (Connection $connection)
 
 havingIsNull ($field)
 
 havingIsNotNull ($field)
 
 havingExists (SelectInterface $select)
 
 havingNotExists (SelectInterface $select)
 
 extend ($extender_name)
 
getFields ()
 
getExpressions ()
 
getOrderBy ()
 
getGroupBy ()
 
getTables ()
 
getUnion ()
 
 escapeLike ($string)
 
 escapeField ($string)
 
 getArguments (PlaceholderInterface $queryPlaceholder=NULL)
 
 isPrepared ()
 
 preExecute (SelectInterface $query=NULL)
 
 execute ()
 
 distinct ($distinct=TRUE)
 
 addField ($table_alias, $field, $alias=NULL)
 
 fields ($table_alias, array $fields=array())
 
 addExpression ($expression, $alias=NULL, $arguments=array())
 
 join ($table, $alias=NULL, $condition=NULL, $arguments=array())
 
 innerJoin ($table, $alias=NULL, $condition=NULL, $arguments=array())
 
 leftJoin ($table, $alias=NULL, $condition=NULL, $arguments=array())
 
 rightJoin ($table, $alias=NULL, $condition=NULL, $arguments=array())
 
 addJoin ($type, $table, $alias=NULL, $condition=NULL, $arguments=array())
 
 orderBy ($field, $direction= 'ASC')
 
 orderRandom ()
 
 range ($start=NULL, $length=NULL)
 
 union (SelectInterface $query, $type= '')
 
 groupBy ($field)
 
 forUpdate ($set=TRUE)
 
 countQuery ()
 
 isNull ($field)
 
 isNotNull ($field)
 
 exists (SelectInterface $select)
 
 notExists (SelectInterface $select)
 
 __toString ()
 
 __clone ()
 
 __call ($method, $args)
 
 conditionGroupFactory ($conjunction= 'AND')
 
 andConditionGroup ()
 
 orConditionGroup ()
 

Data Fields

const NO_POSITIVE_KEYWORDS = 1
 
const EXPRESSIONS_IGNORED = 2
 
const LOWER_CASE_OR = 4
 
const NO_KEYWORD_MATCHES = 8
 

Protected Member Functions

 parseSearchExpression ()
 
 parseWord ($word)
 

Protected Attributes

 $searchExpression
 
 $type
 
 $keys = array('positive' => array(), 'negative' => array())
 
 $simple = TRUE
 
 $conditions
 
 $matches = 0
 
 $words = array()
 
 $normalize = 0
 
 $executedPrepare = FALSE
 
 $status = 0
 
 $scores = array()
 
 $scoresArguments = array()
 
 $relevance_count = 0
 
 $multiply = array()
 
- Protected Attributes inherited from SelectExtender
 $query
 
 $connection
 
 $uniqueIdentifier
 
 $placeholder = 0
 

Detailed Description

Performs a query on the full-text search index for a word or words.

This query is used by search plugins that use the search index (not all search plugins do, as some use a different searching mechanism). It assumes you have set up a query on the {search_index} table with alias 'i', and will only work if the user is searching for at least one "positive" keyword or phrase.

For efficiency, users of this query can run the prepareAndNormalize() method to figure out if there are any search results, before fully setting up and calling execute() to execute the query. The scoring expressions are not needed until the execute() step. However, it's not really necessary to do this, because this class's execute() method does that anyway.

During both the prepareAndNormalize() and execute() steps, there can be problems. Call getStatus() to figure out if the query is OK or not.

The query object is given the tag 'search_$type' and can be further extended with hook_query_alter().

Member Function Documentation

addScore (   $score,
  $arguments = array(),
  $multiply = FALSE 
)

Adds a custom score expression to the search query.

Score expressions are used to order search results. If no calls to addScore() have taken place, a default keyword relevance score will be used. However, if at least one call to addScore() has taken place, the keyword relevance score is not automatically added.

Note that you must use this method to add ordering to your searches, and not call orderBy() directly, when using the SearchQuery extender. This is because of the two-pass system the SearchQuery class uses to normalize scores.

Parameters
string$scoreThe score expression, which should evaluate to a number between 0 and 1. The string 'i.relevance' in a score expression will be replaced by a measure of keyword relevance between 0 and 1.
array$argumentsQuery arguments needed to provide values to the score expression.
float$multiplyIf set, the score is multiplied with this value. However, all scores with multipliers are then divided by the total of all multipliers, so that overall, the normalization is maintained.
Returns
$this

Referenced by SearchQuery\execute().

Here is the caller graph for this function:

countQuery ( )

Builds the default count query for SearchQuery.

Since SearchQuery always uses GROUP BY, we can default to a subquery. We also add the same conditions as execute() because countQuery() is called first.

Implements SelectInterface.

References SelectExtender\conditions(), db_select(), and SearchQuery\prepareAndNormalize().

Here is the call graph for this function:

execute ( )

Executes the search.

The complex conditions are applied to the query including score expressions and ordering.

Error and warning conditions can apply. Call getStatus() after calling this method to retrieve them.

Returns
|null A query result set containing the results of the query.

Implements SelectInterface.

References SelectExtender\addExpression(), SearchQuery\addScore(), SelectExtender\condition(), SelectExtender\conditions(), SelectExtender\getOrderBy(), SelectExtender\join(), SelectExtender\orderBy(), and SearchQuery\preExecute().

Here is the call graph for this function:

getStatus ( )

Returns the query status bitmap.

Returns
int A bitmap indicating query status. Zero indicates there were no problems. A non-zero value is a combination of one or more of the following flags:
parseSearchExpression ( )
protected

Parses the search query into SQL conditions.

Sets up the following variables:

  • $this->keys
  • $this->words
  • $this->conditions
  • $this->simple
  • $this->matches

References SelectExtender\conditions(), Drupal\config(), db_or(), SearchQuery\EXPRESSIONS_IGNORED, SearchQuery\LOWER_CASE_OR, SearchQuery\parseWord(), and SearchQuery\searchExpression().

Referenced by SearchQuery\prepareAndNormalize(), and ViewsSearchQuery\publicParseSearchExpression().

Here is the call graph for this function:

Here is the caller graph for this function:

parseWord (   $word)
protected

Parses a word or phrase for parseQuery().

Splits a phrase into words. Adds its words to $this->words, if it is not already there. Returns a list containing the number of new words found, and the total number of words in the phrase.

References Drupal\config(), and Unicode\strlen().

Referenced by SearchQuery\parseSearchExpression().

Here is the call graph for this function:

Here is the caller graph for this function:

preExecute ( SelectInterface  $query = NULL)

{}

References SearchQuery\prepareAndNormalize().

Referenced by SearchQuery\execute().

Here is the call graph for this function:

Here is the caller graph for this function:

prepareAndNormalize ( )

Prepares the query and calculates the normalization factor.

After the query is normalized the keywords are weighted to give the results a relevancy score. The query is ready for execution after this.

Error and warning conditions can apply. Call getStatus() after calling this method to retrieve them.

Returns
bool TRUE if at least one keyword matched the search index; FALSE if not.

References SelectExtender\condition(), SelectExtender\conditions(), db_or(), SelectExtender\having(), SelectExtender\join(), SearchQuery\NO_KEYWORD_MATCHES, SearchQuery\NO_POSITIVE_KEYWORDS, and SearchQuery\parseSearchExpression().

Referenced by SearchQuery\countQuery(), and SearchQuery\preExecute().

Here is the call graph for this function:

Here is the caller graph for this function:

searchExpression (   $expression,
  $type 
)

Sets the search query expression.

Parameters
string$expressionA search string, which can contain keywords and options.
string$typeThe search type. This maps to {search_index}.type in the database.
Returns
$this

References SelectExtender\addTag(), SelectExtender\conditions(), and db_and().

Referenced by SearchQuery\parseSearchExpression().

Here is the call graph for this function:

Here is the caller graph for this function:

Field Documentation

const EXPRESSIONS_IGNORED = 2

Indicates that part of the search expression was ignored.

To prevent Denial of Service attacks, only ::config('search.settings')->get('and_or_limit') expressions (positive keywords, phrases, negative keywords) are allowed; this flag indicates that expressions existed past that limit and they were removed.

See Also
SearchQuery::getStatus()

Referenced by SearchQuery\parseSearchExpression().

const LOWER_CASE_OR = 4

Indicates that lower-case "or" was in the search expression.

The word "or" in lower case was found in the search expression. This probably means someone was trying to do an OR search but used lower-case instead of upper-case.

See Also
SearchQuery::getStatus()

Referenced by SearchQuery\parseSearchExpression().

const NO_KEYWORD_MATCHES = 8

Indicates that no positive keyword matches were found.

See Also
SearchQuery::getStatus()

Referenced by SearchQuery\prepareAndNormalize().

const NO_POSITIVE_KEYWORDS = 1

Indicates no positive keywords were in the search expression.

Positive keywords are words that are searched for, as opposed to negative keywords, which are words that are excluded. To count as a keyword, a word must be at least ::config('search.settings')->get('index.minimum_word_size') characters.

See Also
SearchQuery::getStatus()

Referenced by SearchQuery\prepareAndNormalize().


The documentation for this class was generated from the following file: