public function Solr_Base_Query::filter_extract in Apache Solr Search 6.2
Same name and namespace in other branches
- 5.2 Solr_Base_Query.php \Solr_Base_Query::filter_extract()
- 6 Solr_Base_Query.php \Solr_Base_Query::filter_extract()
Extract all uses of one named field from a filter string e.g. 'type:book'
1 call to Solr_Base_Query::filter_extract()
- Solr_Base_Query::parse_filters in ./
Solr_Base_Query.php - Parse the filter string in $this->filters into $this->fields.
File
- ./
Solr_Base_Query.php, line 8
Class
Code
public function filter_extract(&$filterstring, $name) {
$extracted = array();
$name = preg_quote($name, '/');
// Range queries. The "TO" is case-sensitive.
$patterns[] = '/(^| |-)' . $name . ':([\\[\\{](\\S+) TO (\\S+)[\\]\\}])/';
// Match quoted values.
$patterns[] = '/(^| |-)' . $name . ':"([^"]*)"/';
// Match unquoted values.
$patterns[] = '/(^| |-)' . $name . ':([^ ]*)/';
foreach ($patterns as $p) {
if (preg_match_all($p, $filterstring, $matches, PREG_SET_ORDER)) {
// Sort matches longest to shortest to avoid accidentally
// removing a sub-string.
usort($matches, array(
$this,
'filter_extract_cmp',
));
foreach ($matches as $match) {
$filter = array();
$filter['#query'] = $match[0];
$filter['#exclude'] = $match[1] == '-';
$filter['#value'] = trim($match[2]);
// Empty values cause Lucene parse errors, so skip them.
if (strlen($filter['#value'])) {
if (isset($match[3])) {
// Extra data for range queries
$filter['#start'] = $match[3];
$filter['#end'] = $match[4];
}
$extracted[] = $filter;
}
// Update the local copy of $filters by removing the match.
$filterstring = str_replace($match[0], '', $filterstring);
}
}
}
return $extracted;
}