public function testTest()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $mapping = new Mapping($type, array('name' => array('type' => 'string', 'store' => 'no'), 'dtmPosted' => array('type' => 'date', 'store' => 'no', 'format' => 'yyyy-MM-dd HH:mm:ss')));
     $type->setMapping($mapping);
     $doc = new Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => "2011-06-23 21:53:00"));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => "2011-06-23 09:53:00"));
     $type->addDocument($doc);
     $doc = new Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => "2011-07-08 08:53:00"));
     $type->addDocument($doc);
     $doc = new Document(4, array('name' => 'elastica search', 'dtmPosted' => "2011-07-08 01:53:00"));
     $type->addDocument($doc);
     $facet = new DateHistogram('dateHist1');
     $facet->setInterval("day");
     $facet->setField("dtmPosted");
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(4, $response->getTotalHits());
     $this->assertEquals(2, count($facets['dateHist1']['entries']));
 }
 /**
  * @group functional
  */
 public function testFactor()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $mapping = new Mapping($type, array('name' => array('type' => 'string', 'store' => 'no'), 'dtmPosted' => array('type' => 'long', 'store' => 'no')));
     $type->setMapping($mapping);
     $doc = new Document(1, array('name' => 'nicolas ruflin', 'dtmPosted' => 1308865980));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'raul martinez jr', 'dtmPosted' => 1308822780));
     $type->addDocument($doc);
     $doc = new Document(3, array('name' => 'rachelle clemente', 'dtmPosted' => 1310115180));
     $type->addDocument($doc);
     $doc = new Document(4, array('name' => 'elastica search', 'dtmPosted' => 1310089980));
     $type->addDocument($doc);
     $facet = new DateHistogram('dateHist1');
     $facet->setInterval('day');
     $facet->setField('dtmPosted');
     $facet->setFactor(1000);
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(4, $response->getTotalHits());
     $this->assertEquals(2, count($facets['dateHist1']['entries']));
 }
Example #3
0
 /**
  * @group functional
  */
 public function testFilter()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $type->addDocument(new Document(1, array('color' => 'red')));
     $type->addDocument(new Document(2, array('color' => 'green')));
     $type->addDocument(new Document(3, array('color' => 'blue')));
     $index->refresh();
     $filter = new Term(array('color' => 'red'));
     $facet = new Filter('test');
     $facet->setFilter($filter);
     $query = new Query();
     $query->addFacet($facet);
     $resultSet = $type->search($query);
     $facets = $resultSet->getFacets();
     $this->assertEquals(1, $facets['test']['count']);
 }
Example #4
0
 /**
  *
  * Add facets on top of Elastica query
  * @param \Elastica\Query $query
  * @param array $options
  * @param \Elastica\Filter\Bool $mainFilter
  */
 public function addFacets(\Elastica\Query $query, $options, \Elastica\Filter\Bool $mainFilter)
 {
     // only add additional facets if a specific module is selected
     if (!$this->isOptionsModuleFilterValid($options)) {
         return;
     }
     $module = array_shift($options['moduleFilter']);
     $facetDefs = $this->loadFacetDefs($module);
     foreach ($facetDefs as $field => $facetDef) {
         if ($facet = FacetFactory::get($facetDef['type'])) {
             // set options from facet definition
             $facet->setOptions($facetDef['options']);
             // get Elastica facet object and assign it to the query
             if ($eFacet = $facet->getFacet($field, $mainFilter)) {
                 $query->addFacet($eFacet);
             }
         }
     }
 }
Example #5
0
 /**
  * @group functional
  */
 public function testFacetScript()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $doc = new Document(1, array('name' => 'rodolfo', 'last_name' => 'moraes'));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'jose', 'last_name' => 'honjoya'));
     $type->addDocument($doc);
     $facet = new Terms('test');
     $facet->setField('name');
     $facet->setScript('term + " "+doc["last_name"].value');
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(2, count($facets['test']['terms']));
 }
Example #6
0
 public function testQuery()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $doc = new Document(1, array('name' => 'nicolas ruflin'));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'ruflin test'));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'nicolas helloworld'));
     $type->addDocument($doc);
     $facet = new Terms('test');
     $facet->setField('name');
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(3, count($facets['test']['terms']));
 }
Example #7
0
 public function testQuery()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $doc = new Document(1, array('name' => 'tom', 'paid' => 7));
     $type->addDocument($doc);
     $doc = new Document(2, array('name' => 'tom', 'paid' => 2));
     $type->addDocument($doc);
     $doc = new Document(3, array('name' => 'tom', 'paid' => 5));
     $type->addDocument($doc);
     $doc = new Document(4, array('name' => 'mike', 'paid' => 13));
     $type->addDocument($doc);
     $doc = new Document(5, array('name' => 'mike', 'paid' => 1));
     $type->addDocument($doc);
     $doc = new Document(6, array('name' => 'mike', 'paid' => 15));
     $type->addDocument($doc);
     $facet = new TermsStats('test');
     $facet->setKeyField('name');
     $facet->setValueField('paid');
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(2, count($facets['test']['terms']));
     foreach ($facets['test']['terms'] as $facet) {
         if ($facet['term'] === 'tom') {
             $this->assertEquals(14, $facet['total']);
         }
         if ($facet['term'] === 'mike') {
             $this->assertEquals(29, $facet['total']);
         }
     }
 }
 /**
  * @group functional
  */
 public function testStatisticalWithSetFields()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = $index->getType('helloworld');
     $doc = new Document(1, array('price' => 10, 'price2' => 20));
     $type->addDocument($doc);
     $doc = new Document(2, array('price' => 35, 'price2' => 70));
     $type->addDocument($doc);
     $doc = new Document(2, array('price' => 45, 'price2' => 90));
     $type->addDocument($doc);
     $facet = new Statistical('stats');
     $facet->setFields(array('price', 'price2'));
     $query = new Query();
     $query->addFacet($facet);
     $query->setQuery(new MatchAll());
     $index->refresh();
     $response = $type->search($query);
     $facets = $response->getFacets();
     $this->assertEquals(165, $facets['stats']['total']);
     $this->assertEquals(10, $facets['stats']['min']);
     $this->assertEquals(90, $facets['stats']['max']);
 }
Example #9
0
 /**
  * @group unit
  */
 public function testAddFacetToArrayCast()
 {
     $query = new Query();
     $facet = new Terms('text');
     $query->addFacet($facet);
     $facet->setName('another text');
     $anotherQuery = new Query();
     $anotherQuery->addFacet($facet);
     $this->assertNotEquals($query->toArray(), $anotherQuery->toArray());
 }
 /**
  *
  * Add facets on elastic query object
  * @param \Elastica\Query $query
  * @param array $options
  * @param \Elastica\Filter\AbstractFilter $mainFilter
  */
 protected function addFacets(\Elastica\Query $query, $options = array(), \Elastica\Filter\AbstractFilter $mainFilter = null)
 {
     // module facet (note: would be less confusing to give another name instead of _type)
     if (!empty($options['apply_module_facet'])) {
         $typeFacet = new \Elastica\Facet\Terms('_type');
         $typeFacet->setField('_type');
         // need to add filter for facet too
         if (isset($mainFilter)) {
             $typeFacet->setFilter($mainFilter);
         }
         $query->addFacet($typeFacet);
     }
     // handle secondary facets
     $this->facetHandler->addFacets($query, $options, $mainFilter);
 }