Ejemplo n.º 1
0
 /**
  * @see FacetInterface::getFilter
  */
 public function getFilter($fieldName, array $values)
 {
     // combine selected filters in an or clause
     $filter = new \Elastica\Filter\Bool();
     $rangeDefs = $this->getRangeDefinitions();
     foreach ($values as $filterId) {
         if (isset($rangeDefs[$filterId])) {
             $rangeFilter = new \Elastica\Filter\Range();
             $rangeFilter->addField($fieldName, $rangeDefs[$filterId]);
             $filter->addShould($rangeFilter);
         }
     }
     return $filter;
 }
Ejemplo n.º 2
0
             $queryMatchTypeOf->setFieldQuery($_GET['fieldquery'], $exm);
             $elasticaTypeOfFilter = new Elastica\Filter\Query();
             $elasticaTypeOfFilter->setQuery($queryMatchTypeOf);
             $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
             $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
             $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaTypeOfFilter);
         }
     }
     $query = new Elastica\Query($filteredQuery);
 } else {
     $query = new Elastica\Query($queryString);
 }
 //DATE QUERY
 if (isset($_GET['range'])) {
     $elasticaFilterRange = new Elastica\Filter\Range();
     $elasticaFilterRange->addField('start_date', array("from" => $_GET['start'], 'to' => $_GET['end']));
     $filteredQuery = new Elastica\Query\Filtered($query, $elasticaFilterRange);
 }
 $search = new Elastica\Search($elasticaClient);
 $searchThrough = $search->addIndex($_GET['index']);
 if (isset($_GET['collection']) && $_GET['collection'] != 'all') {
     $boolQuery = new Elastica\Query\Bool();
     $query_collection = new Elastica\Query\Match();
     $query_collection->setFieldQuery('content_type', $_GET['collection']);
     $elasticaTypeOfFilter = new Elastica\Filter\Query();
     $elasticaTypeOfFilter->setQuery($query_collection);
     $elasticaFilterAnd = new Elastica\Filter\BoolAnd();
     $elasticaFilterAnd->addFilter($elasticaTypeOfFilter);
     $filteredQuery = new Elastica\Query\Filtered($queryString, $elasticaFilterAnd);
 }
 $query = new Elastica\Query($filteredQuery);
Ejemplo n.º 3
0
 /**
  * @param $appliedFilters
  * @param $elasticaQuery
  *
  * @return array
  */
 public function applyFilterToElasticaQuery($appliedFilters, $elasticaQuery)
 {
     $rangeFilters = new \Elastica\Filter\Range();
     $boolFilter = new \Elastica\Filter\BoolFilter();
     $filters = array();
     foreach ($appliedFilters as $facet) {
         if (strpos($facet[key($facet)], "|") !== false) {
             $filters[key($facet)][] = array('range' => explode('|', $facet[key($facet)]));
         } else {
             $filters[key($facet)][] = $facet[key($facet)];
         }
     }
     foreach ($filters as $name => $value) {
         if (is_array($value[0])) {
             foreach ($value as $range) {
                 $rangeFilters->addField($name, array('gte' => $range['range'][0], 'lte' => $range['range'][1]));
                 $boolFilter->addShould($rangeFilters);
             }
         } else {
             $termFilters = new \Elastica\Filter\Terms();
             $termFilters->setTerms($name, $value);
             $boolFilter->addShould($termFilters);
         }
     }
     $elasticaQuery->setFilter($boolFilter);
     return array($termFilters, $rangeFilters, $boolFilter, $filters);
 }
Ejemplo n.º 4
0
 public function searchFile($criteria, $dataset, $sorts = array(), $operator = 'OR')
 {
     /*if($limit <= 0) {
           $limit = Yii::app()->params['es_search']['limits']['default'];
       }*/
     $keyword = $criteria['keyword'];
     $eQueryString = new \Elastica\Query\QueryString();
     $eQueryString->setDefaultOperator($operator);
     $eQueryString->setQuery($keyword);
     $eQuery = new \Elastica\Query();
     $eQuery->setQuery($eQueryString);
     //$eQuery->setFrom($offset);
     //$eQuery->setLimit($limit);
     $filterAnd = new \Elastica\Filter\BoolAnd();
     if (is_array($dataset) && !empty($dataset)) {
         $ids = new \Elastica\Filter\Ids();
         $ids->setIds($dataset);
         $filterAnd->addFilter($ids);
     }
     if (isset($criteria['file_format'])) {
         $filterAnd->addFilter($this->newTerms('format', $criteria['file_format']));
     }
     if (isset($criteria['file_type'])) {
         $filterAnd->addFilter($this->newTerms('filetype', $criteria['file_type']));
     }
     if (isset($criteria['size_from']) && isset($criteria['size_to'])) {
         $sizefilter = new \Elastica\Filter\Range();
         $from = $this->convertSize($criteria['size_from'], $criteria['size_from_unit']);
         $to = $this->convertSize($criteria['size_to'], $criteria['size_to_unit']);
         $sizefilter->addField('size', array('gte' => $from, 'lte' => $to));
         $filterAnd->addFilter($sizefilter);
     } elseif (isset($criteria['size_from'])) {
         $sizefilter = new \Elastica\Filter\Range();
         $from = $this->convertSize($criteria['size_from'], $criteria['size_from_unit']);
         $sizefilter->addField('size', array('gte' => $from));
         $filterAnd->addFilter($sizefilter);
     } elseif (isset($criteria['size_to'])) {
         $sizefilter = new \Elastica\Filter\Range();
         $to = $this->convertSize($criteria['size_to'], $criteria['size_to_unit']);
         $sizefilter->addField('size', array('gte' => 0, 'lte' => $to));
         $filterAnd->addFilter($sizefilter);
     }
     $arrayAnd = $filterAnd;
     $arrayAnd = $arrayAnd->toArray();
     //Yii::log(print_r($arrayAnd, true), 'debug');
     if (count($arrayAnd['and']) > 0) {
         $eQuery->setPostFilter($filterAnd);
     }
     $sortList = array_merge(array('_score' => array('order' => 'desc')), $sorts);
     $eQuery->setSort($sortList);
     $index = Yii::app()->elastic->client->getIndex('gigadb');
     $type = $index->getType('file');
     $eResultSet = $type->search($eQuery);
     $results = $eResultSet->getResults();
     $total = $eResultSet->getTotalHits();
     $data = array();
     foreach ($results as $result) {
         if ($result) {
             $data[] = $result->getData();
         }
     }
     $re = array('data' => $data, 'total' => $total);
     //Yii::log(print_r($re, true), 'debug');
     return $re;
 }
Ejemplo n.º 5
0
 /**
  * @param array $params
  * @return \Elastica\Filter\AbstractFilter
  */
 protected function _getQueryFilter(array $params)
 {
     if (!isset($params['filters']) || empty($params['filters'])) {
         $filters = '*';
     } else {
         $filters = $this->_helper->_prepareFilter($params['filters']);
     }
     $queryFilter = new \Elastica\Filter\Query(new \Elastica\Query\QueryString($filters));
     if (isset($params['range_filters']) && !empty($params['range_filters'])) {
         $andFilter = new \Elastica\Filter\BoolAnd();
         $andFilter->addFilter($queryFilter);
         $filter = new \Elastica\Filter\Range();
         foreach ($params['range_filters'] as $field => $rangeFilter) {
             $filter->addField($field, $rangeFilter);
         }
         $andFilter->addFilter($filter);
         $queryFilter = $andFilter;
     }
     return $queryFilter;
 }
Ejemplo n.º 6
0
 /**
  * @return \Elastica\Filter\AbstractFilter
  * @throws ErrorException
  */
 public function getElasticaFilter()
 {
     switch ($this->condition) {
         case self::CONDITION_EQ:
         case self::CONDITION_NOT_EQ:
             $filter = new \Elastica\Filter\Term();
             $filter->addParam($this->parameter, $this->value);
             break;
         case self::CONDITION_GTE:
             $filter = new \Elastica\Filter\Range();
             $filter->addField($this->parameter, ['gte' => $this->value]);
             break;
         case self::CONDITION_GT:
             $filter = new \Elastica\Filter\Range();
             $filter->addField($this->parameter, ['gt' => $this->value]);
             break;
         case self::CONDITION_LTE:
             $filter = new \Elastica\Filter\Range();
             $filter->addField($this->parameter, ['lte' => $this->value]);
             break;
         case self::CONDITION_LT:
             $filter = new \Elastica\Filter\Range();
             $filter->addField($this->parameter, ['lt' => $this->value]);
             break;
         case self::CONDITION_RANGE:
             $filter = new \Elastica\Filter\Range($this->parameter, $this->getRangeArgs());
             break;
         default:
             throw new ErrorException('Unknown aggregation type');
     }
     return $filter;
 }