upadted to 1.8
This commit is contained in:
+179
-316
@@ -9,7 +9,7 @@
|
||||
interface SearchApiQueryInterface {
|
||||
|
||||
/**
|
||||
* Constructor used when creating SearchApiQueryInterface objects.
|
||||
* Constructs a new search query.
|
||||
*
|
||||
* @param SearchApiIndex $index
|
||||
* The index the query should be executed on.
|
||||
@@ -46,6 +46,8 @@ interface SearchApiQueryInterface {
|
||||
public function __construct(SearchApiIndex $index, array $options = array());
|
||||
|
||||
/**
|
||||
* Retrieves the parse modes supported by this query class.
|
||||
*
|
||||
* @return array
|
||||
* An associative array of parse modes recognized by objects of this class.
|
||||
* The keys are the parse modes' ids, values are associative arrays
|
||||
@@ -56,9 +58,9 @@ interface SearchApiQueryInterface {
|
||||
public function parseModes();
|
||||
|
||||
/**
|
||||
* Method for creating a filter to use with this query object.
|
||||
* Creates a new filter to use with this query object.
|
||||
*
|
||||
* @param $conjunction
|
||||
* @param string $conjunction
|
||||
* The conjunction to use for the filter - either 'AND' or 'OR'.
|
||||
*
|
||||
* @return SearchApiQueryFilterInterface
|
||||
@@ -67,10 +69,12 @@ interface SearchApiQueryInterface {
|
||||
public function createFilter($conjunction = 'AND');
|
||||
|
||||
/**
|
||||
* Sets the keys to search for. If this method is not called on the query
|
||||
* before execution, this will be a filter-only query.
|
||||
* Sets the keys to search for.
|
||||
*
|
||||
* @param $keys
|
||||
* If this method is not called on the query before execution, this will be a
|
||||
* filter-only query.
|
||||
*
|
||||
* @param array|string|null $keys
|
||||
* A string with the unparsed search keys, or NULL to use no search keys.
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
@@ -79,18 +83,20 @@ interface SearchApiQueryInterface {
|
||||
public function keys($keys = NULL);
|
||||
|
||||
/**
|
||||
* Sets the fields that will be searched for the search keys. If this is not
|
||||
* called, all fulltext fields should be searched.
|
||||
* Sets the fields that will be searched for the search keys.
|
||||
*
|
||||
* If this is not called, all fulltext fields will be searched.
|
||||
*
|
||||
* @param array $fields
|
||||
* An array containing fulltext fields that should be searched.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If one of the fields isn't of type "text".
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
* The called object.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If one of the fields isn't of type "text".
|
||||
*/
|
||||
// @todo Allow calling with NULL.
|
||||
public function fields(array $fields);
|
||||
|
||||
/**
|
||||
@@ -105,13 +111,13 @@ interface SearchApiQueryInterface {
|
||||
public function filter(SearchApiQueryFilterInterface $filter);
|
||||
|
||||
/**
|
||||
* Add a new ($field $operator $value) condition filter.
|
||||
* Adds a new ($field $operator $value) condition filter.
|
||||
*
|
||||
* @param $field
|
||||
* @param string $field
|
||||
* The field to filter on, e.g. 'title'.
|
||||
* @param $value
|
||||
* @param mixed $value
|
||||
* The value the field should have (or be related to by the operator).
|
||||
* @param $operator
|
||||
* @param string $operator
|
||||
* The operator to use for checking the constraint. The following operators
|
||||
* are supported for primitive types: "=", "<>", "<", "<=", ">=", ">". They
|
||||
* have the same semantics as the corresponding SQL operators.
|
||||
@@ -127,31 +133,34 @@ interface SearchApiQueryInterface {
|
||||
public function condition($field, $value, $operator = '=');
|
||||
|
||||
/**
|
||||
* Add a sort directive to this search query. If no sort is manually set, the
|
||||
* results will be sorted descending by relevance.
|
||||
* Adds a sort directive to this search query.
|
||||
*
|
||||
* @param $field
|
||||
* If no sort is manually set, the results will be sorted descending by
|
||||
* relevance.
|
||||
*
|
||||
* @param string $field
|
||||
* The field to sort by. The special fields 'search_api_relevance' (sort by
|
||||
* relevance) and 'search_api_id' (sort by item id) may be used.
|
||||
* @param $order
|
||||
* @param string $order
|
||||
* The order to sort items in - either 'ASC' or 'DESC'.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If the field is multi-valued or of a fulltext type.
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
* The called object.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If the field is multi-valued or of a fulltext type.
|
||||
*/
|
||||
public function sort($field, $order = 'ASC');
|
||||
|
||||
/**
|
||||
* Adds a range of results to return. This will be saved in the query's
|
||||
* options. If called without parameters, this will remove all range
|
||||
* restrictions previously set.
|
||||
* Adds a range of results to return.
|
||||
*
|
||||
* @param $offset
|
||||
* This will be saved in the query's options. If called without parameters,
|
||||
* this will remove all range restrictions previously set.
|
||||
*
|
||||
* @param int|null $offset
|
||||
* The zero-based offset of the first result returned.
|
||||
* @param $limit
|
||||
* @param int|null $limit
|
||||
* The number of results to return.
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
@@ -206,24 +215,29 @@ interface SearchApiQueryInterface {
|
||||
public function preExecute();
|
||||
|
||||
/**
|
||||
* Postprocess the search results before they are returned.
|
||||
* Postprocesses the search results before they are returned.
|
||||
*
|
||||
* This method should always be called by execute() and contain all necessary
|
||||
* operations after the results are returned from the server.
|
||||
*
|
||||
* @param array $results
|
||||
* The results returned by the server, which may be altered.
|
||||
* The results returned by the server, which may be altered. The data
|
||||
* structure is the same as returned by execute().
|
||||
*/
|
||||
public function postExecute(array &$results);
|
||||
|
||||
/**
|
||||
* Retrieves the index associated with this search.
|
||||
*
|
||||
* @return SearchApiIndex
|
||||
* The search index this query should be executed on.
|
||||
*/
|
||||
public function getIndex();
|
||||
|
||||
/**
|
||||
* @return
|
||||
* Retrieves the search keys for this query.
|
||||
*
|
||||
* @return array|string|null
|
||||
* This object's search keys - either a string or an array specifying a
|
||||
* complex search expression.
|
||||
* An array will contain a '#conjunction' key specifying the conjunction
|
||||
@@ -236,41 +250,59 @@ interface SearchApiQueryInterface {
|
||||
* the terms in the array should be excluded; with the "OR" conjunction and
|
||||
* negation, all results containing one or more of the terms in the array
|
||||
* should be excluded.
|
||||
*
|
||||
* @see keys()
|
||||
*/
|
||||
public function &getKeys();
|
||||
|
||||
/**
|
||||
* @return
|
||||
* Retrieves the unparsed search keys for this query as originally entered.
|
||||
*
|
||||
* @return array|string|null
|
||||
* The unprocessed search keys, exactly as passed to this object. Has the
|
||||
* same format as getKeys().
|
||||
* same format as the return value of getKeys().
|
||||
*
|
||||
* @see keys()
|
||||
*/
|
||||
public function getOriginalKeys();
|
||||
|
||||
/**
|
||||
* Retrieves the fulltext fields that will be searched for the search keys.
|
||||
*
|
||||
* @return array
|
||||
* An array containing the fields that should be searched for the search
|
||||
* keys.
|
||||
*
|
||||
* @see fields()
|
||||
*/
|
||||
public function &getFields();
|
||||
|
||||
/**
|
||||
* Retrieves the filter object associated with this search query.
|
||||
*
|
||||
* @return SearchApiQueryFilterInterface
|
||||
* This object's associated filter object.
|
||||
*/
|
||||
public function getFilter();
|
||||
|
||||
/**
|
||||
* Retrieves the sorts set for this query.
|
||||
*
|
||||
* @return array
|
||||
* An array specifying the sort order for this query. Array keys are the
|
||||
* field names in order of importance, the values are the respective order
|
||||
* in which to sort the results according to the field.
|
||||
*
|
||||
* @see sort()
|
||||
*/
|
||||
public function &getSort();
|
||||
|
||||
/**
|
||||
* @param $name string
|
||||
* Retrieves an option set on this search query.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of an option.
|
||||
* @param $default mixed
|
||||
* @param mixed $default
|
||||
* The value to return if the specified option is not set.
|
||||
*
|
||||
* @return mixed
|
||||
@@ -279,6 +311,8 @@ interface SearchApiQueryInterface {
|
||||
public function getOption($name, $default = NULL);
|
||||
|
||||
/**
|
||||
* Sets an option for this search query.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of an option.
|
||||
* @param mixed $value
|
||||
@@ -289,6 +323,11 @@ interface SearchApiQueryInterface {
|
||||
public function setOption($name, $value);
|
||||
|
||||
/**
|
||||
* Retrieves all options set for this search query.
|
||||
*
|
||||
* The return value is a reference to the options so they can also be altered
|
||||
* this way.
|
||||
*
|
||||
* @return array
|
||||
* An associative array of query options.
|
||||
*/
|
||||
@@ -297,7 +336,7 @@ interface SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard implementation of SearchApiQueryInterface.
|
||||
* Provides a standard implementation of the SearchApiQueryInterface.
|
||||
*/
|
||||
class SearchApiQuery implements SearchApiQueryInterface {
|
||||
|
||||
@@ -351,44 +390,14 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* Count for providing a unique ID.
|
||||
* Flag for whether preExecute() was already called for this query.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $count = 0;
|
||||
protected $pre_execute = FALSE;
|
||||
|
||||
/**
|
||||
* Constructor for SearchApiQuery objects.
|
||||
*
|
||||
* @param SearchApiIndex $index
|
||||
* The index the query should be executed on.
|
||||
* @param array $options
|
||||
* Associative array of options configuring this query. Recognized options
|
||||
* are:
|
||||
* - conjunction: The type of conjunction to use for this query - either
|
||||
* 'AND' or 'OR'. 'AND' by default. This only influences the search keys,
|
||||
* filters will always use AND by default.
|
||||
* - 'parse mode': The mode with which to parse the $keys variable, if it
|
||||
* is set and not already an array. See SearchApiQuery::parseModes() for
|
||||
* recognized parse modes.
|
||||
* - languages: The languages to search for, as an array of language IDs.
|
||||
* If not specified, all languages will be searched. Language-neutral
|
||||
* content (LANGUAGE_NONE) is always searched.
|
||||
* - offset: The position of the first returned search results relative to
|
||||
* the whole result in the index.
|
||||
* - limit: The maximum number of search results to return. -1 means no
|
||||
* limit.
|
||||
* - 'filter class': Can be used to change the SearchApiQueryFilterInterface
|
||||
* implementation to use.
|
||||
* - 'search id': A string that will be used as the identifier when storing
|
||||
* this search in the Search API's static cache.
|
||||
* - search_api_access_account: The account which will be used for entity
|
||||
* access checks, if available and enabled for the index.
|
||||
* - search_api_bypass_access: If set to TRUE, entity access checks will be
|
||||
* skipped, even if enabled for the index.
|
||||
* All options are optional. Third-party modules might define and use other
|
||||
* options not listed here.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If a search on that index (or with those options) won't be possible.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(SearchApiIndex $index, array $options = array()) {
|
||||
if (empty($index->options['fields'])) {
|
||||
@@ -415,12 +424,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An associative array of parse modes recognized by objects of this class.
|
||||
* The keys are the parse modes' ids, values are associative arrays
|
||||
* containing the following entries:
|
||||
* - name: The translated name of the parse mode.
|
||||
* - description: (optional) A translated text describing the parse mode.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function parseModes() {
|
||||
$modes['direct'] = array(
|
||||
@@ -442,10 +446,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the keys string according to the $mode parameter.
|
||||
*
|
||||
* @return
|
||||
* The parsed keys. Either a string or an array.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function parseKeys($keys, $mode) {
|
||||
if ($keys === NULL || is_array($keys)) {
|
||||
@@ -460,7 +461,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
return array('#conjunction' => $this->options['conjunction'], $keys);
|
||||
|
||||
case 'terms':
|
||||
$ret = explode(' ', $keys);
|
||||
$ret = preg_split('/\s+/u', $keys);
|
||||
$quoted = FALSE;
|
||||
$str = '';
|
||||
foreach ($ret as $k => $v) {
|
||||
@@ -500,13 +501,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for creating a filter to use with this query object.
|
||||
*
|
||||
* @param $conjunction
|
||||
* The conjunction to use for the filter - either 'AND' or 'OR'.
|
||||
*
|
||||
* @return SearchApiQueryFilterInterface
|
||||
* A filter object that is set to use the specified conjunction.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createFilter($conjunction = 'AND') {
|
||||
$filter_class = $this->options['filter class'];
|
||||
@@ -514,14 +509,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the keys to search for. If this method is not called on the query
|
||||
* before execution, this will be a filter-only query.
|
||||
*
|
||||
* @param $keys
|
||||
* A string with the unparsed search keys, or NULL to use no search keys.
|
||||
*
|
||||
* @return SearchApiQuery
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function keys($keys = NULL) {
|
||||
$this->orig_keys = $keys;
|
||||
@@ -534,17 +522,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* Sets the fields that will be searched for the search keys. If this is not
|
||||
* called, all fulltext fields should be searched.
|
||||
*
|
||||
* @param array $fields
|
||||
* An array containing fulltext fields that should be searched.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If one of the fields isn't of type "text".
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fields(array $fields) {
|
||||
$fulltext_fields = $this->index->getFulltextFields();
|
||||
@@ -556,13 +534,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a subfilter to this query's filter.
|
||||
*
|
||||
* @param SearchApiQueryFilterInterface $filter
|
||||
* A SearchApiQueryFilter object that should be added as a subfilter.
|
||||
*
|
||||
* @return SearchApiQuery
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function filter(SearchApiQueryFilterInterface $filter) {
|
||||
$this->filter->filter($filter);
|
||||
@@ -570,24 +542,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new ($field $operator $value) condition filter.
|
||||
*
|
||||
* @param $field
|
||||
* The field to filter on, e.g. 'title'.
|
||||
* @param $value
|
||||
* The value the field should have (or be related to by the operator).
|
||||
* @param $operator
|
||||
* The operator to use for checking the constraint. The following operators
|
||||
* are supported for primitive types: "=", "<>", "<", "<=", ">=", ">". They
|
||||
* have the same semantics as the corresponding SQL operators.
|
||||
* If $field is a fulltext field, $operator can only be "=" or "<>", which
|
||||
* are in this case interpreted as "contains" or "doesn't contain",
|
||||
* respectively.
|
||||
* If $value is NULL, $operator also can only be "=" or "<>", meaning the
|
||||
* field must have no or some value, respectively.
|
||||
*
|
||||
* @return SearchApiQuery
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function condition($field, $value, $operator = '=') {
|
||||
$this->filter->condition($field, $value, $operator);
|
||||
@@ -595,20 +550,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a sort directive to this search query. If no sort is manually set, the
|
||||
* results will be sorted descending by relevance.
|
||||
*
|
||||
* @param $field
|
||||
* The field to sort by. The special fields 'search_api_relevance' (sort by
|
||||
* relevance) and 'search_api_id' (sort by item id) may be used.
|
||||
* @param $order
|
||||
* The order to sort items in - either 'ASC' or 'DESC'.
|
||||
*
|
||||
* @throws SearchApiException
|
||||
* If the field is multi-valued or of a fulltext type.
|
||||
*
|
||||
* @return SearchApiQuery
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function sort($field, $order = 'ASC') {
|
||||
$fields = $this->index->options['fields'];
|
||||
@@ -629,17 +571,7 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a range of results to return. This will be saved in the query's
|
||||
* options. If called without parameters, this will remove all range
|
||||
* restrictions previously set.
|
||||
*
|
||||
* @param $offset
|
||||
* The zero-based offset of the first result returned.
|
||||
* @param $limit
|
||||
* The number of results to return.
|
||||
*
|
||||
* @return SearchApiQueryInterface
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function range($offset = NULL, $limit = NULL) {
|
||||
$this->options['offset'] = $offset;
|
||||
@@ -649,42 +581,9 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
|
||||
|
||||
/**
|
||||
* Executes this search query.
|
||||
*
|
||||
* @return array
|
||||
* An associative array containing the search results. The following keys
|
||||
* are standardized:
|
||||
* - 'result count': The overall number of results for this query, without
|
||||
* range restrictions. Might be approximated, for large numbers.
|
||||
* - results: An array of results, ordered as specified. The array keys are
|
||||
* the items' IDs, values are arrays containing the following keys:
|
||||
* - id: The item's ID.
|
||||
* - score: A float measuring how well the item fits the search.
|
||||
* - fields: (optional) If set, an array containing some field values
|
||||
* already ready-to-use. This allows search engines (or postprocessors)
|
||||
* to store extracted fields so other modules don't have to extract them
|
||||
* again. This fields should always be checked by modules that want to
|
||||
* use field contents of the result items.
|
||||
* - entity: (optional) If set, the fully loaded result item. This field
|
||||
* should always be used by modules using search results, to avoid
|
||||
* duplicate item loads.
|
||||
* - excerpt: (optional) If set, an HTML text containing highlighted
|
||||
* portions of the fulltext that match the query.
|
||||
* - warnings: A numeric array of translated warning messages that may be
|
||||
* displayed to the user.
|
||||
* - ignored: A numeric array of search keys that were ignored for this
|
||||
* search (e.g., because of being too short or stop words).
|
||||
* - performance: An associative array with the time taken (as floats, in
|
||||
* seconds) for specific parts of the search execution:
|
||||
* - complete: The complete runtime of the query.
|
||||
* - hooks: Hook invocations and other client-side preprocessing.
|
||||
* - preprocessing: Preprocessing of the service class.
|
||||
* - execution: The actual query to the search server, in whatever form.
|
||||
* - postprocessing: Preparing the results for returning.
|
||||
* Additional metadata may be returned in other keys. Only 'result count'
|
||||
* and 'result' always have to be set, all other entries are optional.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public final function execute() {
|
||||
public function execute() {
|
||||
$start = microtime(TRUE);
|
||||
|
||||
// Prepare the query for execution by the server.
|
||||
@@ -711,7 +610,12 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for adding a language filter.
|
||||
* Adds language filters for the query.
|
||||
*
|
||||
* Internal helper function.
|
||||
*
|
||||
* @param array $languages
|
||||
* The languages for which results should be returned.
|
||||
*/
|
||||
protected function addLanguages(array $languages) {
|
||||
if (array_search(LANGUAGE_NONE, $languages) === FALSE) {
|
||||
@@ -755,37 +659,32 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the query object for the search.
|
||||
*
|
||||
* This method should always be called by execute() and contain all necessary
|
||||
* operations before the query is passed to the server's search() method.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function preExecute() {
|
||||
// Add filter for languages.
|
||||
if (isset($this->options['languages'])) {
|
||||
$this->addLanguages($this->options['languages']);
|
||||
// Make sure to only execute this once per query.
|
||||
if (!$this->pre_execute) {
|
||||
$this->pre_execute = TRUE;
|
||||
// Add filter for languages.
|
||||
if (isset($this->options['languages'])) {
|
||||
$this->addLanguages($this->options['languages']);
|
||||
}
|
||||
|
||||
// Add fulltext fields, unless set
|
||||
if ($this->fields === NULL) {
|
||||
$this->fields = $this->index->getFulltextFields();
|
||||
}
|
||||
|
||||
// Preprocess query.
|
||||
$this->index->preprocessSearchQuery($this);
|
||||
|
||||
// Let modules alter the query.
|
||||
drupal_alter('search_api_query', $this);
|
||||
}
|
||||
|
||||
// Add fulltext fields, unless set
|
||||
if ($this->fields === NULL) {
|
||||
$this->fields = $this->index->getFulltextFields();
|
||||
}
|
||||
|
||||
// Preprocess query.
|
||||
$this->index->preprocessSearchQuery($this);
|
||||
|
||||
// Let modules alter the query.
|
||||
drupal_alter('search_api_query', $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Postprocess the search results before they are returned.
|
||||
*
|
||||
* This method should always be called by execute() and contain all necessary
|
||||
* operations after the results are returned from the server.
|
||||
*
|
||||
* @param array $results
|
||||
* The results returned by the server, which may be altered.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postExecute(array &$results) {
|
||||
// Postprocess results.
|
||||
@@ -793,86 +692,56 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SearchApiIndex
|
||||
* The search index this query will be executed on.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getIndex() {
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* This object's search keys - either a string or an array specifying a
|
||||
* complex search expression.
|
||||
* An array will contain a '#conjunction' key specifying the conjunction
|
||||
* type, and search strings or nested expression arrays at numeric keys.
|
||||
* Additionally, a '#negation' key might be present, which means – unless it
|
||||
* maps to a FALSE value – that the search keys contained in that array
|
||||
* should be negated, i.e. not be present in returned results. The negation
|
||||
* works on the whole array, not on each contained term individually – i.e.,
|
||||
* with the "AND" conjunction and negation, only results that contain all
|
||||
* the terms in the array should be excluded; with the "OR" conjunction and
|
||||
* negation, all results containing one or more of the terms in the array
|
||||
* should be excluded.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function &getKeys() {
|
||||
return $this->keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* The unprocessed search keys, exactly as passed to this object. Has the
|
||||
* same format as getKeys().
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getOriginalKeys() {
|
||||
return $this->orig_keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An array containing the fields that should be searched for the search
|
||||
* keys.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function &getFields() {
|
||||
return $this->fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SearchApiQueryFilterInterface
|
||||
* This object's associated filter object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilter() {
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An array specifying the sort order for this query. Array keys are the
|
||||
* field names in order of importance, the values are the respective order
|
||||
* in which to sort the results according to the field.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function &getSort() {
|
||||
return $this->sort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name string
|
||||
* The name of an option.
|
||||
*
|
||||
* @return mixed
|
||||
* The option with the specified name, if set, or NULL otherwise.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getOption($name, $default = NULL) {
|
||||
return array_key_exists($name, $this->options) ? $this->options[$name] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* The name of an option.
|
||||
* @param mixed $value
|
||||
* The new value of the option.
|
||||
*
|
||||
* @return The option's previous value.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setOption($name, $value) {
|
||||
$old = $this->getOption($name);
|
||||
@@ -881,28 +750,46 @@ class SearchApiQuery implements SearchApiQueryInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An associative array of query options.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function &getOptions() {
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the magic __sleep() method to avoid serializing the index.
|
||||
*/
|
||||
public function __sleep() {
|
||||
$this->index_id = $this->index->machine_name;
|
||||
$keys = get_object_vars($this);
|
||||
unset($keys['index']);
|
||||
return array_keys($keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the magic __wakeup() method to reload the query's index.
|
||||
*/
|
||||
public function __wakeup() {
|
||||
if (!isset($this->index) && !empty($this->index_id)) {
|
||||
$this->index = search_api_index_load($this->index_id);
|
||||
unset($this->index_id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface representing a search query filter, that filters on one or more
|
||||
* fields with a specific conjunction (AND or OR).
|
||||
* Represents a filter on a search query.
|
||||
*
|
||||
* Methods not noting otherwise will return the object itself, so calls can be
|
||||
* chained.
|
||||
* Filters apply conditions on one or more fields with a specific conjunction
|
||||
* (AND or OR) and may contain nested filters.
|
||||
*/
|
||||
interface SearchApiQueryFilterInterface {
|
||||
|
||||
/**
|
||||
* Constructs a new filter that uses the specified conjunction.
|
||||
*
|
||||
* @param $conjunction
|
||||
* @param string $conjunction
|
||||
* The conjunction to use for this filter - either 'AND' or 'OR'.
|
||||
*/
|
||||
public function __construct($conjunction = 'AND');
|
||||
@@ -910,7 +797,7 @@ interface SearchApiQueryFilterInterface {
|
||||
/**
|
||||
* Sets this filter's conjunction.
|
||||
*
|
||||
* @param $conjunction
|
||||
* @param string $conjunction
|
||||
* The conjunction to use for this filter - either 'AND' or 'OR'.
|
||||
*
|
||||
* @return SearchApiQueryFilterInterface
|
||||
@@ -921,7 +808,7 @@ interface SearchApiQueryFilterInterface {
|
||||
/**
|
||||
* Adds a subfilter.
|
||||
*
|
||||
* @param $filter
|
||||
* @param SearchApiQueryFilterInterface $filter
|
||||
* A SearchApiQueryFilterInterface object that should be added as a
|
||||
* subfilter.
|
||||
*
|
||||
@@ -931,13 +818,13 @@ interface SearchApiQueryFilterInterface {
|
||||
public function filter(SearchApiQueryFilterInterface $filter);
|
||||
|
||||
/**
|
||||
* Add a new ($field $operator $value) condition.
|
||||
* Adds a new ($field $operator $value) condition.
|
||||
*
|
||||
* @param $field
|
||||
* @param string $field
|
||||
* The field to filter on, e.g. 'title'.
|
||||
* @param $value
|
||||
* @param mixed $value
|
||||
* The value the field should have (or be related to by the operator).
|
||||
* @param $operator
|
||||
* @param string $operator
|
||||
* The operator to use for checking the constraint. The following operators
|
||||
* are supported for primitive types: "=", "<>", "<", "<=", ">=", ">". They
|
||||
* have the same semantics as the corresponding SQL operators.
|
||||
@@ -953,12 +840,16 @@ interface SearchApiQueryFilterInterface {
|
||||
public function condition($field, $value, $operator = '=');
|
||||
|
||||
/**
|
||||
* @return
|
||||
* Retrieves the conjunction used by this filter.
|
||||
*
|
||||
* @return string
|
||||
* The conjunction used by this filter - either 'AND' or 'OR'.
|
||||
*/
|
||||
public function getConjunction();
|
||||
|
||||
/**
|
||||
* Return all conditions and nested filters contained in this filter.
|
||||
*
|
||||
* @return array
|
||||
* An array containing this filter's subfilters. Each of these is either an
|
||||
* array (field, value, operator), or another SearchApiFilter object.
|
||||
@@ -968,24 +859,29 @@ interface SearchApiQueryFilterInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard implementation of SearchApiQueryFilterInterface.
|
||||
* Provides a standard implementation of SearchApiQueryFilterInterface.
|
||||
*/
|
||||
class SearchApiQueryFilter implements SearchApiQueryFilterInterface {
|
||||
|
||||
/**
|
||||
* Array containing subfilters. Each of these is either an array
|
||||
* (field, value, operator), or another SearchApiFilter object.
|
||||
* Array containing subfilters.
|
||||
*
|
||||
* Each of these is either an array (field, value, operator), or another
|
||||
* SearchApiFilter object.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filters;
|
||||
|
||||
/** String specifying this filter's conjunction ('AND' or 'OR'). */
|
||||
/**
|
||||
* String specifying this filter's conjunction ('AND' or 'OR').
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $conjunction;
|
||||
|
||||
/**
|
||||
* Constructs a new filter that uses the specified conjunction.
|
||||
*
|
||||
* @param $conjunction
|
||||
* The conjunction to use for this filter - either 'AND' or 'OR'.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct($conjunction = 'AND') {
|
||||
$this->setConjunction($conjunction);
|
||||
@@ -993,13 +889,7 @@ class SearchApiQueryFilter implements SearchApiQueryFilterInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this filter's conjunction.
|
||||
*
|
||||
* @param $conjunction
|
||||
* The conjunction to use for this filter - either 'AND' or 'OR'.
|
||||
*
|
||||
* @return SearchApiQueryFilter
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setConjunction($conjunction) {
|
||||
$this->conjunction = strtoupper(trim($conjunction)) == 'OR' ? 'OR' : 'AND';
|
||||
@@ -1007,14 +897,7 @@ class SearchApiQueryFilter implements SearchApiQueryFilterInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a subfilter.
|
||||
*
|
||||
* @param $filter
|
||||
* A SearchApiQueryFilterInterface object that should be added as a
|
||||
* subfilter.
|
||||
*
|
||||
* @return SearchApiQueryFilter
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function filter(SearchApiQueryFilterInterface $filter) {
|
||||
$this->filters[] = $filter;
|
||||
@@ -1022,24 +905,7 @@ class SearchApiQueryFilter implements SearchApiQueryFilterInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new ($field $operator $value) condition.
|
||||
*
|
||||
* @param $field
|
||||
* The field to filter on, e.g. 'title'.
|
||||
* @param $value
|
||||
* The value the field should have (or be related to by the operator).
|
||||
* @param $operator
|
||||
* The operator to use for checking the constraint. The following operators
|
||||
* are supported for primitive types: "=", "<>", "<", "<=", ">=", ">". They
|
||||
* have the same semantics as the corresponding SQL operators.
|
||||
* If $field is a fulltext field, $operator can only be "=" or "<>", which
|
||||
* are in this case interpreted as "contains" or "doesn't contain",
|
||||
* respectively.
|
||||
* If $value is NULL, $operator also can only be "=" or "<>", meaning the
|
||||
* field must have no or some value, respectively.
|
||||
*
|
||||
* @return SearchApiQueryFilter
|
||||
* The called object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function condition($field, $value, $operator = '=') {
|
||||
$this->filters[] = array($field, $value, $operator);
|
||||
@@ -1047,17 +913,14 @@ class SearchApiQueryFilter implements SearchApiQueryFilterInterface {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* The conjunction used by this filter - either 'AND' or 'OR'.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConjunction() {
|
||||
return $this->conjunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* An array containing this filter's subfilters. Each of these is either an
|
||||
* array (field, value, operator), or another SearchApiFilter object.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function &getFilters() {
|
||||
return $this->filters;
|
||||
|
||||
Reference in New Issue
Block a user