class SolrBaseQuery in Apache Solr Search 6.3
Same name and namespace in other branches
- 8 Solr_Base_Query.php \SolrBaseQuery
- 7 Solr_Base_Query.php \SolrBaseQuery
Hierarchy
- class \SolrFilterSubQuery
- class \SolrBaseQuery implements DrupalSolrQueryInterface
Expanded class hierarchy of SolrBaseQuery
1 string reference to 'SolrBaseQuery'
- apachesolr_drupal_query in ./
apachesolr.module - Factory function for query objects.
File
- ./
Solr_Base_Query.php, line 252 - This class allows you to make operations on a query that will be sent to Apache Solr. methods such as adding and removing sorts, remove and replace parameters, adding and removing filters, getters and setters for various parameters and more
View source
class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterface {
/**
* The parameters that get sent to Solr.
*/
protected $params = array(
'start' => 0,
'rows' => 10,
'fq' => array(),
);
/**
* The search base path.
*/
protected $base_path;
protected $field_map = array();
/**
* DrupalApacheSolrService object
*/
protected $solr;
// The array keys must always be real Solr index fields.
protected $available_sorts;
/**
* The query name is used to construct a searcher string. Mostly the
* environment id
*/
protected $name;
protected $context = array();
// Makes sure we always have a valid sort.
protected $solrsort = array(
'#name' => 'score',
'#direction' => 'desc',
);
// A flag to allow the search to be aborted.
public $abort_search = FALSE;
// A flag to check if need to retrieve another page of the result set
public $page = 0;
/**
* @param $name
* The search name, used for finding the correct blocks and other config.
* Typically "apachesolr".
*
* @param $solr
* An instantiated DrupalApacheSolrService Object.
* Can be instantiated from apachesolr_get_solr().
*
* @param $params
* Array of params to initialize the object (typically 'q' and 'fq').
*
* @param $sortstring
* Visible string telling solr how to sort - added to GET query params.
*
* @param $base_path
* The search base path (without the keywords) for this query, without trailing slash.
*/
function __construct($name, $solr, array $params = array(), $sortstring = '', $base_path = '', $context = array()) {
parent::__construct();
$this->name = $name;
$this->solr = $solr;
$this
->addContext((array) $context);
$this
->addParams((array) $params);
$this->available_sorts = $this
->defaultSorts();
$this->sortstring = trim($sortstring);
$this
->parseSortString();
$this->base_path = $base_path;
}
protected function defaultSorts() {
return array(
'score' => array(
'title' => t('Relevancy'),
'default' => 'desc',
),
'sort_label' => array(
'title' => t('Title'),
'default' => 'asc',
),
'bundle' => array(
'title' => t('Type'),
'default' => 'asc',
),
'sort_name' => array(
'title' => t('Author'),
'default' => 'asc',
),
'ds_created' => array(
'title' => t('Date'),
'default' => 'desc',
),
);
}
/**
* Get query name.
*/
public function getName() {
return $this->name;
}
/**
* Get query searcher name (for facetapi, views, pages, etc).
*/
public function getSearcher() {
return $this->name . '@' . $this->solr
->getId();
}
/**
* Get context values.
*/
public function getContext() {
return $this->context;
}
/**
* Set context value.
*/
public function addContext(array $context) {
foreach ($context as $k => $v) {
$this->context[$k] = $v;
}
// The env_id must match that of the actual $solr object
$this->context['env_id'] = $this->solr
->getId();
return $this->context;
}
protected $single_value_params = array(
'q' => TRUE,
// http://wiki.apache.org/solr/SearchHandler#q
'q.op' => TRUE,
// http://wiki.apache.org/solr/SearchHandler#q.op
'q.alt' => TRUE,
// http://wiki.apache.org/solr/SearchHandler#q
'df' => TRUE,
'qt' => TRUE,
'defType' => TRUE,
'timeAllowed' => TRUE,
'omitHeader' => TRUE,
'debugQuery' => TRUE,
'start' => TRUE,
'rows' => TRUE,
'stats' => TRUE,
'facet' => TRUE,
'facet.prefix' => TRUE,
'facet.limit' => TRUE,
'facet.offset' => TRUE,
'facet.mincount' => TRUE,
'facet.missing' => TRUE,
'facet.method' => TRUE,
'facet.enum.cache.minDf' => TRUE,
'facet.date.start' => TRUE,
'facet.date.end' => TRUE,
'facet.date.gap' => TRUE,
'facet.date.hardend' => TRUE,
'facet.date.other' => TRUE,
'facet.date.include' => TRUE,
'hl' => TRUE,
'hl.snippets' => TRUE,
'hl.fragsize' => TRUE,
'hl.mergeContiguous' => TRUE,
'hl.requireFieldMatch' => TRUE,
'hl.maxAnalyzedChars' => TRUE,
'hl.alternateField' => TRUE,
'hl.maxAlternateFieldLength' => TRUE,
'hl.formatter' => TRUE,
'hl.simple.pre/hl.simple.post' => TRUE,
'hl.fragmenter' => TRUE,
'hl.fragListBuilder' => TRUE,
'hl.fragmentsBuilder' => TRUE,
'hl.useFastVectorHighlighter' => TRUE,
'hl.usePhraseHighlighter' => TRUE,
'hl.highlightMultiTerm' => TRUE,
'hl.regex.slop' => TRUE,
'hl.regex.pattern' => TRUE,
'hl.regex.maxAnalyzedChars' => TRUE,
'spellcheck' => TRUE,
);
public function getParam($name) {
if ($name == 'fq') {
return $this
->rebuildFq();
}
$empty = isset($this->single_value_params[$name]) ? NULL : array();
return isset($this->params[$name]) ? $this->params[$name] : $empty;
}
public function getParams() {
$params = $this->params;
$params['fq'] = $this
->rebuildFq();
return $params;
}
public function getSolrParams() {
$params = $this
->getParams();
// For certain fields Solr prefers a comma separated list.
foreach (array(
'fl',
'hl.fl',
'sort',
'mlt.fl',
) as $name) {
if (isset($params[$name])) {
$params[$name] = implode(',', $params[$name]);
}
}
return $params;
}
protected function addFq($string, $index = NULL) {
$string = trim($string);
$local = '';
$exclude = FALSE;
$name = NULL;
$value = NULL;
// Check if we are dealing with an exclude
if (preg_match('/^-(.*)/', $string, $matches)) {
$exclude = TRUE;
$string = $matches[1];
}
// If {!something} is found as first character then this is a local value
if (preg_match('/\\{!([^}]+)\\}(.*)/', $string, $matches)) {
$local = $matches[1];
$string = $matches[2];
}
// Anything that has a name and value
// check if we have a : in the string
if (strstr($string, ':')) {
list($name, $value) = explode(":", $string, 2);
}
else {
$value = $string;
}
$this
->addFilter($name, $value, $exclude, $local);
return $this;
}
public function addParam($name, $value) {
if (isset($this->single_value_params[$name])) {
if (is_array($value)) {
$value = end($value);
}
$this->params[$name] = $this
->normalizeParamValue($value);
return $this;
}
// We never actually populate $this->params['fq']. Instead
// we manage everything via the filter methods.
if ($name == 'fq') {
if (is_array($value)) {
array_walk_recursive($value, array(
$this,
'addFq',
));
return $this;
}
else {
return $this
->addFq($value);
}
}
if (!isset($this->params[$name])) {
$this->params[$name] = array();
}
if (!is_array($value)) {
// Convert to array for array_map.
$param_values = array(
$value,
);
}
else {
// Convert to a numerically keyed array.
$param_values = array_values($value);
}
$this->params[$name] = array_merge($this->params[$name], array_map(array(
$this,
'normalizeParamValue',
), $param_values));
return $this;
}
protected function normalizeParamValue($value) {
// Convert boolean to string.
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
// Convert to trimmed string.
return trim($value);
}
public function addParams(array $params) {
foreach ($params as $name => $value) {
$this
->addParam($name, $value);
}
return $this;
}
public function removeParam($name) {
unset($this->params[$name]);
if ($name == 'fq') {
$this->fields = array();
$this->subqueries = array();
}
return $this;
}
public function replaceParam($name, $value) {
$this
->removeParam($name);
return $this
->addParam($name, $value);
}
/**
* Handles aliases for field to make nicer URLs.
*
* @param $field_map
* An array keyed with real Solr index field names with the alias as value.
*
* @return DrupalSolrQueryInterface
* The called object.
*/
public function addFieldAliases($field_map) {
$this->field_map = array_merge($this->field_map, $field_map);
// We have to re-parse the filters.
$this
->parseSortString();
return $this;
}
public function getFieldAliases() {
return $this->field_map;
}
public function clearFieldAliases() {
$this->field_map = array();
// We have to re-parse the filters.
$this
->parseSortString();
return $this;
}
protected function parseSortString() {
// Substitute any field aliases with real field names.
$sortstring = strtr($this->sortstring, $this->field_map);
// Score is a special case - it's the default sort for Solr.
if ('' == $sortstring || 'score desc' == $sortstring) {
$this->solrsort['#name'] = 'score';
$this->solrsort['#direction'] = 'desc';
unset($this->params['sort']);
}
else {
// Validate and set sort parameter
$fields = implode('|', array_keys($this->available_sorts));
if (preg_match('/^(?:(' . $fields . ') (asc|desc),?)+$/', $sortstring, $matches)) {
// We only use the last match.
$this->solrsort['#name'] = $matches[1];
$this->solrsort['#direction'] = $matches[2];
$this->params['sort'] = array(
$sortstring,
);
}
}
}
public function getAvailableSorts() {
return $this->available_sorts;
}
public function setAvailableSort($name, $sort) {
// We expect non-aliased sorts to be added.
$this->available_sorts[$name] = $sort;
// Re-parse the sortstring.
$this
->parseSortString();
return $this;
}
public function setAvailableSorts($sorts) {
// We expect a complete array of valid sorts.
$this->available_sorts = $sorts;
$this
->parseSortString();
return $this;
}
public function removeAvailableSort($name) {
unset($this->available_sorts[$name]);
// Re-parse the sortstring.
$this
->parseSortString();
return $this;
}
public function getSolrsort() {
return $this->solrsort;
}
public function setSolrsort($name, $direction) {
$this->sortstring = trim($name) . ' ' . trim($direction);
$this
->parseSortString();
return $this;
}
public function getPath($new_keywords = NULL) {
if (isset($new_keywords)) {
return $this->base_path . '/' . $new_keywords;
}
elseif ($this
->getParam('q')) {
return $this->base_path . '/' . $this
->getParam('q');
}
else {
return $this->base_path;
}
}
public function getSolrsortUrlQuery() {
$queryvalues = array();
$solrsort = $this->solrsort;
if ($solrsort && $solrsort['#name'] != 'score') {
if (isset($this->field_map[$solrsort['#name']])) {
$solrsort['#name'] = $this->field_map[$solrsort['#name']];
}
$queryvalues['solrsort'] = $solrsort['#name'] . ' ' . $solrsort['#direction'];
}
else {
// Return to default relevancy sort.
unset($queryvalues['solrsort']);
}
return $queryvalues;
}
public function search($keys = NULL) {
if ($this->abort_search) {
return NULL;
}
return $this->solr
->search($keys, $this
->getSolrParams());
}
public function solr($method) {
return $this->solr
->{$method}();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SolrBaseQuery:: |
public | property | ||
SolrBaseQuery:: |
protected | property | ||
SolrBaseQuery:: |
protected | property | The search base path. | |
SolrBaseQuery:: |
protected | property | ||
SolrBaseQuery:: |
protected | property | ||
SolrBaseQuery:: |
protected | property | The query name is used to construct a searcher string. Mostly the environment id | |
SolrBaseQuery:: |
public | property | ||
SolrBaseQuery:: |
protected | property | The parameters that get sent to Solr. | |
SolrBaseQuery:: |
protected | property | ||
SolrBaseQuery:: |
protected | property | DrupalApacheSolrService object | |
SolrBaseQuery:: |
protected | property | ||
SolrBaseQuery:: |
public | function |
Set context value. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Handles aliases for field to make nicer URLs. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
protected | function | ||
SolrBaseQuery:: |
public | function |
Adds a param to be sent when running the Solr search. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Adds multiple params to be sent when running the Solr search. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
protected | function | ||
SolrBaseQuery:: |
public | function |
Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Get context values. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Get query name. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Gets the value of a parameter. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Gets all parameters in normalized form. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Returns the search path (including the search keywords). Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Get query searcher name (for facetapi, views, pages, etc). Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Gets parameters in a form suitable for use in a Solr query. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Gets the current sort. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Returns an array representing the URL query string for the current sort. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
protected | function | ||
SolrBaseQuery:: |
protected | function | ||
SolrBaseQuery:: |
public | function |
Removes an available sort. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Removes all values for one Solr param. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Replaces a param to be sent when running the Solr search. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Sends the search request to Solr, unless $query->abort_search is TRUE. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Adds an available sort. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function | ||
SolrBaseQuery:: |
public | function |
Sets the sort. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
public | function |
Calls a method, without arguments, on the Solr object with which the query
object was initialized. Overrides DrupalSolrQueryInterface:: |
|
SolrBaseQuery:: |
function |
Overrides SolrFilterSubQuery:: |
||
SolrFilterSubQuery:: |
protected | property | A keyed array where the key is a position integer and the value is an array with #name and #value properties. Each value is a used for filter queries, e.g. array('#name' => 'is_uid', '#value' => 0) for anonymous… | |
SolrFilterSubQuery:: |
public | property | Each query/subquery will have a unique ID. | |
SolrFilterSubQuery:: |
protected static | property | Static shared by all instances, used to increment ID numbers. | |
SolrFilterSubQuery:: |
public | property | ||
SolrFilterSubQuery:: |
protected | property | An array of subqueries. | |
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
protected | function | Builds a set of filter queries from $this->fields and all subqueries. | |
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
public | function | ||
SolrFilterSubQuery:: |
protected | function | ||
SolrFilterSubQuery:: |
public static | function | Make sure our query matches the pattern name:value or name:"value" Make sure that if we are ranges we use name:[ AND ] allowed inputs : a. bundle:story b. date:[1970-12-31T23:59:59Z TO NOW] Split the text in 4 different parts 1. name, eg.:… | |
SolrFilterSubQuery:: |
function |