Author: Nicolas Ruflin (spam@ruflin.com)
Inheritance: extends AbstractQuery
 /**
  * (non-PHPdoc)
  *
  * @see \Agent\Entity\Relationship::getQuery()
  */
 public function getQuery($value = null, $query = null, $required = null, $boost = null)
 {
     $query = parent::getQuery($value, $query, $required, $boost);
     $method = 'addMust';
     // $required ? 'addMust' : 'addShould';
     if (isset($value) && is_array($value) && count($value) == 2) {
         $range = new Elastica\Query\Range();
         $field = $this->getCriterion()->getTypeField();
         switch ($field) {
             case '_date':
                 foreach ($value 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);
                     }
                 }
                 break;
         }
         @(list($from, $to) = $value);
         if (isset($from, $to)) {
             $range->addField($field, ['gt' => $from, 'lt' => $to]);
             $query->{$method}($range);
         }
     }
     return $query;
 }
 public function testConstruct()
 {
     $ranges = array('from' => 20, 'to' => 40);
     $range = new Range('age', $ranges);
     $expectedArray = array('range' => array('age' => $ranges));
     $this->assertEquals($expectedArray, $range->toArray());
 }
Exemple #3
0
 /**
  * Remove all outdated documents in the index of the configured type
  *
  * @return void
  */
 public function cleanUp()
 {
     $dateRange = new Range();
     $dateRange->addField(self::$ETL_TIMESTAMP, array('lt' => $this->timestamp));
     $query = new Query();
     $query->setQuery($dateRange);
     $this->type->deleteByQuery($query);
     $this->log("Removed outdated documents that were outdated.");
 }
Exemple #4
0
 /**
  * (non-PHPdoc)
  *
  * @see \Agent\Entity\Relationship::getQuery()
  */
 public function getQuery($value = null, $query = null, $required = null, $boost = null)
 {
     $from = $to = null;
     if ($value && is_array($value) && count($value) == 2) {
         @(list($from, $to) = $value);
     }
     $query = parent::getQuery(null, $query, $required, $boost);
     $method = 'addMust';
     // $required ? 'addMust' : 'addShould';
     if (isset($from, $to)) {
         $range = new Elastica\Query\Range();
         $range->addField($this->getCriterion()->getTypeField(), ['gte' => $from, 'lte' => $to]);
         $query->{$method}($range);
     }
     return $query;
 }
Exemple #5
0
 /**
  * (non-PHPdoc)
  *
  * @see \Agent\Entity\Relationship::getQuery()
  */
 public function getQuery($value = null, $query = null, $required = null, $boost = null)
 {
     $query = parent::getQuery($value, $query, $required, $boost);
     $method = 'addMust';
     // $required ? 'addMust' : 'addShould';
     if (isset($value)) {
         $range = new Elastica\Query\Range();
         $field = $this->getCriterion()->getTypeField();
         switch ($field) {
             case '_date':
                 $time = strtotime($value);
                 if ($time) {
                     $value = date('Y-m-d\\TH:i:s', $time);
                 } else {
                     $value = date('Y-m-d\\TH:i:s', -9999999999.0);
                 }
                 break;
         }
         $range->addField($field, ['lte' => $value]);
         $query->{$method}($range);
     }
     return $query;
 }
 protected function searchDocuments()
 {
     dump(__METHOD__);
     $client = $this->getClient();
     $index = $client->getIndex($this->getIndexName());
     $type = $index->getType($this->getTypeName());
     if (true) {
         $query = json_decode('{"query":{"bool":{"must":[{"term":{"uid":19246}},{"term":{"name":"XXXXXXXXXX"}},{"term":{"op":30}}],"filter":[{"range":{"level":{"from":10,"to":300}}},{"range":{"addtime":{"gte":"20150706T145200+0800","lte":"20150707T145203+0800"}}}]}}}', true);
         dump($query);
         $path = $index->getName() . '/' . $type->getName() . '/_search';
         $response = $client->request($path, Request::GET, $query);
         $this->assertTrue($response->isOk());
         //            dump($response->getEngineTime());
         //            dump($response->getQueryTime());
         //            dump($response->getShardsStatistics());
         //            dump($response->getStatus()); // http status code
         //            dump($response->getTransferInfo());
         dump($response->getData()['hits']['hits']);
     }
     if (false) {
         $search = new Search($client);
         $search->addIndex($index)->addType($type);
         $query = new Query\BoolQuery();
         //        $query->setFrom(0);
         //        $query->setSize(10);
         //        $query->setSort(['uid' => 'asc']);
         //        $query->setFields(['snsid', 'uid']);
         //        $query->setHighlight(['fields' => 'uid']);
         //        $query->setExplain(true);
         //        $term = new Query\Term(['name' => 'XXXXXXXXXX']);
         //        $query->setQuery($term);
         $query->addMust(new Term(['uid' => 19246]));
         $query->addMust(new Term(['name' => 'XXXXXXXXXX']));
         $query->addMust(new Term(['op' => 30]));
         //        $query->addMustNot(new Term(['country' => 'CN']));
         $range = new Query\Range('level', ['from' => 10, 'to' => 300]);
         $query->addFilter($range);
         $range = new Query\Range();
         $range->addField('addtime', ['gte' => '20150706T145200+0800', 'lte' => '20150707T145203+0800']);
         $query->addFilter($range);
         $search->setQuery($query);
         $resultSet = $search->search();
         dump('Hit: ' . $resultSet->count());
         $queryArray = $resultSet->getQuery()->toArray();
         dump(json_encode($queryArray));
         dump($resultSet->getResponse()->getData()['hits']['hits']);
         dump('query time: ' . \PHP_Timer::secondsToTimeString($resultSet->getResponse()->getQueryTime()));
     }
 }
Exemple #7
0
 /**
  * @group functional
  */
 public function testSearchWithErrorWithKeys()
 {
     $type = $this->_createType();
     $index = $type->getIndex();
     $client = $index->getClient();
     $multiSearch = new MultiSearch($client);
     $searchGood = new Search($client);
     $searchGood->setQuery('bunny');
     $searchGood->addIndex($index)->addType($type);
     $multiSearch->addSearch($searchGood, 'search1');
     $searchBad = new Search($client);
     $searchBadQuery = new Range();
     $searchBadQuery->addField('bad', array('_id' => 0));
     $searchBadQuery->setParam('_cache', true);
     $searchBad->setQuery($searchBadQuery);
     $searchBad->addIndex($index)->addType($type);
     $multiSearch->addSearch($searchBad);
     $multiResultSet = $multiSearch->search();
     $this->assertInstanceOf('Elastica\\Multi\\ResultSet', $multiResultSet);
     $resultSets = $multiResultSet->getResultSets();
     $this->assertInternalType('array', $resultSets);
     $this->assertArrayHasKey('search1', $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets['search1']);
     $this->assertSame($searchGood->getQuery(), $resultSets['search1']->getQuery());
     $this->assertSame(6, $resultSets['search1']->getTotalHits());
     $this->assertCount(6, $resultSets['search1']);
     $this->assertArrayHasKey(0, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[0]);
     $this->assertSame($searchBad->getQuery(), $resultSets[0]->getQuery());
     $this->assertSame(0, $resultSets[0]->getTotalHits());
     $this->assertCount(0, $resultSets[0]);
     $this->assertTrue($resultSets[0]->getResponse()->hasError());
     $this->assertTrue($multiResultSet->hasError());
 }
 /**
  *
  * @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;
 }
 /**
  * Find all documents in a given range. The range is provided as an array with
  * at least either a 'lt' or 'lte' key and a 'gt' or 'gte' key.
  *
  * 'lt'  stands for less than
  * 'lte' for less than or equal to
  * 'gt'  for greater than
  * 'gte' for greater than or equal to
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
  *
  * @param string $field
  * @param array $range
  * @param bool $timeZone
  * @param bool $format
  * @return Query
  */
 public function range($field, array $range, $timeZone = false, $format = false)
 {
     $range = new Range($field, $range);
     if ($timeZone) {
         $range->setParam('time_zone', $timeZone);
     }
     if ($format) {
         $range->setParam('format', $format);
     }
     $query = $this->newQuery($range);
     $this->query[] = $query;
     return $query;
 }
Exemple #10
0
 /**
  * @param string $fieldName
  * @param float $minValue
  * @param float $maxValue
  * @param string $greaterParam
  * @param string $lessParam
  *
  * @return \Elastica\Query\Range
  */
 public function createRangeQuery($fieldName, $minValue, $maxValue, $greaterParam = 'gte', $lessParam = 'lte')
 {
     $rangeQuery = new Range();
     $rangeQuery->addField($fieldName, [$greaterParam => $minValue, $lessParam => $maxValue]);
     return $rangeQuery;
 }