Flarum\Core\Search\Discussion\DiscussionSearcher::search PHP Method

    public function search(SearchCriteria $criteria, $limit = null, $offset = 0, array $load = [])
    {
        $actor = $criteria->actor;
        $query = $this->discussions->query()->whereVisibleTo($actor);
        // Construct an object which represents this search for discussions.
        // Apply gambits to it, sort, and paging criteria. Also give extensions
        // an opportunity to modify it.
        $search = new DiscussionSearch($query->getQuery(), $actor);
        $this->gambits->apply($search, $criteria->query);
        $this->applySort($search, $criteria->sort);
        $this->applyOffset($search, $offset);
        $this->applyLimit($search, $limit + 1);
        // TODO: inject dispatcher
        event(new ConfigureDiscussionSearch($search, $criteria));
        // Execute the search query and retrieve the results. We get one more
        // results than the user asked for, so that we can say if there are more
        // results. If there are, we will get rid of that extra result.
        $discussions = $query->get();
        $areMoreResults = $limit > 0 && $discussions->count() > $limit;
        if ($areMoreResults) {
            $discussions->pop();
        }
        // The relevant posts relationship isn't a typical Eloquent
        // relationship; rather, we need to extract that information from our
        // search object. We will delegate that task and prevent Eloquent
        // from trying to load it.
        if (in_array('relevantPosts', $load)) {
            $this->loadRelevantPosts($discussions, $search);
            $load = array_diff($load, ['relevantPosts', 'relevantPosts.discussion', 'relevantPosts.user']);
        }
        Discussion::setStateUser($actor);
        $discussions->load($load);
        return new SearchResults($discussions, $areMoreResults);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 protected function data(ServerRequestInterface $request, Document $document)
 {
     $actor = $request->getAttribute('actor');
     $query = array_get($this->extractFilter($request), 'q');
     $sort = $this->extractSort($request);
     $criteria = new SearchCriteria($actor, $query, $sort);
     $limit = $this->extractLimit($request);
     $offset = $this->extractOffset($request);
     $load = array_merge($this->extractInclude($request), ['state']);
     $results = $this->searcher->search($criteria, $limit, $offset, $load);
     $document->addPaginationLinks($this->url->toRoute('discussions.index'), $request->getQueryParams(), $offset, $limit, $results->areMoreResults() ? null : 0);
     return $results->getResults();
 }