3 * This class allows you to make operations on a query that will be sent to
4 * Apache Solr. methods such as adding and removing sorts, remove and replace
5 * parameters, adding and removing filters, getters and setters for various
8 * Class that defines the base query for the Apache Solr Drupal module.
11 class SolrFilterSubQuery
{
14 * Static shared by all instances, used to increment ID numbers.
16 protected static
$idCount = 0;
19 * Each query/subquery will have a unique ID.
25 * A keyed array where the key is a position integer and the value
26 * is an array with #name and #value properties. Each value is a
27 * used for filter queries, e.g. array('#name' => 'is_uid', '#value' => 0)
28 * for anonymous content.
30 protected
$fields = array();
33 * An array of subqueries.
35 protected
$subqueries = array();
37 function __construct($operator = 'OR') {
38 $this->operator
= $operator;
39 $this->id
= ++SolrFilterSubQuery
::$idCount;
43 $this->id
= ++SolrFilterSubQuery
::$idCount;
46 public
function getFilters($name = NULL
) {
52 foreach ($this->fields as
$filter) {
53 if ($filter['#name'] == $name) {
60 public
function hasFilter($name, $value, $exclude = FALSE
) {
61 foreach ($this->fields as
$pos => $values) {
62 if ($values['#name'] == $name && $values['#value'] == $value && $values['#exclude'] == $exclude) {
69 public
function addFilter($name, $value, $exclude = FALSE
, $local = '') {
70 // @todo - escape the value if it has spaces in it and is not a range query or parenthesized.
72 '#exclude' => (bool
) $exclude,
73 '#name' => trim($name),
74 '#value' => trim($value),
75 '#local' => trim($local),
77 $this->fields
[] = $filter;
81 public
function removeFilter($name, $value = NULL
, $exclude = FALSE
) {
82 // Remove from the public list of filters.
83 $this->unsetFilter($this->fields
, $name, $value, $exclude);
87 protected
function unsetFilter(&$fields, $name, $value, $exclude) {
89 foreach ($fields as
$pos => $values) {
90 if ($values['#name'] == $name) {
96 foreach ($fields as
$pos => $values) {
97 if ($values['#name'] == $name && $values['#value'] == $value && $values['#exclude'] == $exclude) {
104 public
function getFilterSubQueries() {
105 return $this->subqueries
;
108 public
function addFilterSubQuery(SolrFilterSubQuery
$query) {
109 $this->subqueries
[$query->id
] = $query;
113 public
function removeFilterSubQuery(SolrFilterSubQuery
$query) {
114 unset($this->subqueries
[$query->id
]);
118 public
function removeFilterSubQueries() {
119 $this->subqueries
= array();
123 public
function makeFilterQuery(array $filter) {
124 $prefix = empty($filter['#exclude']) ?
'' : '-';
125 if ($filter['#local']) {
126 $prefix = '{!' .
$filter['#local'] .
'}' .
$prefix;
128 // If the field value contains a colon or a space, wrap it in double quotes,
129 // unless it is a range query or is already wrapped in double quotes or
131 if (preg_match('/[ :]/', $filter['#value']) && !preg_match('/^[\[\{]\S+ TO \S+[\]\}]$/', $filter['#value']) && !preg_match('/^["\(].*["\)]$/', $filter['#value'])) {
132 $filter['#value'] = '"' .
$filter['#value'] .
'"';
134 return $prefix .
$filter['#name'] .
':' .
$filter['#value'];
138 * Make sure our query matches the pattern name:value or name:"value"
139 * Make sure that if we are ranges we use name:[ AND ]
142 * b. date:[1970-12-31T23:59:59Z TO NOW]
143 * Split the text in 4 different parts
144 * 1. name, eg.: bundle or date
145 * 2. The first opening bracket (or nothing), eg.: [
146 * 3. The value of the field, eg. article or 1970-12-31T23:59:59Z TO NOW
147 * 4. The last closing bracket, eg.: ]
148 * @param string $filter
149 * The filter to validate
152 public static
function validFilterValue($filter) {
158 if (preg_match('/(?P<name>[^:]+):(?P<value>.+)?$/', $filter, $matches)) {
159 foreach ($matches as
$match_id => $match) {
170 // For the name we allow any character that fits between the A-Z0-9 range and
171 // any alternative for this in other languages. No special characters allowed
172 if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) {
176 // For the value we allow anything that is UTF8
177 if (!drupal_validate_utf8($value)) {
181 // Check our bracket count. If it does not match it is also not valid
182 $valid_brackets = TRUE
;
183 $brackets['opening']['{'] = substr_count($value, '{');
184 $brackets['closing']['}'] = substr_count($value, '}');
185 $valid_brackets = ($brackets['opening']['{'] != $brackets['closing']['}']) ? FALSE
: TRUE
;
186 $brackets['opening']['['] = substr_count($value, '[');
187 $brackets['closing'][']'] = substr_count($value, ']');
188 $valid_brackets = ($brackets['opening']['['] != $brackets['closing'][']']) ? FALSE
: TRUE
;
189 $brackets['opening']['('] = substr_count($value, '(');
190 $brackets['closing'][')'] = substr_count($value, ')');
191 $valid_brackets = ($brackets['opening']['('] != $brackets['closing'][')']) ? FALSE
: TRUE
;
192 if (!$valid_brackets) {
196 // Check the date field inputs
197 if (preg_match('/\[(.+) TO (.+)\]$/', $value, $datefields)) {
198 // Only Allow a value in the form of
199 // http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
200 // http://lucene.apache.org/solr/api/org/apache/solr/util/DateMathParser.html
201 // http://wiki.apache.org/solr/SolrQuerySyntax
202 // 1976-03-06T23:59:59.999Z (valid)
204 // 1995-12-31T23:59:59.999Z (valid)
205 // 2007-03-06T00:00:00Z (valid)
206 // NOW-1YEAR/DAY (valid)
207 // NOW/DAY+1DAY (valid)
208 // 1976-03-06T23:59:59.999Z (valid)
209 // 1976-03-06T23:59:59.999Z+1YEAR (valid)
210 // 1976-03-06T23:59:59.999Z/YEAR (valid)
211 // 1976-03-06T23:59:59.999Z (valid)
212 // 1976-03-06T23::59::59.999Z (invalid)
213 if (!empty($datefields[1]) && !empty($datefields[2])) {
214 // Do not check to full value, only the splitted ones
215 unset($datefields[0]);
216 // Check if both matches are valid datefields
217 foreach ($datefields as
$datefield) {
218 if (!preg_match('/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:[\d\.]{2,6}Z(\S)*)|(^([A-Z\*]+)(\A-Z0-9\+\-\/)*)/', $datefield, $datefield_match)) {
229 * Builds a set of filter queries from $this->fields and all subqueries.
231 * Returns an array of strings that can be combined into
232 * a URL query parameter or passed to Solr as fq paramters.
234 protected
function rebuildFq() {
236 foreach ($this->fields as
$pos => $field) {
237 $fq[] = $this->makeFilterQuery($field);
239 foreach ($this->subqueries as
$subquery) {
240 $subfq = $subquery->rebuildFq();
242 $operator = $subquery->operator
;
243 $fq[] = "(" .
implode(" $operator ", $subfq) .
")";
251 class SolrBaseQuery
extends SolrFilterSubQuery implements DrupalSolrQueryInterface
{
254 * The parameters that get sent to Solr.
256 protected
$params = array('start' => 0, 'rows' => 10, 'fq' => array());
259 * The search base path.
261 protected
$base_path;
262 protected
$field_map = array();
265 * DrupalApacheSolrService object
268 // The array keys must always be real Solr index fields.
269 protected
$available_sorts;
272 * The query name is used to construct a searcher string. Mostly the
276 protected
$context = array();
277 // Makes sure we always have a valid sort.
278 protected
$solrsort = array('#name' => 'score', '#direction' => 'desc');
279 // A flag to allow the search to be aborted.
280 public
$abort_search = FALSE
;
282 // A flag to check if need to retrieve another page of the result set
287 * The search name, used for finding the correct blocks and other config.
288 * Typically "apachesolr".
291 * An instantiated DrupalApacheSolrService Object.
292 * Can be instantiated from apachesolr_get_solr().
295 * Array of params to initialize the object (typically 'q' and 'fq').
298 * Visible string telling solr how to sort - added to GET query params.
301 * The search base path (without the keywords) for this query, without trailing slash.
303 function __construct($name, $solr, array $params = array(), $sortstring = '', $base_path = '', $context = array()) {
304 parent
::__construct();
307 $this->addContext((array) $context);
308 $this->addParams((array) $params);
309 $this->available_sorts
= $this->defaultSorts();
310 $this->sortstring
= trim($sortstring);
311 $this->parseSortString();
312 $this->base_path
= $base_path;
315 protected
function defaultSorts() {
317 'score' => array('title' => t('Relevancy'), 'default' => 'desc'),
318 'sort_label' => array('title' => t('Title'), 'default' => 'asc'),
319 'bundle' => array('title' => t('Type'), 'default' => 'asc'),
320 'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
321 'ds_created' => array('title' => t('Date'), 'default' => 'desc'),
328 public
function getName() {
333 * Get query searcher name (for facetapi, views, pages, etc).
335 public
function getSearcher() {
336 return $this->name .
'@' .
$this->solr
->getId();
340 * Get context values.
342 public
function getContext() {
343 return $this->context
;
349 public
function addContext(array $context) {
350 foreach ($context as
$k => $v) {
351 $this->context
[$k] = $v;
353 // The env_id must match that of the actual $solr object
354 $this->context
['env_id'] = $this->solr
->getId();
355 return $this->context
;
358 protected
$single_value_params = array(
359 'q' => TRUE
, // http://wiki.apache.org/solr/SearchHandler#q
360 'q.op' => TRUE
, // http://wiki.apache.org/solr/SearchHandler#q.op
361 'q.alt' => TRUE
, // http://wiki.apache.org/solr/SearchHandler#q
365 'timeAllowed' => TRUE
,
366 'omitHeader' => TRUE
,
367 'debugQuery' => TRUE
,
372 'facet.prefix' => TRUE
,
373 'facet.limit' => TRUE
,
374 'facet.offset' => TRUE
,
375 'facet.mincount' => TRUE
,
376 'facet.missing' => TRUE
,
377 'facet.method' => TRUE
,
378 'facet.enum.cache.minDf' => TRUE
,
379 'facet.date.start' => TRUE
,
380 'facet.date.end' => TRUE
,
381 'facet.date.gap' => TRUE
,
382 'facet.date.hardend' => TRUE
,
383 'facet.date.other' => TRUE
,
384 'facet.date.include' => TRUE
,
386 'hl.snippets' => TRUE
,
387 'hl.fragsize' => TRUE
,
388 'hl.mergeContiguous' => TRUE
,
389 'hl.requireFieldMatch' => TRUE
,
390 'hl.maxAnalyzedChars' => TRUE
,
391 'hl.alternateField' => TRUE
,
392 'hl.maxAlternateFieldLength' => TRUE
,
393 'hl.formatter' => TRUE
,
394 'hl.simple.pre/hl.simple.post' => TRUE
,
395 'hl.fragmenter' => TRUE
,
396 'hl.fragListBuilder' => TRUE
,
397 'hl.fragmentsBuilder' => TRUE
,
398 'hl.useFastVectorHighlighter' => TRUE
,
399 'hl.usePhraseHighlighter' => TRUE
,
400 'hl.highlightMultiTerm' => TRUE
,
401 'hl.regex.slop' => TRUE
,
402 'hl.regex.pattern' => TRUE
,
403 'hl.regex.maxAnalyzedChars' => TRUE
,
404 'spellcheck' => TRUE
,
407 public
function getParam($name) {
409 return $this->rebuildFq();
411 $empty = isset($this->single_value_params
[$name]) ? NULL
: array();
412 return isset($this->params
[$name]) ?
$this->params
[$name] : $empty;
415 public
function getParams() {
416 $params = $this->params
;
417 $params['fq'] = $this->rebuildFq();
421 public
function getSolrParams() {
422 $params = $this->getParams();
423 // For certain fields Solr prefers a comma separated list.
424 foreach (array('fl', 'hl.fl', 'sort', 'mlt.fl') as
$name) {
425 if (isset($params[$name])) {
426 $params[$name] = implode(',', $params[$name]);
432 protected
function addFq($string, $index = NULL
) {
433 $string = trim($string);
439 // Check if we are dealing with an exclude
440 if (preg_match('/^-(.*)/', $string, $matches)) {
442 $string = $matches[1];
445 // If {!something} is found as first character then this is a local value
446 if (preg_match('/\{!([^}]+)\}(.*)/', $string, $matches)) {
447 $local = $matches[1];
448 $string = $matches[2];
451 // Anything that has a name and value
452 // check if we have a : in the string
453 if (strstr($string, ':')) {
454 list($name, $value) = explode(":", $string, 2);
459 $this->addFilter($name, $value, $exclude, $local);
463 public
function addParam($name, $value) {
464 if (isset($this->single_value_params
[$name])) {
465 if (is_array($value)) {
466 $value = end($value);
468 $this->params
[$name] = $this->normalizeParamValue($value);
471 // We never actually populate $this->params['fq']. Instead
472 // we manage everything via the filter methods.
474 if (is_array($value)) {
475 array_walk_recursive($value, array($this, 'addFq'));
479 return $this->addFq($value);
483 if (!isset($this->params
[$name])) {
484 $this->params
[$name] = array();
487 if (!is_array($value)) {
488 // Convert to array for array_map.
489 $param_values = array($value);
492 // Convert to a numerically keyed array.
493 $param_values = array_values($value);
495 $this->params
[$name] = array_merge($this->params
[$name], array_map(array($this, 'normalizeParamValue'), $param_values));
500 protected
function normalizeParamValue($value) {
501 // Convert boolean to string.
502 if (is_bool($value)) {
503 return $value ?
'true' : 'false';
505 // Convert to trimmed string.
509 public
function addParams(Array $params) {
510 foreach ($params as
$name => $value) {
511 $this->addParam($name, $value);
516 public
function removeParam($name) {
517 unset($this->params
[$name]);
519 $this->fields
= array();
520 $this->subqueries
= array();
525 public
function replaceParam($name, $value) {
526 $this->removeParam($name);
527 return $this->addParam($name, $value);
531 * Handles aliases for field to make nicer URLs.
534 * An array keyed with real Solr index field names with the alias as value.
536 * @return DrupalSolrQueryInterface
539 public
function addFieldAliases($field_map) {
540 $this->field_map
= array_merge($this->field_map
, $field_map);
541 // We have to re-parse the filters.
542 $this->parseSortString();
546 public
function getFieldAliases() {
547 return $this->field_map
;
550 public
function clearFieldAliases() {
551 $this->field_map
= array();
552 // We have to re-parse the filters.
553 $this->parseSortString();
557 protected
function parseSortString() {
558 // Substitute any field aliases with real field names.
559 $sortstring = strtr($this->sortstring
, $this->field_map
);
560 // Score is a special case - it's the default sort for Solr.
561 if ('' == $sortstring || 'score desc' == $sortstring) {
562 $this->solrsort
['#name'] = 'score';
563 $this->solrsort
['#direction'] = 'desc';
564 unset($this->params
['sort']);
567 // Validate and set sort parameter
568 $fields = implode('|', array_keys($this->available_sorts
));
569 if (preg_match('/^(?:(' .
$fields .
') (asc|desc),?)+$/', $sortstring, $matches)) {
570 // We only use the last match.
571 $this->solrsort
['#name'] = $matches[1];
572 $this->solrsort
['#direction'] = $matches[2];
573 $this->params
['sort'] = array($sortstring);
578 public
function getAvailableSorts() {
579 return $this->available_sorts
;
582 public
function setAvailableSort($name, $sort) {
583 // We expect non-aliased sorts to be added.
584 $this->available_sorts
[$name] = $sort;
585 // Re-parse the sortstring.
586 $this->parseSortString();
590 public
function setAvailableSorts($sorts) {
591 // We expect a complete array of valid sorts.
592 $this->available_sorts
= $sorts;
593 $this->parseSortString();
597 public
function removeAvailableSort($name) {
598 unset($this->available_sorts
[$name]);
599 // Re-parse the sortstring.
600 $this->parseSortString();
604 public
function getSolrsort() {
605 return $this->solrsort
;
608 public
function setSolrsort($name, $direction) {
609 $this->sortstring
= trim($name) .
' ' .
trim($direction);
610 $this->parseSortString();
614 public
function getPath($new_keywords = NULL
) {
615 if (isset($new_keywords)) {
616 return $this->base_path .
'/' .
$new_keywords;
618 elseif ($this->getParam('q')) {
619 return $this->base_path .
'/' .
$this->getParam('q');
622 // Return with empty query (the slash). The path for a facet
623 // becomes $this->base_path . '//facetinfo';
624 // We do this so we can have a consistent way of retrieving the query +
625 // additional parameters
626 return $this->base_path .
'/';
630 public
function getSolrsortUrlQuery() {
631 $queryvalues = array();
632 $solrsort = $this->solrsort
;
633 if ($solrsort && ($solrsort['#name'] != 'score')) {
634 if (isset($this->field_map
[$solrsort['#name']])) {
635 $solrsort['#name'] = $this->field_map
[$solrsort['#name']];
637 $queryvalues['solrsort'] = $solrsort['#name'] .
' ' .
$solrsort['#direction'];
640 // Return to default relevancy sort.
641 unset($queryvalues['solrsort']);
646 public
function search($keys = NULL
) {
647 if ($this->abort_search
) {
650 return $this->solr
->search($keys, $this->getSolrParams());
653 public
function solr($method) {
654 return $this->solr
->$method();