/** * Adds spatial features to the search query. */ public function setSpatial(Query $solarium_query, QueryInterface $query, $spatial_options = array(), $field_names) { foreach ($spatial_options as $i => $spatial) { // reset radius for each option unset($radius); if (empty($spatial['field']) || empty($spatial['lat']) || empty($spatial['lon'])) { continue; } $field = $field_names[$spatial['field']]; $escaped_field = SearchApiSolrUtility::escapeFieldName($field); $point = (double) $spatial['lat'] . ',' . (double) $spatial['lon']; // Prepare the filter settings. if (isset($spatial['radius'])) { $radius = (double) $spatial['radius']; } $spatial_method = 'geofilt'; if (isset($spatial['method']) && in_array($spatial['method'], array('geofilt', 'bbox'))) { $spatial_method = $spatial['method']; } $filter_queries = $solarium_query->getFilterQueries(); // Change the fq facet ranges to the correct fq. foreach ($filter_queries as $key => $filter_query) { // If the fq consists only of a filter on this field, replace it with // a range. $preg_field = preg_quote($escaped_field, '/'); if (preg_match('/^' . $preg_field . ':\\["?(\\*|\\d+(?:\\.\\d+)?)"? TO "?(\\*|\\d+(?:\\.\\d+)?)"?\\]$/', $filter_query, $matches)) { unset($filter_queries[$key]); if ($matches[1] && is_numeric($matches[1])) { $min_radius = isset($min_radius) ? max($min_radius, $matches[1]) : $matches[1]; } if (is_numeric($matches[2])) { // Make the radius tighter accordingly. $radius = isset($radius) ? min($radius, $matches[2]) : $matches[2]; } } } // If either a radius was given in the option, or a filter was // encountered, set a filter for the lowest value. If a lower boundary // was set (too), we can only set a filter for that if the field name // doesn't contains any colons. if (isset($min_radius) && strpos($field, ':') === FALSE) { $upper = isset($radius) ? " u={$radius}" : ''; $solarium_query->createFilterQuery($field)->setQuery("{!frange l={$min_radius}{$upper}}geodist({$field},{$point})"); } elseif (isset($radius)) { $solarium_query->createFilterQuery($field)->setQuery("{!{$spatial_method} pt={$point} sfield={$field} d={$radius}}"); } // @todo: Check if this object returns the correct value $sorts = $solarium_query->getSorts(); // Change sort on the field, if set (and not already changed). if (isset($sorts[$spatial['field']]) && substr($sorts[$spatial['field']], 0, strlen($field)) === $field) { $sorts[$spatial['field']] = str_replace($field, "geodist({$field},{$point})", $sorts[$spatial['field']]); } // Change the facet parameters for spatial fields to return distance // facets. $facets = $solarium_query->getFacetSet(); // @todo: Fix this so it takes it from the solarium query if (!empty($facets)) { if (!empty($facet_params['facet.field'])) { $facet_params['facet.field'] = array_diff($facet_params['facet.field'], array($field)); } foreach ($facets as $delta => $facet) { if ($facet['field'] != $spatial['field']) { continue; } $steps = $facet['limit'] > 0 ? $facet['limit'] : 5; $step = (isset($radius) ? $radius : 100) / $steps; for ($k = $steps - 1; $k > 0; --$k) { $distance = $step * $k; $key = "spatial-{$delta}-{$distance}"; $facet_params['facet.query'][] = "{!{$spatial_method} pt={$point} sfield={$field} d={$distance} key={$key}}"; } foreach (array('limit', 'mincount', 'missing') as $setting) { unset($facet_params["f.{$field}.facet.{$setting}"]); } } } } // Normal sorting on location fields isn't possible. foreach (array_keys($solarium_query->getSorts()) as $sort) { if (substr($sort, 0, 3) === 'loc') { $solarium_query->removeSort($sort); } } }
/** * Helper method for creating the facet field parameters. */ protected function setFacets(array $facets, array $field_names, Query $solarium_query) { $fq = array(); if (!$facets) { return; } $facet_set = $solarium_query->getFacetSet(); $facet_set->setSort('count'); $facet_set->setLimit(10); $facet_set->setMinCount(1); $facet_set->setMissing(FALSE); $taggedFields = array(); foreach ($facets as $info) { if (empty($field_names[$info['field']])) { continue; } // String fields have their own corresponding facet fields. $field = $field_names[$info['field']]; // Check for the "or" operator. if (isset($info['operator']) && $info['operator'] === 'or') { // Remember that filters for this field should be tagged. $escaped = SearchApiSolrUtility::escapeFieldName($field_names[$info['field']]); $taggedFields[$escaped] = "{!tag={$escaped}}"; // Add the facet field. $facet_field = $facet_set->createFacetField($field)->setField("{!ex={$escaped}}{$field}"); } else { // Add the facet field. $facet_field = $facet_set->createFacetField($field)->setField($field); } // Set limit, unless it's the default. if ($info['limit'] != 10) { $limit = $info['limit'] ? $info['limit'] : -1; $facet_field->setLimit($limit); } // Set mincount, unless it's the default. if ($info['min_count'] != 1) { $facet_field->setMinCount($info['min_count']); } // Set missing, if specified. if ($info['missing']) { $facet_field->setMissing(TRUE); } } // Tag filters of fields with "OR" facets. foreach ($taggedFields as $field => $tag) { $regex = '#(?<![^( ])' . preg_quote($field, '#') . ':#'; foreach ($fq as $i => $conditions) { // Solr can't handle two tags on the same filter, so we don't add two. // Another option here would even be to remove the other tag, too, // since we can be pretty sure that this filter does not originate from // a facet – however, wrong results would still be possible, and this is // definitely an edge case, so don't bother. if (preg_match($regex, $conditions) && substr($conditions, 0, 6) != '{!tag=') { $fq[$i] = $tag . $conditions; } } } foreach ($fq as $key => $conditions_query) { $solarium_query->createFilterQuery('facets_' . $key)->setQuery($conditions_query); } }