/** * Iterate on index documents and perform $closure * Iteration uses ElasticSearch scroll scan methods * Note: Using setLimit(N) and setFrom(N) for query does not affect actual limit and offset (Limited by ES scan/scroll functionality, see Docs in link) * * See docs about $scroll in link: * @link http://www.elasticsearch.org/guide/reference/api/search/scroll.html * * @param Query|AbstractQuery $query * @param \Closure $closure Receives arguments: function(DataProviderDocument $doc, $i, $total); Return TRUE in $closure if you want to break and stop iteration * @param int $batchSize * @param string $scroll */ public function iterate(Query $query, \Closure $closure, $batchSize = 100, $scroll = '5m') { $response = $this->index->request('_search', 'GET', $query->toArray(), array('search_type' => 'scan', 'scroll' => $scroll, 'size' => $batchSize, 'limit' => 1)); $data = $response->getData(); $scrollId = $data['_scroll_id']; $total = $data['hits']['total']; $i = 0; $response = $this->client->request('_search/scroll', 'GET', $scrollId, array('scroll' => $scroll)); $data = $response->getData(); while (count($data['hits']['hits']) > 0) { foreach ($data['hits']['hits'] as $item) { $itemData = $item['_source']; $doc = new DataProviderDocument($item['_id'], $item['_type'], $itemData); if ($break = $closure($doc, $i, $total)) { break 2; } $i++; } $scrollId = $data['_scroll_id']; $response = $this->client->request('_search/scroll', 'GET', $scrollId, array('scroll' => $scroll)); $data = $response->getData(); } }
public function testToArray() { $query = new Query(); $post_filter = new Term(array('color' => 'green')); $query->setPostFilter($post_filter->toArray()); $data = $query->toArray(); $this->assertArrayHasKey('post_filter', $data); $this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']); $query->setPostFilter(array()); $this->assertArrayNotHasKey('post_filter', $query->toArray()); }
public function toArray() { if ($this->query) { return $this->query->toArray(); } return []; }
protected function getStates() { $results = []; $query = new Elastica\Query(); $query->setSize(0); $agg = new Elastica\Aggregation\Nested('states', 'state'); $st_terms = new Elastica\Aggregation\Terms('abbrev'); $st_terms->setField('state.abbrev'); $st_terms->setOrder('_term', 'asc'); $st_terms->setSize(0); $state_terms = new Elastica\Aggregation\Terms('full'); $state_terms->setField('state.full'); $st_terms->addAggregation($state_terms); $agg->addAggregation($st_terms); $query->addAggregation($agg); /* @var $elastica_client Elastica\Client */ $elastica_client = $this->getServiceLocator()->getServiceLocator()->get('elastica-client'); try { /* @var $response \Elastica\Response */ $response = $elastica_client->request('usgeodb/locality/_search?query_cache=true', Request::GET, $query->toArray()); $data = $response->getData(); $aggregations = isset($data['aggregations']['states']['abbrev']['buckets']) ? $data['aggregations']['states']['abbrev']['buckets'] : []; foreach ($aggregations as $aggregation) { $key = strtoupper($aggregation['key']); $value = ucwords($aggregation['full']['buckets'][0]['key']); $results[$key] = $value; } } catch (\Exception $e) { } return $results; }
/** * @group unit */ public function testToArrayWithLegacyFilter() { $query = new Query(); $this->hideDeprecated(); $post_filter = new Term(array('color' => 'green')); $query->setPostFilter($post_filter); $this->showDeprecated(); $data = $query->toArray(); $this->assertArrayHasKey('post_filter', $data); $this->assertEquals(array('term' => array('color' => 'green')), $data['post_filter']); }
public function testQuery() { $query = new Query(); $mainQuery = new Match(); $mainQuery = $mainQuery->setFieldQuery('test1', 'foo'); $secQuery = new Term(); $secQuery = $secQuery->setTerm('test2', 'bar', 2); $queryRescore = new QueryRescore($secQuery); $queryRescore->setWindowSize(50); $queryRescore->setQueryWeight(0.7); $queryRescore->setRescoreQueryWeight(1.2); $query->setQuery($mainQuery); $query->setRescore($queryRescore); $data = $query->toArray(); $results = $this->_index->search($query); $response = $results->getResponse(); $this->assertEquals(true, $response->isOk()); $this->assertEquals(0, $results->getTotalHits()); }
/** * @group unit */ public function testLegacySetPostFilterToArrayCast() { $this->hideDeprecated(); $query = new Query(); $postFilter = new \Elastica\Filter\Terms(); $postFilter->setTerms('key', array('term')); $query->setPostFilter($postFilter); $postFilter->setTerms('another key', array('another term')); $anotherQuery = new Query(); $anotherQuery->setPostFilter($postFilter); $this->showDeprecated(); $this->assertEquals($query->toArray(), $anotherQuery->toArray()); }
public function testSetFields() { $query = new Query(); $params = array('query' => 'test'); $query->setFields(array('firstname', 'lastname')); $data = $query->toArray(); $this->assertContains('firstname', $data['fields']); $this->assertContains('lastname', $data['fields']); $this->assertEquals(2, count($data['fields'])); }
public function testSetFacets() { $query = new Query(); $facet = new Terms('text'); $query->setFacets(array($facet)); $data = $query->toArray(); $this->assertArrayHasKey('facets', $data); $this->assertEquals(array('text' => array('terms' => array())), $data['facets']); $query->setFacets(array()); $this->assertArrayNotHasKey('facets', $query->toArray()); }
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); }