Inheritance: extends AbstractQuery
 /**
  * @group unit
  */
 public function testSetQuery()
 {
     $nested = new Nested();
     $path = 'test1';
     $queryString = new QueryString('test');
     $this->assertInstanceOf('Elastica\\Query\\Nested', $nested->setQuery($queryString));
     $this->assertInstanceOf('Elastica\\Query\\Nested', $nested->setPath($path));
     $expected = array('nested' => array('query' => $queryString->toArray(), 'path' => $path));
     $this->assertEquals($expected, $nested->toArray());
 }
 /**
  *
  * @param BoolQuery $lead_query        	
  * @param Filter $filter        	
  *
  * @return BoolQuery $lead_query
  */
 protected function applyFilters(BoolQuery $lead_query, Filter $filter = null)
 {
     if ($filter) {
         $account = $filter->getAccountFilter();
         $date = $filter->getDateFilter();
         if ($account) {
             $mode = $account->getMode();
             switch ($mode) {
                 case 'orphan':
                     $account_query = new Elastica\Query\Nested();
                     $account_query->setPath('account')->setScoreMode('avg');
                     $account_query->setQuery(new Elastica\Query\MatchAll());
                     $lead_query->addMustNot($account_query);
                     break;
                 case 'account':
                     if (($account = $account->getAccount()) == true) {
                         $account_id = $account->getId();
                         $account_query = new Elastica\Query\Nested();
                         $account_query->setPath('account')->setScoreMode('avg');
                         $account_match_query = new Elastica\Query\Match('account.id', $account_id);
                         $account_bool_query = new Elastica\Query\BoolQuery();
                         $account_bool_query->addMust($account_match_query);
                         $account_query->setQuery($account_bool_query);
                         $lead_query->addMust($account_query);
                     }
                     break;
             }
         }
         if ($date) {
             $dates = [];
             $mode = $date->getMode();
             switch ($mode) {
                 case null:
                 case false:
                     break;
                 case 'timecreated':
                     if (($range = $date->getTimecreated()) == true) {
                         $dates = explode(" - ", $range);
                     }
                     break;
                 default:
                     switch ($mode) {
                         // Today
                         case "1":
                             $dates = [date('Y-m-d\\T00:00:00', time()), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // Last 7 days
                         // Last 7 days
                         case "7":
                             $dates = [date('Y-m-d\\T00:00:00', strtotime('-7 days')), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // Last 30 days
                         // Last 30 days
                         case "30":
                             $dates = [date('Y-m-d\\T00:00:00', strtotime('-30 days')), date('Y-m-d\\T23:59:59', time())];
                             break;
                             // This Month
                         // This Month
                         case "month":
                             $dates = [date('Y-m-01\\T00:00:00', time()), date('Y-m-t\\T23:59:59', time())];
                             break;
                             // Last Month
                         // Last Month
                         case "lmonth":
                             $dates = [date('Y-m-01\\T00:00:00', strtotime('last month')), date('Y-m-t\\T23:59:59', strtotime('last month'))];
                             break;
                             // This Year
                         // This Year
                         case "year":
                             $dates = [date('Y-01-01\\T00:00:00', time()), date('Y-m-d\\T23:59:59', time())];
                             break;
                     }
                     break;
             }
             if ($dates) {
                 $date_query = new Elastica\Query\Range();
                 foreach ($dates as &$date) {
                     $time = strtotime($date);
                     if ($time) {
                         $date = date('Y-m-d\\TH:i:s', $time);
                     } else {
                         $date = date('Y-m-d\\TH:i:s', -9999999999.0);
                     }
                 }
                 @(list($from, $to) = $dates);
                 if (isset($from, $to)) {
                     $date_query->addField('timecreated', ['gte' => $from, 'lte' => $to]);
                     $lead_query->addMust($date_query);
                 }
             }
         }
     }
     return $lead_query;
 }
 /**
  *
  * @param array $location        	
  *
  * @return Elastica\Query $localityQuery
  */
 public static function getLocalityQuery($location)
 {
     $query = new Agent\Elastica\Query\BoolQuery();
     $method = 'addMust';
     if (!isset($location['state']) && !isset($location['zip']) && !isset($location['locality'])) {
         foreach (['phone', 'ipaddress'] as $field) {
             if (isset($location[$field]) && empty($location['state'])) {
                 switch ($field) {
                     case 'ipaddress':
                         $geo = self::$geo;
                         $loc = $geo->getRecord($location['ipaddress']);
                         if ($loc instanceof Record) {
                             $state = $loc->getRegion();
                             if ($state) {
                                 $location['state'] = $state;
                             }
                         }
                         break;
                     case 'phone':
                         $phone = Helper::parse_phonenumber($location['phone'], 'array');
                         if ($phone) {
                             $state = Helper::area_code_to_state($phone[0]);
                             if ($state) {
                                 $location['state'] = $state;
                             }
                         }
                         break;
                 }
             }
         }
     }
     foreach ($location as $field => $value) {
         switch ($field) {
             case 'locality':
                 if (!isset($location['zip'])) {
                     $fields = ['latitude', 'longitude'];
                     $values = is_array($value) ? $value : explode(",", $value);
                     $latlon = count($values) == 2 ? array_combine($fields, $values) : false;
                     if ($latlon) {
                         $path = "location";
                         $nested = new Elastica\Query\Nested();
                         $nested->setPath($path);
                         $bool = new Elastica\Query\BoolQuery();
                         foreach ($latlon as $dim => $coord) {
                             $bool->addMust(new Elastica\Query\Match("{$path}.{$dim}", $coord));
                         }
                         $nested->setQuery($bool);
                         $query->addMust($nested);
                     }
                 }
                 break;
             case 'city':
                 if (!isset($location['locality'])) {
                     $query->addShould(new Elastica\Query\Match($field, $value));
                 }
                 break;
             case 'state':
                 if (!isset($location['locality'])) {
                     $fields = ['state.abbrev', 'state.full'];
                     $values = is_array($value) ? $value : [$value];
                     foreach ($values as $state) {
                         $querystring = new Elastica\Query\QueryString($state);
                         $querystring->setFields($fields);
                         $nested = new Elastica\Query\Nested();
                         $nested->setQuery($querystring);
                         $nested->setPath($field);
                         if (count($values) > 1) {
                             $query->addShould($nested);
                         } else {
                             $query->addMust($nested);
                         }
                     }
                 }
                 break;
             case 'zip':
                 $query->{$method}(new Elastica\Query\Match($field, $value));
                 break;
         }
     }
     $localityQuery = new Elastica\Query($query);
     $localityQuery->setSize(1);
     return $localityQuery;
 }
 /**
  *
  * @param mixed $value        	
  * @param BoolQuery $query        	
  * @param integer|boolean $required        	
  * @param integer|float $boost        	
  *
  * @return BoolQuery $query
  */
 public function getQuery($value = null, $query = null, $required = null, $boost = null)
 {
     $relationship = $this->getRelationship();
     if ($relationship) {
         $attributeDesc = false;
         $criterion = $this->getCriterion();
         if ($criterion) {
             $attribute = $criterion->getAttribute();
             if ($attribute) {
                 $attributeDesc = $attribute->getAttributeDesc();
             }
         }
         if ($attributeDesc) {
             if (!isset($query)) {
                 $query = new BoolQuery();
             }
             $attribute_query = new Elastica\Query\Nested();
             $attribute_query->setPath('attribute');
             $attribute_query->setQuery(new Elastica\Query\Match('attribute.attributeDesc', $attributeDesc));
             $query->addMust($attribute_query);
         }
         $this->setQuery($query);
     }
     return $this->query;
 }
Exemple #5
0
 /**
  * @param string $fieldName
  *
  * @return \Elastica\Query\Nested
  */
 public function createNestedQuery($fieldName)
 {
     $nestedQuery = new Nested();
     return $nestedQuery->setPath($fieldName);
 }
Exemple #6
0
 public function nested($fullPath, $value, $scoreMode = 'none')
 {
     $path = explode($fullPath, '.')[0];
     $nestedQuery = $this->match([$fullPath => $value]);
     $nested = new Nested();
     $nested->setPath($path);
     $nested->setQuery($nestedQuery);
     $nested->setScoreMode($scoreMode);
     $query = $this->newQuery($nested);
     $this->query[] = $query;
     return $query;
 }
 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);
 }