public function testMaxAggregation()
 {
     $agg = new Max("min_price");
     $agg->setField("price");
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("min_price");
     $this->assertEquals(8, $results['value']);
     // test using a script
     $agg->setScript(new Script("_value * conversion_rate", array("conversion_rate" => 1.2)));
     $query = new Query();
     $query->addAggregation($agg);
     $results = $this->_index->search($query)->getAggregation("min_price");
     $this->assertEquals(8 * 1.2, $results['value']);
 }
Exemple #2
0
 /**
  * @group functional
  */
 public function testMaxAggregation()
 {
     $index = $this->_getIndexForTest();
     $agg = new Max('min_price');
     $agg->setField('price');
     $query = new Query();
     $query->addAggregation($agg);
     $results = $index->search($query)->getAggregation('min_price');
     $this->assertEquals(8, $results['value']);
     // test using a script
     $agg->setScript(new Script('_value * conversion_rate', array('conversion_rate' => 1.2)));
     $query = new Query();
     $query->addAggregation($agg);
     $results = $index->search($query)->getAggregation('min_price');
     $this->assertEquals(8 * 1.2, $results['value']);
 }
 public function attributeAction()
 {
     $result = ['outcome' => 0];
     $entityClass = 'Lead\\Entity\\LeadAttribute';
     $id = $this->getEvent()->getRouteMatch()->getParam('id', '0');
     $action = $this->params()->fromQuery('action', 'update');
     $em = $this->getEntityManager();
     $objRepository = $em->getRepository($entityClass);
     if ($id) {
         switch ($action) {
             case 'update':
                 $attributeType = $this->params()->fromQuery('type', false);
                 if ($attributeType) {
                     /* @var $entity \Lead\Entity\LeadAttribute */
                     $entity = $objRepository->find($id);
                     if ($entity && $entity instanceof $entityClass) {
                         $entity->setAttributeType($attributeType);
                         try {
                             $em->persist($entity);
                             $em->flush();
                             $this->createServiceEvent()->setEntityId($id)->setEntityClass($entityClass)->setDescription("Lead Attribute Edited")->setMessage("Lead Attribute: " . $entity->getAttributeDesc() . " was edited.");
                             $this->logEvent("EditAction.post");
                             $result['outcome'] = 1;
                         } catch (\Exception $e) {
                             $this->createServiceEvent()->setEntityId($id)->setEntityClass($entityClass)->setDescription("Lead Attribute Edited");
                             $this->logError($e);
                         }
                     }
                 }
                 break;
             case 'relationship':
                 /* @var $entity \Lead\Entity\LeadAttribute */
                 $entity = $objRepository->find($id);
                 if ($entity && $entity instanceof $entityClass) {
                     $attributeType = $entity->getAttributeType();
                     /* @var $qb \Doctrine\ORM\QueryBuilder */
                     $qb = $em->createQueryBuilder();
                     $qb->add('select', 'e')->add('from', 'Agent\\Entity\\Relationship' . ' e')->where(' e.allowed LIKE :attributeType')->setParameter('attributeType', "%" . $attributeType . "%");
                     $dql = $qb->getDQL();
                     $query = $qb->getQuery();
                     $query->useQueryCache(true);
                     $query->useResultCache(true, 3600, 'relationship-' . md5($dql));
                     $result['data'] = $query->getArrayResult();
                     $result['outcome'] = 1;
                 }
                 break;
             case 'values':
                 $qb = $em->createQueryBuilder()->select('v.value')->from('Lead\\Entity\\LeadAttributeValue', 'v')->where('v.attribute = :id')->setParameter('id', $id)->distinct(true);
                 $dql = $qb->getDQL();
                 $query = $qb->getQuery();
                 $query->useQueryCache(true);
                 $query->useResultCache(true, 3600, 'leadattributevalue-' . md5($dql));
                 $results = $query->getArrayResult();
                 if ($results) {
                     $result['data'] = $results;
                     $result['outcome'] = 1;
                 }
                 break;
             case 'limits':
                 $attribute_query = new Elastica\Query\Nested();
                 $attribute_query->setPath('attribute');
                 $attribute_query->setQuery(new Elastica\Query\Match('attribute.id', $id));
                 $value_filter = new Elastica\Filter\Query();
                 $value_filter->setQuery($attribute_query);
                 $max = new Elastica\Aggregation\Max('max_number');
                 $max->setField('_number');
                 $min = new Elastica\Aggregation\Min('min_number');
                 $min->setField('_number');
                 $aggs = new Elastica\Aggregation\Filter('numeric_range', $value_filter);
                 $aggs->addAggregation($max)->addAggregation($min);
                 $query = new Elastica\Query();
                 $query->setSize(0);
                 $query->addAggregation($aggs);
                 /* @var $elastica_client Elastica\Client */
                 $elastica_client = $this->getServiceLocator()->get('elastica-client');
                 $result['query'] = $query->toArray();
                 try {
                     /* @var $response \Elastica\Response */
                     $response = $elastica_client->request('reports/value/_search', Request::GET, $query->toArray());
                     $data = $response->getData();
                     $limits = ['min' => 0, 'max' => 0];
                     if (isset($data['aggregations']['numeric_range']) && $data['aggregations']['numeric_range']['doc_count'] > 0) {
                         $limits['min'] = $data['aggregations']['numeric_range']['min_number']['value'];
                         $limits['max'] = $data['aggregations']['numeric_range']['max_number']['value'];
                     }
                     $result['data'] = $limits;
                     $result['outcome'] = 1;
                 } catch (\Exception $e) {
                     $result['error'] = $e->getMessage();
                 }
                 break;
         }
     }
     return new JsonModel($result);
 }