本文整理汇总了PHP中Elastica\Query::setFrom方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::setFrom方法的具体用法?PHP Query::setFrom怎么用?PHP Query::setFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Query
的用法示例。
在下文中一共展示了Query::setFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addPaginationToQuery
/**
* @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);
}
示例2: setPagination
/**
* @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;
}
示例3: doIndex
/**
* @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;
}
}
示例4: searchText
/**
* @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);
}
示例5: searchContents
/**
* 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;
}
示例6: prepareQuery
/**
* @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;
}
示例7: searchQueryLogDetails
/**
* 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);
}
示例8: searchQueryString
/**
* 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);
}
示例9: search
/**
* 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);
}
示例10: search
/**
* 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':
//.........这里部分代码省略.........
示例11: generateQueryBy
/**
* {@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;
}
示例12: querySearchAuto
/**
* 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;
}
示例13: search
/**
* 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);
}
示例14: doSearch
/**
* 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);
}
示例15: setOffset
/**
* @param \Elastica\Query $baseQuery
*
* @return void
*/
protected function setOffset($baseQuery)
{
if ($this->offset !== null) {
$baseQuery->setFrom($this->offset);
}
}