setFrom() public method

Sets the start from which the search results should be returned.
public setFrom ( integer $from )
$from integer
 /**
  * @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);
 }
 /**
  * @param int $offset
  * @param int $size
  *
  * @return SearcherInterface
  */
 public function setPagination($offset, $size)
 {
     if (is_int($offset)) {
         $this->query->setFrom($offset);
     }
     if (is_int($size)) {
         $this->query->setSize($size);
     }
     return $this;
 }
Example #3
0
 /**
  * @httpMethod GET
  * @path /
  */
 public function doIndex()
 {
     $url = new Url($this->base->getSelf());
     $count = $url->getParam('count') > 0 ? $url->getParam('count') : 8;
     $count = $count > 16 ? 16 : $count;
     $search = $this->get->search('string');
     if (!empty($search)) {
         $search = strlen($search) > 64 ? substr($search, 0, 64) : $search;
         $queryString = new QueryString();
         //$queryString->setDefaultOperator('AND');
         $queryString->setQuery($search);
         $query = new Query();
         $query->setQuery($queryString);
         $query->setFrom($url->getParam('startIndex'));
         $query->setLimit($count);
         $query->setHighlight(array('pre_tags' => array('<mark>'), 'post_tags' => array('</mark>'), 'fields' => array('title' => new \stdClass(), 'content' => new \stdClass())));
         // get elasticsearch client
         $client = new Client(array('host' => $this->registry['search.host'], 'port' => $this->registry['search.port']));
         $index = $client->getIndex('amun');
         $searchResult = $index->search($query);
         $result = new ResultSet($searchResult->getTotalHits(), $url->getParam('startIndex'), $count);
         foreach ($searchResult as $row) {
             $data = $row->getData();
             $data['url'] = $this->config['psx_url'] . '/' . $this->config['psx_dispatch'] . $data['path'];
             $data['date'] = new DateTime('@' . $data['date']);
             // if we have an highlite overwrite the title or content
             $highlights = $row->getHighlights();
             if (isset($highlights['title'])) {
                 $data['title'] = implode(' ... ', $highlights['title']);
             }
             if (isset($highlights['content'])) {
                 $data['content'] = implode(' ... ', $highlights['content']);
             }
             $result->addData($data);
         }
         $this->template->assign('resultSearch', $result);
         $paging = new Paging($url, $result);
         $this->template->assign('pagingSearch', $paging, 0);
         return $result;
     }
 }
 /**
  * @param string $term text to search
  * @return \Status
  */
 public function searchText($term)
 {
     $term = trim($term);
     // No searching for nothing!  That takes forever!
     if (!$term) {
         return null;
     }
     $query = new Query();
     $offset = min($this->offset, static::MAX_OFFSET);
     if ($offset) {
         $query->setFrom($offset);
     }
     if ($this->limit) {
         $query->setSize($this->limit);
     }
     $filter = new Bool();
     // filters
     if ($this->namespaces) {
         $filter->addMust(new Terms('namespace', $this->namespaces));
     }
     if ($this->pageIds) {
         $filter->addMust(new Terms('pageid', $this->pageIds));
     }
     if ($this->moderationStates) {
         $filter->addMust(new Terms('revisions.moderation_state', $this->moderationStates));
     }
     // only apply filters if there are any
     if ($filter->toArray()) {
         $query->setFilter($filter);
     }
     $sortArgs = $this->getSortArgs();
     if (isset($sortArgs[$this->sort]) && $sortArgs[$this->sort]) {
         $query->setSort($sortArgs[$this->sort]);
     }
     // @todo: interwiki stuff? (see \CirrusSearch)
     $searcher = new Searcher($query, false, $this->type);
     return $searcher->searchText($term);
 }
Example #5
0
 /**
  * Search contents.
  *
  * @return array $elasticsearches Combine of all results, total and aggregations
  *
  * @since 1.5.0
  */
 public function searchContents()
 {
     //Return array
     $return = array('query' => array('search' => '', 'type' => '', 'paged' => 0, 'perpage' => 0), 'total' => 0, 'types' => array(), 'results' => array());
     //Check page
     if (!is_search()) {
         return $return;
     }
     //Get query vars
     $request = isset($_REQUEST) ? $_REQUEST : array();
     $results = array();
     $types = array();
     //Check request
     if (empty($request)) {
         return $return;
     }
     //Get Elasticsearch datas
     $index = $this->getIndex();
     //Check index
     if (null === $index || empty($index)) {
         return $return;
     }
     //Get search datas
     $search = isset($request['s']) ? str_replace('\\"', '"', $request['s']) : '';
     //Return everything
     if (empty($search)) {
         return $return;
     }
     //Get search datas
     $type = isset($request['type']) ? $request['type'] : '';
     $paged = isset($request['paged']) && !empty($request['paged']) ? $request['paged'] - 1 : 0;
     $perpage = isset($request['perpage']) ? $request['perpage'] : TeaThemeOptions::getOption('posts_per_page', 10);
     //Build query string
     $es_querystring = new QueryString();
     //'And' or 'Or' default: 'Or'
     $es_querystring->setDefaultOperator('OR');
     $es_querystring->setQuery($search);
     //Create the actual search object with some data.
     $es_query = new Query();
     $es_query->setQuery($es_querystring);
     //Define options
     $es_query->setFrom($paged);
     //Start
     $es_query->setLimit($perpage);
     //How many
     //Search!
     $es_resultset = $index->search($es_query);
     //Retrieve data
     $es_results = $es_resultset->getResults();
     //Check results
     if (null == $es_results || empty($es_results)) {
         $return['query']['search'] = str_replace(' ', '+', $search);
         return $return;
     }
     //Iterate to retrieve all IDs
     foreach ($es_results as $res) {
         $typ = $res->getType();
         //Save type
         $types[$typ] = $typ;
         //Save datas
         $results[$typ][] = array('id' => $res->getId(), 'score' => $res->getScore(), 'source' => $res->getSource());
     }
     //Get total
     $total = $es_resultset->getTotalHits();
     //Return everything
     $return = array('query' => array('search' => str_replace(' ', '+', $search), 'type' => $type, 'paged' => $paged, 'perpage' => $perpage), 'total' => $total, 'types' => $types, 'results' => $results);
     return $return;
 }
Example #6
0
 /**
  * @return ElasticaQuery
  */
 private function prepareQuery($params, $start = null, $limit = null)
 {
     $query = null;
     $filter = null;
     $sort = ['_score' => 'desc'];
     // We'd like to search in both title and description for keywords
     if (!empty($params['keywords'])) {
         $query = new QueryString($params['keywords']);
         $query->setDefaultOperator('AND')->setFields(['title', 'description']);
     }
     // Add location filter is location is selected from autosuggest
     if (!empty($params['location_id'])) {
         $location = Location::find($params['location_id']);
         $filter = new GeoDistance('location', ['lat' => $location->lat, 'lon' => $location->lon], $params['radius'] . 'mi');
         // Sort by nearest hit
         $sort = ['_geo_distance' => ['jobs.location' => [(double) $location->lon, (double) $location->lat], 'order' => 'asc', 'unit' => 'mi']];
     }
     // If neither keyword nor location supplied, then return all
     if (empty($params['keywords']) && empty($params['location_id'])) {
         $query = new MatchAll();
     }
     // We need a filtered query
     $elasticaQuery = new ElasticaQuery(new Filtered($query, $filter));
     $elasticaQuery->addSort($sort);
     // Offset and limits
     if (!is_null($start) && !is_null($limit)) {
         $elasticaQuery->setFrom($start)->setSize($limit);
     }
     // Set up the highlight
     $elasticaQuery->setHighlight(['order' => 'score', 'fields' => ['title' => ['fragment_size' => 100], 'description' => ['fragment_size' => 200]]]);
     return $elasticaQuery;
 }
Example #7
0
 /**
  * method to search log details
  *
  * @param string $operator    the operator for the query
  * @param string $words       the words
  * @param string $names_types the types to search
  *
  * @return \Elastica\ResultSet
  */
 function searchQueryLogDetails($operator, $words, $names_types = null)
 {
     $words = CmbString::normalizeUtf8(stripcslashes($words));
     // Define a Query. We want a string query.
     $elasticaQueryString = new QueryString();
     //'And' or 'Or' default : 'Or'
     $elasticaQueryString->setDefaultOperator($operator);
     $elasticaQueryString->setQuery($words);
     // Create the actual search object with some data.
     $elasticaQuery = new Query();
     $elasticaQuery->setQuery($elasticaQueryString);
     //Search on the index.
     $index = CAppUI::conf("search index_name") . "_log";
     $this->_index = $this->loadIndex($index);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($this->_index);
     if ($names_types) {
         $search->addTypes($names_types);
     }
     $elasticaQuery->setFrom(0);
     // Where to start
     $elasticaQuery->setLimit(1000);
     return $search->search($elasticaQuery);
 }
Example #8
0
 /**
  * simple search with an operator and words
  *
  * @param string  $operator    'And' or 'Or' default : 'Or'
  * @param string  $words       data
  * @param integer $start       the begining of the paging
  * @param integer $limit       the interval of the paging
  * @param array   $names_types the restrictive type(s) where the search take place.
  * @param bool    $aggregation parameter the search to be aggregated or not.
  *
  * @return \Elastica\ResultSet
  */
 function searchQueryString($operator, $words, $start = 0, $limit = 30, $names_types = null, $aggregation = false)
 {
     $words = CSearch::normalizeEncoding($words);
     // Define a Query. We want a string query.
     $queryString = new Elastica\Query\QueryString($words);
     $queryString->setDefaultOperator("and");
     // Create the actual search object with some data.
     $query = new Elastica\Query($queryString);
     //create aggregation
     if ($aggregation) {
         // on aggrège d'abord par class d'object référents
         // on effectue un sous aggrégation par id des objets référents.
         $agg_by_date = new CSearchAggregation("Terms", "date_log", "date", 10);
         $sub_agg_by_user = new CSearchAggregation("Terms", "user_id", "user_id", 10);
         $sub_agg_by_contexte = new CSearchAggregation("Terms", "contexte", "_type", 10);
         $sub_agg_by_user->_aggregation->addAggregation($sub_agg_by_contexte->_aggregation);
         $agg_by_date->_aggregation->addAggregation($sub_agg_by_user->_aggregation);
         $query->addAggregation($agg_by_date->_aggregation);
     } else {
         //  Pagination
         $query->setFrom($start);
         // Where to start
         $query->setLimit($limit);
     }
     //Highlight
     $query->setHighlight(array("fields" => array("body" => array("pre_tags" => array(" <em> <strong> "), "post_tags" => array(" </strong> </em>"), "fragment_size" => 80, "number_of_fragments" => 10))));
     //Search on the index.
     $index = CAppUI::conf("search index_name") . "_log";
     $index = $this->loadIndex($index);
     $search = new \Elastica\Search($this->_client);
     $search->addIndex($index);
     if ($names_types) {
         $search->addTypes($names_types);
     }
     return $search->search($query);
 }
 /**
  * Launch the search
  *
  */
 private function search()
 {
     $this->elasticaQuery->setLimit($this->limitPerPage);
     $this->elasticaQuery->setFrom($this->getOffset());
     $this->resultSet = $this->elastica->search($this->elasticaQuery, $this->index);
 }
 /**
  * Powers full-text-like searches including prefix search.
  *
  * @param string $type
  * @param string $for
  * @return Status(mixed) results from the query transformed by the resultsType
  */
 private function search($type, $for)
 {
     if ($this->nonTextQueries) {
         $bool = new \Elastica\Query\Bool();
         if ($this->query !== null) {
             $bool->addMust($this->query);
         }
         foreach ($this->nonTextQueries as $nonTextQuery) {
             $bool->addMust($nonTextQuery);
         }
         $this->query = $bool;
     }
     if ($this->resultsType === null) {
         $this->resultsType = new FullTextResultsType(FullTextResultsType::HIGHLIGHT_ALL);
     }
     // Default null queries now so the rest of the method can assume it is not null.
     if ($this->query === null) {
         $this->query = new \Elastica\Query\MatchAll();
     }
     $query = new Elastica\Query();
     $query->setParam('_source', $this->resultsType->getSourceFiltering());
     $query->setParam('fields', $this->resultsType->getFields());
     $extraIndexes = array();
     $indexType = $this->pickIndexTypeFromNamespaces();
     if ($this->namespaces) {
         $extraIndexes = $this->getAndFilterExtraIndexes();
         if ($this->needNsFilter($extraIndexes, $indexType)) {
             $this->filters[] = new \Elastica\Filter\Terms('namespace', $this->namespaces);
         }
     }
     // Wrap $this->query in a filtered query if there are any filters
     $unifiedFilter = Filters::unify($this->filters, $this->notFilters);
     if ($unifiedFilter !== null) {
         $this->query = new \Elastica\Query\Filtered($this->query, $unifiedFilter);
     }
     // Call installBoosts right after we're done munging the query to include filters
     // so any rescores installBoosts adds to the query are done against filtered results.
     $this->installBoosts();
     $query->setQuery($this->query);
     $highlight = $this->resultsType->getHighlightingConfiguration($this->highlightSource);
     if ($highlight) {
         // Fuzzy queries work _terribly_ with the plain highlighter so just drop any field that is forcing
         // the plain highlighter all together.  Do this here because this works so badly that no
         // ResultsType should be able to use the plain highlighter for these queries.
         if ($this->fuzzyQuery) {
             $highlight['fields'] = array_filter($highlight['fields'], function ($field) {
                 return $field['type'] !== 'plain';
             });
         }
         if (!empty($this->nonTextHighlightQueries)) {
             // We have some phrase_prefix queries, so let's include them in the
             // generated highlight_query.
             $bool = new \Elastica\Query\Bool();
             if ($this->highlightQuery) {
                 $bool->addShould($this->highlightQuery);
             }
             foreach ($this->nonTextHighlightQueries as $nonTextHighlightQuery) {
                 $bool->addShould($nonTextHighlightQuery);
             }
             $this->highlightQuery = $bool;
         }
         if ($this->highlightQuery) {
             $highlight['highlight_query'] = $this->highlightQuery->toArray();
         }
         $query->setHighlight($highlight);
     }
     if ($this->suggest) {
         $query->setParam('suggest', $this->suggest);
         $query->addParam('stats', 'suggest');
     }
     if ($this->offset) {
         $query->setFrom($this->offset);
     }
     if ($this->limit) {
         $query->setSize($this->limit);
     }
     if ($this->sort != 'relevance') {
         // Clear rescores if we aren't using relevance as the search sort because they aren't used.
         $this->rescore = array();
     }
     if ($this->rescore) {
         // rescore_query has to be in array form before we send it to Elasticsearch but it is way easier to work
         // with if we leave it in query for until now
         $modifiedRescore = array();
         foreach ($this->rescore as $rescore) {
             $rescore['query']['rescore_query'] = $rescore['query']['rescore_query']->toArray();
             $modifiedRescore[] = $rescore;
         }
         $query->setParam('rescore', $modifiedRescore);
     }
     $query->addParam('stats', $type);
     switch ($this->sort) {
         case 'relevance':
             break;
             // The default
         // The default
         case 'title_asc':
             $query->setSort(array('title.keyword' => 'asc'));
             break;
         case 'title_desc':
             $query->setSort(array('title.keyword' => 'desc'));
             break;
         case 'incoming_links_asc':
             $query->setSort(array('incoming_links' => array('order' => 'asc', 'missing' => '_first')));
             break;
         case 'incoming_links_desc':
             $query->setSort(array('incoming_links' => array('order' => 'desc', 'missing' => '_last')));
             break;
         default:
             LoggerFactory::getInstance('CirrusSearch')->warning("Invalid sort type: {sort}", array('sort' => $this->sort));
     }
     $queryOptions = array();
     if ($this->config->get('CirrusSearchMoreAccurateScoringMode')) {
         $queryOptions['search_type'] = 'dfs_query_then_fetch';
     }
     switch ($type) {
         case 'regex':
             $poolCounterType = 'CirrusSearch-Regex';
             $queryOptions['timeout'] = $this->config->getElement('CirrusSearchSearchShardTimeout', 'regex');
             break;
         case 'prefix':
             $poolCounterType = 'CirrusSearch-Prefix';
             $queryOptions['timeout'] = $this->config->getElement('CirrusSearchSearchShardTimeout', 'default');
             break;
         default:
             $poolCounterType = 'CirrusSearch-Search';
             $queryOptions['timeout'] = $this->config->getElement('CirrusSearchSearchShardTimeout', 'default');
     }
     $this->connection->setTimeout($queryOptions['timeout']);
     // Setup the search
     $pageType = $this->connection->getPageType($this->indexBaseName, $indexType);
     $search = $pageType->createSearch($query, $queryOptions);
     foreach ($extraIndexes as $i) {
         $search->addIndex($i);
     }
     $description = "{queryType} search for '{query}'";
     $logContext = array('queryType' => $type, 'query' => $for);
     if ($this->returnQuery) {
         return Status::newGood(array('description' => $this->formatDescription($description, $logContext), 'path' => $search->getPath(), 'params' => $search->getOptions(), 'query' => $query->toArray(), 'options' => $queryOptions));
     }
     if ($this->returnExplain && $this->returnResult) {
         $query->setExplain(true);
     }
     // Perform the search
     $searcher = $this;
     $user = $this->user;
     $result = Util::doPoolCounterWork($poolCounterType, $this->user, function () use($searcher, $search, $description, $logContext) {
         try {
             $searcher->start($description, $logContext);
             return $searcher->success($search->search());
         } catch (\Elastica\Exception\ExceptionInterface $e) {
             return $searcher->failure($e);
         }
     }, function ($error, $key, $userName) use($type, $description, $user, $logContext) {
         $forUserName = $userName ? "for {userName} " : '';
         LoggerFactory::getInstance('CirrusSearch')->warning("Pool error {$forUserName}on key {key} during {$description}:  {error}", $logContext + array('userName' => $userName, 'key' => 'key', 'error' => $error));
         if ($error === 'pool-queuefull') {
             if (strpos($key, 'nowait:CirrusSearch:_per_user') === 0) {
                 $loggedIn = $user->isLoggedIn() ? 'logged-in' : 'anonymous';
                 return Status::newFatal("cirrussearch-too-busy-for-you-{$loggedIn}-error");
             }
             if ($type === 'regex') {
                 return Status::newFatal('cirrussearch-regex-too-busy-error');
             }
             return Status::newFatal('cirrussearch-too-busy-error');
         }
         return Status::newFatal('cirrussearch-backend-error');
     });
     if ($result->isOK()) {
         $responseData = $result->getValue()->getResponse()->getData();
         if ($this->returnResult) {
             return Status::newGood(array('description' => $this->formatDescription($description, $logContext), 'path' => $search->getPath(), 'result' => $responseData));
         }
         $result->setResult(true, $this->resultsType->transformElasticsearchResult($this->suggestPrefixes, $this->suggestSuffixes, $result->getValue(), $this->searchContext->isSearchContainedSyntax()));
         if (isset($responseData['timed_out']) && $responseData['timed_out']) {
             LoggerFactory::getInstance('CirrusSearch')->warning("{$description} timed out and only returned partial results!", $logContext);
             if ($result->getValue()->numRows() === 0) {
                 return Status::newFatal('cirrussearch-backend-error');
             } else {
                 $result->warning('cirrussearch-timed-out');
             }
         }
     }
     return $result;
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function generateQueryBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 {
     $query = new Query();
     if (empty($criteria)) {
         $query->setQuery(new MatchAll());
     } else {
         $query->setQuery(new Filtered(null, $this->generateFilterBy($criteria)));
     }
     if ($orderBy) {
         $query->setSort($orderBy);
     }
     if ($limit) {
         $query->setSize($limit);
     }
     if ($offset) {
         $query->setFrom($offset);
     }
     return $query;
 }
Example #12
0
 /**
  * Query to search auto
  *
  * @param CSearchThesaurusEntry $favori The favori
  * @param CSejour               $sejour The sejour
  *
  * @return Query
  */
 function querySearchAuto($favori, $sejour)
 {
     $query_bool = new Elastica\Query\Bool();
     // query des séjours
     $query_sejour = new Elastica\Query\QueryString();
     $query_sejour->setQuery($this->constructWordsWithSejour($sejour->_id));
     $query_sejour->setDefaultOperator("and");
     $query_bool->addMust($query_sejour);
     // query du favoris
     $query_words = new Elastica\Query\QueryString();
     $query_words->setQuery($this->normalizeEncoding($favori->entry));
     $query_words->setFields(array("body", "title"));
     $query_words->setDefaultOperator("and");
     $query_bool->addMust($query_words);
     $query = new Query($query_bool);
     //  Pagination
     $query->setFrom(0);
     // Where to start
     $query->setLimit(30);
     //Highlight
     $query->setHighlight(array("pre_tags" => array(" <em> <strong> "), "post_tags" => array(" </strong> </em>"), "fields" => array("body" => array("fragment_size" => 50, "number_of_fragments" => 3, "highlight_query" => array("bool" => array("must" => array("match" => array("body" => array("query" => $this->normalizeEncoding($favori->entry)))), "minimum_should_match" => 1))))));
     return $query;
 }
Example #13
0
File: Logs.php Project: bd808/SAL
 /**
  * Search for logs
  *
  * @param array $params Search parameters:
  *   - project: Project to find logs for
  *   - query: Elasticsearch simple query string
  *   - items: Number of results to return per page
  *   - page: Page of results to return (0-index)
  * @return ResultSet
  */
 public function search(array $params = [])
 {
     $params = array_merge(['project' => 'production', 'query' => null, 'items' => 20, 'page' => 0, 'date' => null], $params);
     $filters = new BoolQuery();
     if ($params['query'] !== null) {
         $filters->addMust(new SimpleQueryString($params['query'], ['message', 'nick']));
     }
     if ($params['date'] !== null) {
         $filters->addMust(new Range('@timestamp', ['lte' => "{$params['date']}||/d"]));
     }
     $query = new Query($filters);
     $query->setPostFilter(new Term(['project' => $params['project']]));
     $query->setFrom($params['page'] * $params['items'])->setSize($params['items'])->setFields(self::$DEFAULT_FIELDS)->setSort(['@timestamp' => ['order' => 'desc']]);
     return $this->doSearch($query);
 }
 /**
  * Search on Elasticsearch
  * Example Usage:
  *
  * With Elastica Query Builder
  *      $qb = new QueryBuilder();
  *      $query = $qb->query()->match_all();
  *      $mainQuery = new \Elastica\Query($query);
  *      $this->doSearch($mainQuery, 1, 10);
  *
  * @param  ElasticaQuery    $query
  * @param  int              $page
  * @param  int              $itemPerPage
  * @param  string           $type           Already defined strings at self.
  * @param  int              $version        Search version for index and type
  * @return Resultset
  */
 protected function doSearch(ElasticaQuery $query, $page, $itemsPerPage, $type = self::VIEW_LIST, $version = 1)
 {
     $query->setFrom(($page - 1) * $itemsPerPage)->setSize($itemsPerPage);
     if ($type === self::VIEW_LIST) {
         if ($this->getListViewFields()) {
             $query->setSource($this->getListViewFields());
         }
     } elseif ($type === self::VIEW_SHORT) {
         if ($this->getShortViewFields()) {
             $query->setSource($this->getShortViewFields());
         }
     } elseif ($type === self::VIEW_DETAIL) {
         if ($this->getDetailViewFields()) {
             $query->setSource($this->getDetailViewFields());
         }
     }
     return $this->getClient()->getIndex($this->getIndexName($version))->getType($this->getTypeName($version))->search($query);
 }
Example #15
0
 /**
  * @param \Elastica\Query $baseQuery
  *
  * @return void
  */
 protected function setOffset($baseQuery)
 {
     if ($this->offset !== null) {
         $baseQuery->setFrom($this->offset);
     }
 }
Example #16
0
 public function testDeltaSync()
 {
     // @TODO: ensure records flagged as deleted are removed from the index
     // delete and create index if it already exists
     // make index dirty with prepopulated stuff
     $this->createEsIndex();
     $initial_data = $this->populateEsIndex();
     $data_source = Phake::mock('Renegare\\ES\\Tests\\Mock\\TestModel');
     $index = $this->es_test_index_name;
     $doc_type = $this->es_test_doc_type;
     $last_sync = new \DateTime();
     $updated_data = $this->getFakeDeltaSyncData($initial_data);
     $expected_data = array_merge($initial_data, $updated_data);
     // extract data flagged to be deleted ... if any!
     $deleted_data = array();
     foreach ($expected_data as $id => $data) {
         if (isset($data['__deleted'])) {
             $deleted_data[$id] = $data;
             unset($expected_data[$id]);
         }
     }
     $expected_data_count = count($expected_data);
     Phake::when($data_source)->getData($index, $doc_type, $last_sync)->thenReturn($updated_data);
     Phake::when($data_source)->getLastSync($index, $doc_type)->thenReturn($last_sync);
     $this->manager->setDataSource($data_source);
     $this->manager->setLastSyncHandler($data_source);
     $this->manager->sync(ElasticSearchManager::SYNC_DELTA, 'test', 'test_type');
     // make sure our data source is called correctly
     Phake::verify($data_source, Phake::times(1))->getData($index, $doc_type, $last_sync);
     Phake::verify($data_source, Phake::times(1))->getLastSync($index, $doc_type);
     // needs more thought as the date time does not always match and fails
     Phake::verify($data_source, Phake::times(1))->setLastSync($index, $doc_type);
     // make sure that all and only expected_data is in es
     // we are using Elastica Library here and not the ESManager ... #ethical
     $es_index = $this->es_client->getIndex($index);
     $es_query_string = new QueryString();
     $es_query_string->setQuery('*');
     $es_query = new Query();
     $es_query->setQuery($es_query_string);
     // ensure we get everything back!
     $es_query->setFrom(0);
     $es_query->setLimit($expected_data_count);
     //Search on the index.
     $es_result = $es_index->search($es_query);
     $this->assertEquals($expected_data_count, $es_result->getTotalHits());
     foreach ($es_result->getResults() as $result) {
         $data = $result->getData();
         $id = $result->getId();
         $this->assertArrayHasKey($id, $expected_data);
         $this->assertEquals(serialize($expected_data[$id]), serialize($data));
         // ensure a duplicate record does not exist
         unset($expected_data[$id]);
     }
     $this->assertEquals(0, count($expected_data));
     // delete test index
     $this->deleteEsIndex();
 }
Example #17
0
 public function search()
 {
     //$finder = $this->container->get('fos_elastica.finder.search.articles');
     $bool = new Query\Bool();
     $multiMatch = new Query\MultiMatch();
     $multiMatch->setFields(['subjects', 'title', 'keywords', 'subtitle', 'citations.raw', 'journal.title', 'journal.subtitle']);
     $multiMatch->setType('phrase_prefix');
     $multiMatch->setQuery($this->getParam('term'));
     $bool->addMust($multiMatch);
     if ($this->filter) {
         foreach ($this->filter as $key => $filter) {
             $filterObj = new Query\Match();
             $this->applyFilter($filterObj, $key, $filter);
             $bool->addMust($filterObj);
         }
     }
     $missing = new Filter\Missing("issue");
     $not = new Filter\BoolNot($missing);
     $notQ = new Query\Filtered();
     $notQ->setFilter($not);
     $bool->addMust($notQ);
     $query = new Query();
     $query->setQuery($bool);
     $query->setFrom($this->getPage() * $this->getLimit());
     $query->setSize($this->getLimit());
     $aggregation = new Terms('journals');
     $aggregation->setField('journal.id');
     $aggregation->setOrder('_count', 'desc');
     $qb = $this->em->createQueryBuilder();
     $qb->select('count(r.id)')->from('OjsJournalBundle:Journal', 'r')->where($qb->expr()->eq('r.status', 3));
     $aggregation->setSize($qb->getQuery()->getSingleScalarResult());
     $query->addAggregation($aggregation);
     $aggregation = new Terms('authors');
     $aggregation->setField('articleAuthors.author.id');
     $aggregation->setOrder('_count', 'desc');
     $qb = $this->em->createQueryBuilder();
     $qb->select('count(r.id)')->from('OjsJournalBundle:Author', 'r');
     $aggregation->setSize($qb->getQuery()->getSingleScalarResult());
     $query->addAggregation($aggregation);
     $elasticaAdapter = new ElasticaAdapter($this->index, $query);
     $pagerFanta = new Pagerfanta($elasticaAdapter);
     $pagerFanta->setMaxPerPage($this->getLimit());
     $pagerFanta->setCurrentPage($this->getPage());
     /** @var ResultSet $search */
     $search = $pagerFanta->getCurrentPageResults();
     $result = $search->getResults();
     //$search->getResults();
     $this->pager = $pagerFanta;
     $transformer = new ElasticaToModelTransformer($this->registry, 'OjsJournalBundle:Article');
     $transformer->setPropertyAccessor($this->propertyAccessor);
     $this->result = $transformer->transform($result);
     $this->setCount($pagerFanta->getNbResults());
     $this->addAggregation('journal', $this->transform($search->getAggregation('journals')['buckets'], 'OjsJournalBundle:Journal'));
     $this->addAggregation('author', $this->transform($search->getAggregation('authors')['buckets'], 'OjsJournalBundle:Author'));
     return $this;
 }
Example #18
0
 /**
  * Get quips ordered by votes
  *
  * @param array $params Search parameters:
  *   - items: Number of results to return per page
  *   - page: Page of results to return (0-index)
  * @return ResultSet
  */
 public function top(array $params = array())
 {
     $params = array_merge(array('items' => 20, 'page' => 0), $params);
     $query = new Query();
     $query->setFrom($params['page'] * $params['items'])->setSize($params['items'])->setFields($this->defaultFields())->setSort(array(array('score' => array('order' => 'desc', 'missing' => '_last')), array('up_votes' => array('order' => 'desc')), array('down_votes' => array('order' => 'asc')), array('@timestamp' => array('order' => 'desc'))));
     return $this->doSearch($query);
 }
Example #19
0
 /**
  * @inheritDoc
  */
 public function paginate()
 {
     $this->query->setSize($this->limit);
     $this->query->setFrom(($this->page - 1) * $this->limit);
     return $this->query;
 }
Example #20
0
 /**
  * Perform a search based on the specified filter
  *
  * @param $typeTerms array
  * @param null $user
  * @param null $sortColumn
  * @param string $sortDirection
  * @param int $limit
  * @return array
  */
 public function filteredSearch($typeTerms, $user = null, $sortColumn = null, $sortDirection = 'desc', $limit = 50)
 {
     $query = new Query();
     $query->setFrom(0);
     $query->setSize($limit);
     $boolQuery = new BoolQuery();
     // Handle 'must' filters
     if (isset($typeTerms['must'])) {
         foreach ($typeTerms['must'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addMust($termQueryType);
         }
     }
     // Handle 'should' filters
     if (isset($typeTerms['should'])) {
         foreach ($typeTerms['should'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addShould($termQueryType);
         }
     }
     $query->setQuery($boolQuery);
     if ($user) {
         $userId = $user->id;
         $termQueryUser = new TermQuery(['user_id' => $userId]);
         $boolQuery->addMust($termQueryUser);
     }
     if ($sortColumn) {
         $query->addSort([$sortColumn => ['order' => $sortDirection]]);
     }
     $resultSet = $this->getTypedIndex()->search($query);
     return $this->getModels($resultSet);
 }
 /**
  * @param $searchClosed
  * @return \Elastica\ResultSet
  * @throws Exception
  */
 public function doSearch($searchClosed)
 {
     $this->connection = new ElasticSearchConnection();
     $this->connection->init();
     $this->whereClause = new Query\QueryString();
     $this->whereClause->setQuery($this->searchTerms);
     $this->utility = new Util();
     if (isset($_GET['page'])) {
         $this->currentPage = intval($_GET['page']);
     }
     $this->fieldMapping = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, 'WMDB.Forger.SearchTermMapping');
     $elasticaQuery = new Query();
     if ($searchClosed === 'true') {
         $elasticaQuery->setQuery($this->whereClause);
     } else {
         $boolSearch = new Query\Bool();
         $boolSearch->addMust($this->whereClause);
         $boolSearch->addMustNot(['term' => ['status.name' => 'Closed']]);
         $boolSearch->addMustNot(['term' => ['status.name' => 'Rejected']]);
         $boolSearch->addMustNot(['term' => ['status.name' => 'Resolved']]);
         $elasticaQuery->setQuery($boolSearch);
     }
     $elasticaQuery->setSize($this->perPage);
     $elasticaQuery->setFrom($this->currentPage * $this->perPage - $this->perPage);
     $usedFilters = $this->addFilters();
     if ($usedFilters !== false) {
         $elasticaQuery->setPostFilter($usedFilters);
     }
     $this->addAggregations($elasticaQuery);
     $elasticaResultSet = $this->connection->getIndex()->search($elasticaQuery);
     $results = $elasticaResultSet->getResults();
     $maxScore = $elasticaResultSet->getMaxScore();
     $aggs = $elasticaResultSet->getAggregations();
     $this->totalHits = $elasticaResultSet->getTotalHits();
     $out = array('pagesToLinkTo' => $this->getPages(), 'currentPage' => $this->currentPage, 'prev' => $this->currentPage - 1, 'next' => $this->currentPage < ceil($this->totalHits / $this->perPage) ? $this->currentPage + 1 : 0, 'totalResults' => $this->totalHits, 'startingAtItem' => $this->currentPage * $this->perPage - ($this->perPage - 1), 'endingAtItem' => $this->currentPage * $this->perPage, 'results' => $results, 'maxScore' => $maxScore, 'aggs' => $aggs);
     if (intval($this->totalHits) <= intval($out['endingAtItem'])) {
         $out['endingAtItem'] = intval($this->totalHits);
     }
     return $out;
 }