protected function translateFilters(CriteriaList $filter_criteria_list)
 {
     $elasticsearch_filters = [];
     foreach ($filter_criteria_list as $criteria) {
         if ($criteria instanceof CriteriaContainerInterface) {
             $operator = $criteria->getOperator();
             $filters = $this->translateFilters($criteria->getCriteriaList());
             if (!empty($filters)) {
                 if (isset($elasticsearch_filters[$operator])) {
                     $elasticsearch_filters[] = array_merge_recursive($elasticsearch_filters[$operator], $filters);
                 } else {
                     $elasticsearch_filters[] = $filters;
                 }
             }
         } elseif ($criteria instanceof AttributeCriteria) {
             $elasticsearch_filters[] = $this->buildFilterFor($criteria);
         } elseif ($criteria instanceof RangeCriteria) {
             $elasticsearch_filters[] = $this->buildRangeFilterFor($criteria);
         } elseif ($criteria instanceof SpatialCriteria) {
             $elasticsearch_filters[] = $this->buildSpatialFilterFor($criteria);
         } elseif ($criteria instanceof CustomCriteria) {
             $elasticsearch_filters[] = $criteria->getQueryPart();
         } else {
             throw new RuntimeError(sprintf('Invalid criteria type %s given to %s', get_class($criteria), static::CLASS));
         }
     }
     if (count($elasticsearch_filters)) {
         return [$filter_criteria_list->getOperator() => $elasticsearch_filters];
     } else {
         return [];
     }
 }
예제 #2
0
 public function asQuery()
 {
     $filter_criteria = [];
     if ($this->hasFilter()) {
         foreach ($this->getFilter() as $attribute_path => $value) {
             if (!is_string($value)) {
                 throw new RuntimeError('Only strings are supported as filter values at the moment.');
             }
             if (!preg_match_all('#(?<criteria>\\w+)\\((?<value>.+)\\)(?:,|$)#U', $value, $matches, PREG_SET_ORDER)) {
                 $matches = explode(',', $value);
                 foreach ($matches as $match) {
                     $filter_criteria[] = $this->buildAttributeFilterFor($attribute_path, $match);
                 }
             } else {
                 foreach ($matches as $match) {
                     switch ($match['criteria']) {
                         case 'range':
                             $filter_criteria[] = $this->buildRangeFilterFor($attribute_path, $match['value']);
                             break;
                         case 'spatial':
                             $filter_criteria[] = $this->buildSpatialFilterFor($attribute_path, $match['value']);
                             break;
                         case 'match':
                             $filter_criteria[] = $this->buildMatchFilterFor($attribute_path, $match['value']);
                             break;
                         default:
                             // better fallback for regex gone wrong
                             $filter_criteria = [];
                             $filter_criteria[] = $this->buildAttributeFilterFor($attribute_path, $value);
                             //throw new RuntimeError('Unsupported query criteria: ' . $match['criteria']);
                     }
                 }
             }
         }
     }
     $filter_criteria_list = new CriteriaList($filter_criteria);
     $search_criteria_list = new CriteriaList();
     if ($this->hasSearch()) {
         $search_criteria_list->push(new SearchCriteria($this->getSearch()));
     }
     $sort_criteria_list = new CriteriaList();
     if ($this->hasSort()) {
         $sort_string = $this->getSort();
         $sort = [];
         $sort_fields = explode(',', $sort_string);
         foreach ($sort_fields as $sort_field) {
             if (!preg_match('/^([\\w\\.]+):(asc|desc)$/u', $sort_field)) {
                 throw new RuntimeError('The sort value has a wrong format. Should be: field1:asc,field2:desc');
             }
             list($attribute_path, $direction) = explode(':', $sort_field);
             $sort_criteria_list->push(new SortCriteria($attribute_path, $direction));
         }
     }
     return new CriteriaQuery($search_criteria_list, $filter_criteria_list, $sort_criteria_list, $this->getOffset(), $this->getLimit());
 }
 protected function buildQuery($identifier, CriteriaList $reference_filter_list)
 {
     $affected_relatives = [];
     if (!empty($reference_filter_list)) {
         // prevent circular self reference loading
         $filter_criteria_list = new CriteriaList();
         $filter_criteria_list->push(new AttributeCriteria('identifier', new Equals($identifier, true)));
         $filter_criteria_list->push($reference_filter_list);
         $this->getQueryService()->scroll(new CriteriaQuery(new CriteriaList(), $filter_criteria_list, new CriteriaList(), 0, $this->config->get('batch_size', 1000)), function (ProjectionInterface $projection) use(&$affected_relatives) {
             // @note if there are many affected relatives this could consume memory
             $affected_relatives[] = $projection;
         });
     }
     return new ProjectionMap($affected_relatives);
 }