public function testPostCreateRequest()
 {
     // create a very long query
     $fq = '';
     for ($i = 1; $i <= 1000; $i++) {
         $fq .= ' OR price:' . $i;
     }
     $fq = substr($fq, 4);
     $this->query->createFilterQuery('fq')->setQuery($fq);
     $requestOutput = $this->client->createRequest($this->query);
     $requestInput = clone $requestOutput;
     $event = new PostCreateRequestEvent($this->query, $requestOutput);
     $this->plugin->postCreateRequest($event);
     $this->assertEquals(Request::METHOD_GET, $requestInput->getMethod());
     $this->assertEquals(Request::METHOD_POST, $requestOutput->getMethod());
     $this->assertEquals($requestInput->getQueryString(), $requestOutput->getRawData());
     $this->assertEquals('', $requestOutput->getQueryString());
 }
 /**
  * Adds filter queries configured in TypoScript to $query.
  *
  * @param \Solarium\QueryType\Select\Query\Query $query
  * @return $this
  */
 protected function addTypoScriptFilters()
 {
     if (!empty($this->settings['additionalFilters'])) {
         foreach ($this->settings['additionalFilters'] as $key => $filterQuery) {
             $this->query->createFilterQuery('additionalFilter-' . $key)->setQuery($filterQuery);
         }
     }
     return $this;
 }
Example #3
0
 public function testCreateFilterQuery()
 {
     $options = array('optionA' => 1, 'optionB' => 2);
     $fq = $this->query->createFilterQuery($options);
     // check class
     $this->assertThat($fq, $this->isInstanceOf('Solarium\\QueryType\\Select\\Query\\FilterQuery'));
     // check option forwarding
     $fqOptions = $fq->getOptions();
     $this->assertEquals($options['optionB'], $fqOptions['optionB']);
 }
Example #4
0
 protected function createFilterQuery(Query $query)
 {
     if ($this->expression === NULL) {
         return NULL;
     }
     $this->expression = $this->doInverse($this->expression);
     if (($queryString = $this->expression->build()) !== NULL) {
         $key = $this->key ?: md5($queryString);
         $fq = $query->createFilterQuery($key)->setQuery($queryString);
         if ($this->expression instanceof FieldExpression) {
             $fq->addTag($this->expression->getField());
         }
         if ($this->key !== NULL) {
             $fq->addTag($this->key);
         }
     } else {
         $fq = NULL;
     }
     $this->expression = $this->doInverse($this->expression);
     //return to original state
     return $fq;
 }
 /**
  * @param \Solarium\QueryType\Select\Query\Query $select
  */
 public function addKritsToSolr(&$select)
 {
     foreach ($this->krits as $krit) {
         switch ($krit["typ"]) {
             case "betreff":
                 $helper = $select->getHelper();
                 $select->createFilterQuery("betreff")->setQuery("antrag_betreff:" . $helper->escapeTerm($krit["suchbegriff"]));
                 break;
             case "antrag_typ":
                 $select->createFilterQuery("antrag_typ")->setQuery("antrag_typ:" . $krit["suchbegriff"]);
                 break;
             case "antrag_wahlperiode":
                 $select->createFilterQuery("antrag_wahlperiode")->setQuery("antrag_wahlperiode:" . $krit["suchbegriff"]);
                 break;
             case "volltext":
                 /** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */
                 $dismax = $select->getDisMax();
                 $dismax->setQueryParser('edismax');
                 $dismax->setQueryFields("text text_ocr");
                 $select->setQuery($krit["suchbegriff"]);
                 break;
             case "ba":
                 $select->createFilterQuery("dokument_bas")->setQuery("dokument_bas:" . $krit["ba_nr"]);
                 break;
             case "geo":
                 $helper = $select->getHelper();
                 $select->createFilterQuery("geo")->setQuery($helper->geofilt("geo", $krit["lat"], $krit["lng"], $krit["radius"] / 1000));
                 break;
             case "referat":
                 $helper = $select->getHelper();
                 $select->createFilterQuery("referat")->setQuery("referat_id:" . $helper->escapeTerm($krit["referat_id"]));
                 break;
             case "antrag_nr":
                 /** @var Solarium\QueryType\Select\Query\Component\DisMax $dismax */
                 $dismax = $select->getDisMax();
                 $dismax->setQueryParser('edismax');
                 $dismax->setQueryFields("antrag_nr");
                 $select->setQuery("*" . $krit["suchbegriff"] . "*");
                 break;
         }
     }
 }
Example #6
0
 /**
  * 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);
     }
 }
 /**
  * Adds filter queries configured in TypoScript to $query.
  *
  * @param \Solarium\QueryType\Select\Query\Query $query
  */
 private function addTypoScriptFilters($query)
 {
     if (!empty($this->templateVariableContainer->get('settings')['additionalFilters'])) {
         foreach ($this->templateVariableContainer->get('settings')['additionalFilters'] as $key => $filterQuery) {
             $query->createFilterQuery('additionalFilter-' . $key)->setQuery($filterQuery);
         }
     }
 }