Author: Nicolas Ruflin (spam@ruflin.com)
 /**
  * Reindex documents from an old index to a new index.
  *
  * @link https://www.elastic.co/guide/en/elasticsearch/guide/master/reindex.html
  *
  * @param \Elastica\Index $oldIndex
  * @param \Elastica\Index $newIndex
  * @param array           $options  keys: CrossIndex::OPTION_* constants
  *
  * @return \Elastica\Index The new index object
  */
 public static function reindex(Index $oldIndex, Index $newIndex, array $options = array())
 {
     // prepare search
     $search = new Search($oldIndex->getClient());
     $options = array_merge(array(self::OPTION_TYPE => null, self::OPTION_QUERY => new MatchAll(), self::OPTION_EXPIRY_TIME => '1m', self::OPTION_SIZE_PER_SHARD => 1000), $options);
     $search->addIndex($oldIndex);
     if (isset($options[self::OPTION_TYPE])) {
         $type = $options[self::OPTION_TYPE];
         $search->addTypes(is_array($type) ? $type : array($type));
     }
     $search->setQuery($options[self::OPTION_QUERY]);
     // search on old index and bulk insert in new index
     $scanAndScroll = new ScanAndScroll($search, $options[self::OPTION_EXPIRY_TIME], $options[self::OPTION_SIZE_PER_SHARD]);
     foreach ($scanAndScroll as $resultSet) {
         $bulk = new Bulk($newIndex->getClient());
         $bulk->setIndex($newIndex);
         foreach ($resultSet as $result) {
             $action = new Bulk\Action();
             $action->setType($result->getType());
             $action->setId($result->getId());
             $action->setSource($result->getData());
             $bulk->addAction($action);
         }
         $bulk->send();
     }
     $newIndex->refresh();
     return $newIndex;
 }
Exemple #2
0
 /**
  * {@inheritDoc}
  *
  * @api
  */
 public function getItems($offset, $limit)
 {
     $query = $this->search->getQuery();
     $query->setFrom($offset);
     $query->setSize($limit);
     return $this->search->search()->getResults();
 }
Exemple #3
0
 /**
  * Run a search.
  *
  * @param Query $q
  * @return ResultSet
  */
 protected function doSearch(Query $q)
 {
     $search = new Search($this->client);
     $search->addIndex('bash')->addType('bash');
     $search->setQuery($q);
     return $search->search();
 }
 public function findByCriteria(TrackRepositoryCriteria $criteria)
 {
     $boolQuery = new \Elastica\Query\BoolQuery();
     if ($criteria->albumId()) {
         $query = new \Elastica\Query\Term();
         $query->setParam('album.id', $criteria->albumId());
         $boolQuery->addMust($query);
     }
     if ($criteria->albumTitle()) {
         $query = new \Elastica\Query\Match();
         $query->setFieldQuery('album.title', $criteria->albumTitle());
         $query->setFieldFuzziness('album.title', 2);
         $boolQuery->addMust($query);
     }
     if ($criteria->trackName()) {
         $query = new \Elastica\Query\Match();
         $query->setFieldQuery('name', $criteria->trackName());
         $query->setFieldFuzziness('name', 2);
         $boolQuery->addMust($query);
     }
     if ($criteria->composer()) {
         $query = new \Elastica\Query\Match();
         $query->setFieldQuery('composer', $criteria->composer());
         $query->setFieldFuzziness('composer', 2);
         $boolQuery->addMust($query);
     }
     $this->elasticaSearch->setQuery($boolQuery);
     $query = $this->elasticaSearch->getQuery();
     $query->setSize($criteria->size());
     $query->setFrom(($criteria->page() - 1) * $criteria->size());
     $query->addSort(['name_not_analyzed' => ['order' => 'asc']]);
     return $this->buildEntities($this->elasticaSearch->search()->getResults());
 }
 /**
  * @group functional
  */
 public function testShouldReturnTheRightNumberOfResult()
 {
     $filter = new Nested();
     $this->assertEquals(array('nested' => array()), $filter->toArray());
     $query = new Terms();
     $query->setTerms('hobby', array('guitar'));
     $filter->setPath('hobbies');
     $filter->setFilter($query);
     $client = $this->_getClient();
     $search = new Search($client);
     $index = $this->_getIndexForTest();
     $search->addIndex($index);
     $resultSet = $search->search($filter);
     $this->assertEquals(1, $resultSet->getTotalHits());
     $filter = new Nested();
     $this->assertEquals(array('nested' => array()), $filter->toArray());
     $query = new Terms();
     $query->setTerms('hobby', array('opensource'));
     $filter->setPath('hobbies');
     $filter->setFilter($query);
     $client = $this->_getClient();
     $search = new Search($client);
     $index = $this->_getIndexForTest();
     $search->addIndex($index);
     $resultSet = $search->search($filter);
     $this->assertEquals(2, $resultSet->getTotalHits());
 }
 /**
  * @group functional
  */
 public function testHasParent()
 {
     $index = $this->_createIndex();
     $shopType = $index->getType('shop');
     $productType = $index->getType('product');
     $mapping = new Mapping();
     $mapping->setParent('shop');
     $productType->setMapping($mapping);
     $shopType->addDocuments(array(new Document('zurich', array('brand' => 'google')), new Document('london', array('brand' => 'apple'))));
     $doc1 = new Document(1, array('device' => 'chromebook'));
     $doc1->setParent('zurich');
     $doc2 = new Document(2, array('device' => 'macmini'));
     $doc2->setParent('london');
     $productType->addDocument($doc1);
     $productType->addDocument($doc2);
     $index->refresh();
     // All documents
     $parentQuery = new HasParent(new MatchAll(), $shopType->getName());
     $search = new Search($index->getClient());
     $results = $search->search($parentQuery);
     $this->assertEquals(2, $results->count());
     $match = new Match();
     $match->setField('brand', 'google');
     $parentQuery = new HasParent($match, $shopType->getName());
     $search = new Search($index->getClient());
     $results = $search->search($parentQuery);
     $this->assertEquals(1, $results->count());
     $result = $results->current();
     $data = $result->getData();
     $this->assertEquals($data['device'], 'chromebook');
 }
 /**
  * @return \Elastica\ResultSet
  */
 public function getSearchResult()
 {
     $index = $this->search->getIndex($this->getIndexName() . '_' . $this->language);
     $search = new Search($this->search->getClient());
     $search->addIndex($index);
     $search->addType($index->getType($this->indexType));
     $result = $search->search($this->query);
     return $result;
 }
 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()));
     }
 }
 /**
  * index: 12 docs, 2 shards.
  *
  * @return Search
  */
 private function _prepareSearch()
 {
     $index = $this->_createIndex('', true, 2);
     $index->refresh();
     $docs = array();
     for ($x = 1; $x <= 12; $x++) {
         $docs[] = new Document($x, array('id' => $x, 'key' => 'value'));
     }
     $type = $index->getType('scanAndScrollTest');
     $type->addDocuments($docs);
     $index->refresh();
     $search = new Search($this->_getClient());
     $search->addIndex($index)->addType($type);
     return $search;
 }
 /**
  * Perform the show action for a block
  *
  * @param ReadBlockInterface $block
  *
  * @return Response
  */
 public function show(ReadBlockInterface $block)
 {
     $request = $this->requestStack->getCurrentRequest();
     $data = $request->get('elastica_search');
     $searchData = array();
     if (is_array($data) && array_key_exists('search', $data) && null !== $data['search']) {
         $searchParameter = $data['search'];
         $index = $this->client->getIndex($this->indexName);
         $qb = new QueryBuilder();
         $search = new Search($this->client);
         $search->addIndex($index);
         $search->setQuery($qb->query()->filtered($qb->query()->query_string($searchParameter), $qb->filter()->bool()->addMust($qb->filter()->term(array('language' => $request->getLocale())))));
         $searchData = $search->search(null, array('limit' => $block->getAttribute('searchLimit')));
     }
     return $this->render('OpenOrchestraElasticaFrontBundle:Block/List:show.html.twig', array('searchData' => $searchData));
 }
 /**
  * @group functional
  */
 public function testMatchAllIndicesTypes()
 {
     $index1 = $this->_createIndex();
     $index2 = $this->_createIndex();
     $client = $index1->getClient();
     $search1 = new Search($client);
     $resultSet1 = $search1->search(new MatchAll());
     $doc1 = new Document(1, array('name' => 'ruflin'));
     $doc2 = new Document(1, array('name' => 'ruflin'));
     $index1->getType('test')->addDocument($doc1);
     $index2->getType('test')->addDocument($doc2);
     $index1->refresh();
     $index2->refresh();
     $search2 = new Search($client);
     $resultSet2 = $search2->search(new MatchAll());
     $this->assertEquals($resultSet1->getTotalHits() + 2, $resultSet2->getTotalHits());
 }
 /**
  *
  * @return bool
  */
 public function searchHtml()
 {
     $q = $this->getQuery();
     if (!$q) {
         echo json_encode(null);
         exit;
     }
     $es = $this->app->container->get('elastica.client');
     $search = new Search($es);
     $results = $search->search($q);
     $hits = [];
     foreach ($results->getResults() as $result) {
         $result = $result->getHit();
         $hits[] = $result['_source'];
     }
     $this->app->render('partials/searchResutls.html', ['packages' => $hits]);
     return true;
 }
 /**
  * Get Query Count
  *
  * @param string $index        	
  * @param string $type        	
  *
  * @return number
  */
 public function getElasticaCount($index = false, $type = false)
 {
     $elastica_query = new Query();
     $elastica_client = $this->getElasticaClient();
     // If you want to restrict your search to a particular index then get
     // that
     $elastica_index = $index ? $elastica_client->getIndex($index) : false;
     // If you want to restrict your search to a particular type then get
     // that
     $elastica_type = $elastica_index && $type ? $elastica_index->getType($type) : false;
     $elastica_search = new Search($elastica_client);
     if ($elastica_index) {
         $elastica_search->addIndex($elastica_index);
     }
     if ($elastica_type) {
         $elastica_search->addType($elastica_type);
     }
     return $elastica_search->count($elastica_query);
 }
Exemple #14
0
 public function readData($source_definition, $rest_parameters = [])
 {
     Pager::setDefaultLimit(500);
     list($limit, $offset) = Pager::calculateLimitAndOffset();
     $client = new Client(['host' => $source_definition['host'], 'port' => $source_definition['port'], 'username' => $source_definition['username'], 'password' => $source_definition['password']]);
     $index = $client->getIndex($source_definition['es_index']);
     $type = $index->getType($source_definition['es_type']);
     $search = new Search($client);
     $search->addIndex($index);
     $search->addType($type);
     $query_param = \Input::get('query');
     if (empty($query_param)) {
         $query = new MatchAll();
         $search->setQuery($query);
     } else {
         $query = new SimpleQueryString($query_param);
         $search->setQuery($query);
     }
     $search->getQuery()->setFrom($offset);
     $search->getQuery()->setSize($limit);
     $resultSet = $search->search();
     $data = new Data();
     $data_results = [];
     foreach ($resultSet->getResults() as $result) {
         $data_result = $result->getData();
         unset($data_result['__tdt_etl_timestamp__']);
         $data_results[] = $data_result;
     }
     $data->data = $data_results;
     if ($resultSet->getTotalHits() > 0) {
         $paging = Pager::calculatePagingHeaders($limit, $offset, $resultSet->getTotalHits());
         $data->paging = $paging;
     }
     $data->preferred_formats = $this->getPreferredFormats();
     return $data;
 }
Exemple #15
0
 /**
  * @param Response   $childResponse
  * @param BaseSearch $search
  *
  * @return \Elastica\ResultSet
  */
 private function buildResultSet(Response $childResponse, BaseSearch $search)
 {
     return $search->getResultSetBuilder()->buildResultSet($childResponse, $search->getQuery());
 }
Exemple #16
0
<?php

require_once 'vendor/autoload.php';
use Elastica\Client;
use Elastica\Query;
use Elastica\Query\QueryString;
use Elastica\Search;
$client = new Client();
$index = $client->getIndex('index000');
$type = $index->getType('jdbc');
$search = new Search($client);
$query = new QueryString('クエリー');
$search->setQuery($query);
$resultSet = $search->search();
foreach ($resultSet as $res) {
    $d = $res->getData();
    print_r($d);
}
?>

Exemple #17
0
 /**
  * @param \Elastica\Search $search
  *
  * @return array
  */
 protected function _getSearchDataHeader(BaseSearch $search)
 {
     $header = $search->getOptions();
     if ($search->hasIndices()) {
         $header['index'] = $search->getIndices();
     }
     if ($search->hasTypes()) {
         $header['types'] = $search->getTypes();
     }
     // Filter options accepted in the "header"
     return array_intersect_key($header, array_flip(self::$HEADER_OPTIONS));
 }
Exemple #18
0
 /**
  * @group functional
  */
 public function testQueryCacheOption()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('zero');
     $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true);
     $type = $index->getType('zeroType');
     $type->addDocuments(array(new Document(1, array('id' => 1, 'username' => 'farrelley')), new Document(2, array('id' => 2, 'username' => 'bunny'))));
     $index->refresh();
     $aggregation = new Aggregation\Terms('username');
     $aggregation->setField('username');
     $query = new Query();
     $query->addAggregation($aggregation);
     $search = new Search($client);
     $search->addIndex($index);
     $search->setQuery($query);
     $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT);
     $search->setOption(Search::OPTION_QUERY_CACHE, true);
     // before search query cache should be empty
     $statsData = $index->getStats()->getData();
     $queryCache = $statsData['_all']['primaries']['query_cache'];
     $this->assertEquals(0, $queryCache['memory_size_in_bytes']);
     $this->assertEquals(0, $queryCache['evictions']);
     $this->assertEquals(0, $queryCache['hit_count']);
     $this->assertEquals(0, $queryCache['miss_count']);
     // first search should result in cache miss and save data to cache
     $search->search();
     $index->getStats()->refresh();
     $statsData = $index->getStats()->getData();
     $queryCache = $statsData['_all']['primaries']['query_cache'];
     $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']);
     $this->assertEquals(0, $queryCache['evictions']);
     $this->assertEquals(0, $queryCache['hit_count']);
     $this->assertEquals(1, $queryCache['miss_count']);
     // next search should result in cache hit
     $search->search();
     $index->getStats()->refresh();
     $statsData = $index->getStats()->getData();
     $queryCache = $statsData['_all']['primaries']['query_cache'];
     $this->assertNotEquals(0, $queryCache['memory_size_in_bytes']);
     $this->assertEquals(0, $queryCache['evictions']);
     $this->assertEquals(1, $queryCache['hit_count']);
     $this->assertEquals(1, $queryCache['miss_count']);
 }
Exemple #19
0
 /**
  * @group functional
  */
 public function testAddDocumentHashId()
 {
     $index = $this->_createIndex();
     $type = $index->getType('test2');
     $hashId = '#1';
     $doc = new Document($hashId, array('name' => 'ruflin'));
     $type->addDocument($doc);
     $index->refresh();
     $search = new Search($index->getClient());
     $search->addIndex($index);
     $resultSet = $search->search(new MatchAll());
     $this->assertEquals($hashId, $resultSet->current()->getId());
     $doc = $type->getDocument($hashId);
     $this->assertEquals($hashId, $doc->getId());
 }
Exemple #20
0
 protected function buildQuery(array $classes)
 {
     $searchQuery = new Search($this->client);
     $searchQuery->setOption(Search::OPTION_VERSION, true);
     foreach ($classes as $class) {
         if ($class->index) {
             $indexObject = $this->getIndex($class->index);
             $searchQuery->addIndex($indexObject);
             if ($class->type) {
                 $searchQuery->addType($indexObject->getType($class->type));
             }
         }
     }
     return $searchQuery;
 }
Exemple #21
0
 /**
  * @param string|array|\Elastica\Query $query
  * @param int|array                    $options
  * @param BuilderInterface             $builder
  *
  * @return Search
  */
 public function createSearch($query = '', $options = null, BuilderInterface $builder = null)
 {
     $search = new Search($this->getClient(), $builder);
     $search->addIndex($this);
     $search->setOptionsAndQuery($options, $query);
     return $search;
 }
 /**
  * @param int $from
  * @param int $size
  *
  * @return array
  */
 public function allStats($from = null, $size = null)
 {
     $_query = ['query' => ['term' => ['fabric.facility.raw' => 'cloud/cli/metrics']], 'size' => $size ?: 99999999, 'from' => $from ?: 0, 'sort' => ['@timestamp' => ['order' => 'desc']]];
     $_query = new Query($_query);
     $_search = new Search($this->_client);
     $_result = $_search->search($_query)->getResults();
     $_data = [];
     foreach ($_result as $_hit) {
         $_data[] = $_hit->getSource();
     }
     return $_data;
 }
Exemple #23
0
 /**
  * @group functional
  */
 public function testGlobalSearchTypeSearchWithKeys()
 {
     $type = $this->_createType();
     $index = $type->getIndex();
     $client = $index->getClient();
     $multiSearch = new MultiSearch($client);
     $search1 = new Search($client);
     $search1->addIndex($index)->addType($type);
     $query1 = new Query();
     $termQuery1 = new Term();
     $termQuery1->setTerm('username', 'farrelley');
     $query1->setQuery($termQuery1);
     $query1->setSize(2);
     $search1->setQuery($query1);
     $multiSearch->addSearch($search1);
     $this->assertCount(1, $multiSearch->getSearches());
     $search2 = new Search($client);
     $search2->addIndex($index)->addType($type);
     $query2 = new Query();
     $termQuery2 = new Term();
     $termQuery2->setTerm('username', 'bunny');
     $query2->setQuery($termQuery2);
     $query2->setSize(3);
     $search2->setQuery($query2);
     $multiSearch->addSearch($search2);
     $multiSearch->setSearchType(Search::OPTION_SEARCH_TYPE_COUNT);
     $multiResultSet = $multiSearch->search();
     $this->assertInstanceOf('Elastica\\Multi\\ResultSet', $multiResultSet);
     $this->assertCount(2, $multiResultSet);
     $this->assertInstanceOf('Elastica\\Response', $multiResultSet->getResponse());
     $resultSets = $multiResultSet->getResultSets();
     $this->assertInternalType('array', $resultSets);
     $this->assertArrayHasKey(0, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[0]);
     $this->assertCount(0, $resultSets[0]);
     $this->assertSame($query1, $resultSets[0]->getQuery());
     $this->assertEquals(3, $resultSets[0]->getTotalHits());
     $this->assertArrayHasKey(1, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[1]);
     $this->assertCount(0, $resultSets[1]);
     $this->assertSame($query2, $resultSets[1]->getQuery());
     $this->assertEquals(6, $resultSets[1]->getTotalHits());
     $search1->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_QUERY_AND_FETCH);
     $multiResultSet = $multiSearch->search();
     $this->assertInstanceOf('Elastica\\Multi\\ResultSet', $multiResultSet);
     $this->assertCount(2, $multiResultSet);
     $this->assertInstanceOf('Elastica\\Response', $multiResultSet->getResponse());
     $resultSets = $multiResultSet->getResultSets();
     $this->assertInternalType('array', $resultSets);
     $this->assertArrayHasKey(0, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[0]);
     $this->assertCount(2, $resultSets[0]);
     $this->assertSame($query1, $resultSets[0]->getQuery());
     $this->assertEquals(3, $resultSets[0]->getTotalHits());
     $this->assertArrayHasKey(1, $resultSets);
     $this->assertInstanceOf('Elastica\\ResultSet', $resultSets[1]);
     $this->assertCount(0, $resultSets[1]);
     $this->assertSame($query2, $resultSets[1]->getQuery());
     $this->assertEquals(6, $resultSets[1]->getTotalHits());
 }
 /**
  * Create a new elastica search.
  *
  * @param Client $client
  * @param Index $index
  * @param string|array $types
  * @return Search
  */
 protected function newSearch(Client $client, Index $index, $types)
 {
     if (is_string($types)) {
         $types = [$types];
     }
     $search = new Search($client);
     $search->addIndex($index);
     $search->addTypes($types);
     return $search;
 }
 /**
  * Build a Search that will count all pages that link to $titles.
  * @param string $titles title in prefixedDBKey form
  * @return Search that counts all pages that link to $titles
  */
 private function buildCount($titles)
 {
     $filter = new Terms('outgoing_link', $titles);
     $filter->setCached(false);
     // We're not going to be redoing this any time soon.
     $type = $this->connection->getPageType(wfWikiId());
     $search = new Search($type->getIndex()->getClient());
     $search->addIndex($type->getIndex());
     $search->addType($type);
     $search->setOption(Search::OPTION_SEARCH_TYPE, Search::OPTION_SEARCH_TYPE_COUNT);
     $matchAll = new MatchAll();
     $search->setQuery(new Filtered($matchAll, $filter));
     $search->getQuery()->addParam('stats', 'link_count');
     return $search;
 }
Exemple #26
0
 /**
  * @group functional
  */
 public function testIgnoreUnavailableOption()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('elastica_7086b4c2ee585bbb6740ece5ed7ece01');
     $query = new MatchAll();
     $search = new Search($client);
     $search->addIndex($index);
     $exception = null;
     try {
         $search->search($query);
     } catch (ResponseException $e) {
         $exception = $e;
     }
     $error = $exception->getResponse()->getFullError();
     $this->assertEquals('index_not_found_exception', $error['type']);
     $results = $search->search($query, array(Search::OPTION_SEARCH_IGNORE_UNAVAILABLE => true));
     $this->assertInstanceOf('\\Elastica\\ResultSet', $results);
 }
Exemple #27
0
 /**
  * @param  \Elastica\Search $search
  * @return array
  */
 protected function _getSearchDataHeader(BaseSearch $search)
 {
     $header = $search->getOptions();
     if ($search->hasIndices()) {
         $header['index'] = $search->getIndices();
     }
     if ($search->hasTypes()) {
         $header['types'] = $search->getTypes();
     }
     return $header;
 }
 /**
  * Create search object
  *
  * @param  string|array|\Elastica\Query $query   Array with all query data inside or a Elastica\Query object
  * @param  int|array                   $options OPTIONAL Limit or associative array of options (option=>value)
  * @return \Elastica\Search
  */
 public function createSearch($query = '', $options = null)
 {
     $search = new Search($this->getIndex()->getClient());
     $search->addIndex($this->getIndex());
     $search->addType($this);
     $search->setOptionsAndQuery($options, $query);
     return $search;
 }
 public function testShouldReturnTheRightNumberOfResult()
 {
     $f = new Nested();
     $this->assertEquals(array('nested' => array()), $f->toArray());
     $q = new Terms();
     $q->setTerms('hobby', array('guitar'));
     $f->setPath('hobbies');
     $f->setFilter($q);
     $c = $this->_getClient();
     $s = new Search($c);
     $i = $c->getIndex('elastica_test_filter_nested_abstract_filter');
     $s->addIndex($i);
     $r = $s->search($f);
     $this->assertEquals(1, $r->getTotalHits());
     $f = new Nested();
     $this->assertEquals(array('nested' => array()), $f->toArray());
     $q = new Terms();
     $q->setTerms('hobby', array('opensource'));
     $f->setPath('hobbies');
     $f->setFilter($q);
     $c = $this->_getClient();
     $s = new Search($c);
     $i = $c->getIndex('elastica_test_filter_nested_abstract_filter');
     $s->addIndex($i);
     $r = $s->search($f);
     $this->assertEquals(2, $r->getTotalHits());
 }
 public function searchAction()
 {
     //WIP, todo: export to repository and use it in Search Playlists
     $elasticaClient = new Client();
     $playListIndex = $elasticaClient->getIndex('playlist');
     $trackType = $playListIndex->getType('track');
     $search = new Search($elasticaClient);
     $search->addIndex($playListIndex)->addType($trackType);
     $query = new Query();
     $query->setSize(5)->setSort(['name' => 'asc'])->setFields(['name', 'ids', 'id', 'composer'])->setExplain(true)->setVersion(true)->setHighlight(['fields' => 'composer']);
     $query->setQuery(new MatchAll());
     //        $query->addAggregation(new \Elastica\Aggregation\Range('name'));
     //        $term = new \Elastica\Suggest\Term('name', 'field');
     //        $term->setText('aaaaa');
     //        $query->setSuggest(new \Elastica\Suggest($term));
     //        $query->setFacets([new \Elastica\Facet\Range('name')]);
     $search->setQuery($query);
     $resultSet = $search->search();
     $numberOfEntries = $search->count();
     $results = $resultSet->getResults();
     $totalResults = $resultSet->getTotalHits();
     return $this->render('PlayWithElasticSearchBundle:Elastica:search.html.twig', ['query' => $query, 'numberOfEntries' => $numberOfEntries, 'resultSet' => $resultSet, 'results' => $results, 'totalResults' => $totalResults]);
 }