public function indexAction(Request $request, $page = 1) { $getTypes = $request->query->get('type_filters'); $getSubjects = $request->query->get('subject_filters'); $getPublishers = $request->query->get('publisher_filters'); $typeFilters = !empty($getTypes) ? explode(',', $getTypes) : []; $subjectFilters = !empty($getSubjects) ? explode(',', $getSubjects) : []; $publisherFilters = !empty($getPublishers) ? explode(',', $getPublishers) : []; $journalSearcher = $this->get('fos_elastica.index.search.journal'); $boolQuery = new Query\Bool(); $match = new Query\Match(); $match->setField('status', 1); $boolQuery->addMust($match); $match = new Query\Match(); $match->setField('published', true); $boolQuery->addMust($match); if (!empty($typeFilters) || !empty($subjectFilters) || !empty($publisherFilters)) { foreach ($typeFilters as $type) { $match = new Query\Match(); $match->setField('publisher.publisher_type.name', $type); $boolQuery->addMust($match); } foreach ($subjectFilters as $subject) { $match = new Query\Match(); $match->setField('subjects.subject', $subject); $boolQuery->addMust($match); } foreach ($publisherFilters as $publisher) { $match = new Query\Match(); $match->setField('publisher.name', $publisher); $boolQuery->addMust($match); } } $journalQuery = new Query($boolQuery); $typeAgg = new Aggregation\Terms('types'); $typeAgg->setField('publisher.publisher_type.name'); $typeAgg->setOrder('_term', 'asc'); $typeAgg->setSize(0); $journalQuery->addAggregation($typeAgg); $subjectAgg = new Aggregation\Terms('subjects'); $subjectAgg->setField('subjects.subject'); $subjectAgg->setOrder('_term', 'asc'); $subjectAgg->setSize(0); $journalQuery->addAggregation($subjectAgg); $publisherAgg = new Aggregation\Terms('publishers'); $publisherAgg->setField('publisher.name'); $publisherAgg->setOrder('_term', 'asc'); $publisherAgg->setSize(0); $journalQuery->addAggregation($publisherAgg); $adapter = new ElasticaAdapter($journalSearcher, $journalQuery); $pagerfanta = new Pagerfanta($adapter); $pagerfanta->setMaxPerPage(21); $pagerfanta->setCurrentPage($page); $journals = $pagerfanta->getCurrentPageResults(); $types = $adapter->getResultSet()->getAggregation('types')['buckets']; $subjects = $adapter->getResultSet()->getAggregation('subjects')['buckets']; $publishers = $adapter->getResultSet()->getAggregation('publishers')['buckets']; $data = ['types' => $types, 'subjects' => $subjects, 'publishers' => $publishers, 'type_filters' => $typeFilters, 'subject_filters' => $subjectFilters, 'publisher_filters' => $publisherFilters, 'journals' => $journals, 'pagerfanta' => $pagerfanta, 'page' => 'explore']; return $this->render('OjsSiteBundle:Explore:index.html.twig', $data); }
/** * @group functional */ public function testReverseNestedAggregation() { $agg = new Nested('comments', 'comments'); $names = new Terms('name'); $names->setField('comments.name'); $tags = new Terms('tags'); $tags->setField('tags'); $reverseNested = new ReverseNested('main'); $reverseNested->addAggregation($tags); $names->addAggregation($reverseNested); $agg->addAggregation($names); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('comments'); $this->assertArrayHasKey('name', $results); $nameResults = $results['name']; $this->assertCount(3, $nameResults['buckets']); // bob $this->assertEquals('bob', $nameResults['buckets'][0]['key']); $tags = array(array('key' => 'foo', 'doc_count' => 2), array('key' => 'bar', 'doc_count' => 1), array('key' => 'baz', 'doc_count' => 1)); $this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']); // john $this->assertEquals('john', $nameResults['buckets'][1]['key']); $tags = array(array('key' => 'bar', 'doc_count' => 1), array('key' => 'foo', 'doc_count' => 1)); $this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']); // susan $this->assertEquals('susan', $nameResults['buckets'][2]['key']); $tags = array(array('key' => 'baz', 'doc_count' => 1), array('key' => 'foo', 'doc_count' => 1)); $this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']); }
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'])); }
/** * @inheritDoc */ public function applyQueries($query, $alias = null, string $column = null, string $sort = null, array $ids = [], array $order = []) { if (!$query instanceof Filtered) { throw new \InvalidArgumentException('Expected argument of type "Elastica\\Query\\Filtered", ' . get_class($query) . ' given'); } if (!empty($ids)) { /** @var BoolFilter $filter */ $filter = $query->getFilter(); $idsFilter = new Terms(); $idsFilter->setTerms('id', $ids); $filter->addMust($idsFilter); $query->setFilter($filter); } $newQuery = new Query(); $newQuery->setQuery($query); $query = $newQuery; unset($newQuery); if ($column !== null && $sort !== null) { $query->addSort([$column => ['order' => strtolower($sort)]]); } unset($sort); if (!empty($order)) { foreach ($order as $sort => $o) { if ($column !== $sort) { $query->addSort([$sort => ['order' => strtolower($o)]]); } } } return $query; }
/** * @param \Elastica\Query $query * @param \Spryker\Client\Search\Dependency\Plugin\PaginationConfigBuilderInterface $paginationConfig * @param array $requestParameters * * @return void */ protected function addPaginationToQuery(Query $query, PaginationConfigBuilderInterface $paginationConfig, array $requestParameters) { $currentPage = $paginationConfig->getCurrentPage($requestParameters); $itemsPerPage = $paginationConfig->getCurrentItemsPerPage($requestParameters); $query->setFrom(($currentPage - 1) * $itemsPerPage); $query->setSize($itemsPerPage); }
/** * @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'])); }
/** * @group functional */ public function testGeoPoint() { $index = $this->_createIndex(); $type = $index->getType('test'); // Set mapping $type->setMapping(array('location' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('location', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('location', 30, 40); $type->addDocument($doc2); $index->refresh(); // Only one point should be in polygon $query = new Query(); $points = array(array(16, 16), array(16, 20), array(20, 20), array(20, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setPostFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $points = array(array(16, 16), array(16, 40), array(40, 40), array(40, 16), array(16, 16)); $geoFilter = new GeoPolygon('location', $points); $query = new Query(new MatchAll()); $query->setPostFilter($geoFilter); $this->assertEquals(2, $type->search($query)->count()); }
public function testGeoPoint() { $client = $this->_getClient(); $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); // Set mapping $type->setMapping(array('point' => array('type' => 'geo_point'))); // Add doc 1 $doc1 = new Document(1, array('name' => 'ruflin')); $doc1->addGeoPoint('point', 17, 19); $type->addDocument($doc1); // Add doc 2 $doc2 = new Document(2, array('name' => 'ruflin')); $doc2->addGeoPoint('point', 30, 40); $type->addDocument($doc2); $index->optimize(); $index->refresh(); // Only one point should be in radius $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '1km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $this->assertEquals(1, $type->search($query)->count()); // Both points should be inside $query = new Query(); $geoFilter = new GeoDistance('point', array('lat' => 30, 'lon' => 40), '40000km'); $query = new Query(new MatchAll()); $query->setFilter($geoFilter); $index->refresh(); $this->assertEquals(2, $type->search($query)->count()); }
/** * @group functional */ public function testQuery() { $client = $this->_getClient(); $nodes = $client->getCluster()->getNodes(); if (!$nodes[0]->getInfo()->hasPlugin('geocluster-facet')) { $this->markTestSkipped('geocluster-facet plugin not installed'); } $index = $this->_createIndex(); $type = $index->getType('testQuery'); $geoField = 'location'; $type->setMapping(new Mapping($type, array($geoField => array('type' => 'geo_point', 'lat_lon' => true)))); $doc = new Document(1, array('name' => 'item1', 'location' => array(20, 20))); $type->addDocument($doc); $doc = new Document(2, array('name' => 'item2', 'location' => array(20, 20))); $type->addDocument($doc); $doc = new Document(3, array('name' => 'item3', 'location' => array(20, 20))); $type->addDocument($doc); $index->refresh(); $facet = new GeoCluster('clusters'); $facet->setField($geoField)->setFactor(1)->setShowIds(false); $query = new Query(); $query->setFacets(array($facet)); $response = $type->search($query); $facets = $response->getFacets(); $this->assertEquals(1, count($facets['clusters']['clusters'])); $index->delete(); }
public function testResponse() { $index = $this->_createIndex(); $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); $query = new Query(); $query->setQuery(new MatchAll()); $index->refresh(); $resultSet = $type->search($query); $engineTime = $resultSet->getResponse()->getEngineTime(); $shardsStats = $resultSet->getResponse()->getShardsStatistics(); $this->assertInternalType('int', $engineTime); $this->assertTrue(is_array($shardsStats)); $this->assertArrayHasKey('total', $shardsStats); $this->assertArrayHasKey('successful', $shardsStats); }
/** * @group functional */ public function testIndicesQuery() { $docs = array(new Document(1, array('color' => 'blue')), new Document(2, array('color' => 'green')), new Document(3, array('color' => 'blue')), new Document(4, array('color' => 'yellow'))); $index1 = $this->_createIndex(); $index1->addAlias('indices_query'); $index1->getType('test')->addDocuments($docs); $index1->refresh(); $index2 = $this->_createIndex(); $index2->addAlias('indices_query'); $index2->getType('test')->addDocuments($docs); $index2->refresh(); $boolQuery = new Query\BoolQuery(); $boolQuery->addMustNot(new Term(array('color' => 'blue'))); $indicesQuery = new Indices($boolQuery, array($index1->getName())); $boolQuery = new Query\BoolQuery(); $boolQuery->addMustNot(new Term(array('color' => 'yellow'))); $indicesQuery->setNoMatchQuery($boolQuery); $query = new Query(); $query->setPostFilter($indicesQuery); // search over the alias $index = $this->_getClient()->getIndex('indices_query'); $results = $index->search($query); // ensure that the proper docs have been filtered out for each index $this->assertEquals(5, $results->count()); foreach ($results->getResults() as $result) { $data = $result->getData(); $color = $data['color']; if ($result->getIndex() === $index1->getName()) { $this->assertNotEquals('blue', $color); } else { $this->assertNotEquals('yellow', $color); } } }
/** * @group functional */ public function testLookup() { $index = $this->_createIndex(); $type1 = $index->getType('musicians'); $type2 = $index->getType('bands'); //index some test data $type1->addDocuments(array(new Document(1, array('name' => 'robert', 'lastName' => 'plant')), new Document(2, array('name' => 'jimmy', 'lastName' => 'page')), new Document(3, array('name' => 'john paul', 'lastName' => 'jones')), new Document(4, array('name' => 'john', 'lastName' => 'bonham')), new Document(5, array('name' => 'jimi', 'lastName' => 'hendrix')))); $type2->addDocument(new Document('led zeppelin', array('members' => array('plant', 'page', 'jones', 'bonham')))); $index->refresh(); //use the terms lookup feature to query for some data $termsFilter = new Terms(); $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', null); $query = new Query(); $query->setPostFilter($termsFilter); $results = $index->search($query); $this->assertEquals($results->count(), 4, 'Terms lookup with null index'); $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index); $query->setPostFilter($termsFilter); $results = $index->search($query); $this->assertEquals($results->count(), 4, 'Terms lookup with index as object'); //Query with index given as string $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', $index->getName()); $query->setPostFilter($termsFilter); $results = $index->search($query); $this->assertEquals($results->count(), 4, 'Terms lookup with index as string'); //Query with array of options $termsFilter->setLookup('lastName', $type2, 'led zeppelin', 'members', array('index' => $index)); $query->setPostFilter($termsFilter); $results = $index->search($query); $this->assertEquals($results->count(), 4, 'Terms lookup with options array'); $index->delete(); }
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 functional */ public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldmlt'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail')); $type->addDocument($doc); // Refresh index $index->refresh(); $mltQuery = new MoreLikeThis(); $mltQuery->setLike('fake gmail sample'); $mltQuery->setFields(array('email', 'content')); $mltQuery->setMaxQueryTerms(3); $mltQuery->setMinDocFrequency(1); $mltQuery->setMinTermFrequency(1); $query = new Query(); $query->setQuery($mltQuery); $resultSet = $type->search($query); $resultSet->getResponse()->getData(); $this->assertEquals(2, $resultSet->count()); }
/** * @group functional */ public function testScriptedMetricAggregation() { $agg = new ScriptedMetric('scripted', "_agg['durations'] = [:]", "key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;", 'values = []; for (item in _agg.durations) { values.add(item.value) }; return values'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('scripted'); $this->assertEquals(array(100, 50, 150), $results['value'][0]); }
public function testValueCountAggregation() { $agg = new ValueCount("count", "price"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("count"); $this->assertEquals(5, $results['value']); }
public function testDateHistogramAggregation() { $agg = new DateHistogram("hist", "created", "1h"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("hist"); $this->assertEquals(3, sizeof($results['buckets'])); }
public function testMissingAggregation() { $agg = new Missing("missing", "color"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("missing"); $this->assertEquals(1, $results['doc_count']); }
/** * @group functional */ public function testMissingAggregation() { $agg = new Missing('missing', 'color'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('missing'); $this->assertEquals(1, $results['doc_count']); }
/** * @group functional */ public function testValueCountAggregation() { $agg = new ValueCount('count', 'price'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('count'); $this->assertEquals(5, $results['value']); }
/** * @param string $search * @return ResultSet */ public function search($search) { $query = new Query(); $query->setQuery($this->getBoolQuery($search)); $query = $this->searchManager->createQuery()->searchWith($query)->hydrateWith($this->getHydrateQuery())->setMaxResults(50); $this->onSearch($search, $query); return $query->getResult(); }
/** * @group functional */ public function testSetOffsetWorks() { $agg = new DateHistogram('hist', 'created', '1m'); $agg->setOffset('+40s'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']); }
/** * 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."); }
/** * @group functional */ public function testAggregationScriptAsString() { $agg = new Sum('sum'); $agg->setScript("doc['price'].value"); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); $this->assertEquals(5 + 8 + 1 + 3, $results['value']); }
public function testCardinalityAggregation() { $agg = new Cardinality("cardinality"); $agg->setField("color"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("cardinality"); $this->assertEquals(3, $results['value']); }
public function testSumAggregation() { $agg = new Sum("sum"); $agg->setField("price"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("sum"); $this->assertEquals(5 + 8 + 1 + 3, $results['value']); }
/** * @group functional */ public function testAvgAggregation() { $agg = new Avg('avg'); $agg->setField('price'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregations(); $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']['value']); }
/** * @group functional */ public function testCardinalityAggregation() { $agg = new Cardinality('cardinality'); $agg->setField('color'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); $this->assertEquals(3, $results['value']); }
public function testMinAggregation() { $agg = new Min("min_price"); $agg->setField("price"); $query = new Query(); $query->addAggregation($agg); $results = $this->_index->search($query)->getAggregation("min_price"); $this->assertEquals(1, $results['value']); }
/** * @group functional */ public function testSumAggregation() { $agg = new Sum('sum'); $agg->setField('price'); $query = new Query(); $query->addAggregation($agg); $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); $this->assertEquals(5 + 8 + 1 + 3, $results['value']); }