Bolt\Legacy\Storage::searchContent PHP Method

searchContent() public method

Unless the query is invalid it will always return a 'result array'. It may complain in the log but it won't abort.
public searchContent ( string $q, array $contenttypes = null, array $filters = null, integer $limit = 9999, integer $offset ) : mixed
$q string Search string
$contenttypes array Contenttype names to search for: - string: Specific contenttype - null: Every searchable contenttype
$filters array Additional filters for contenttypes - key is contenttype - value is filter
$limit integer limit the number of results
$offset integer skip this number of results
return mixed false if query is invalid, an array with results if query was executed
    public function searchContent($q, array $contenttypes = null, array $filters = null, $limit = 9999, $offset = 0)
    {
        $query = $this->decodeSearchQuery($q);
        if (!$query['valid']) {
            return false;
        }
        $appCt = $this->app['config']->get('contenttypes');
        // By default we only search through searchable contenttypes
        if (is_null($contenttypes)) {
            $contenttypes = array_keys($appCt);
            $contenttypes = array_filter($contenttypes, function ($ct) use($appCt) {
                if (isset($appCt[$ct]['searchable']) && $appCt[$ct]['searchable'] === false || isset($appCt[$ct]['viewless']) && $appCt[$ct]['viewless'] === true) {
                    return false;
                }
                return true;
            });
            $contenttypes = array_map(function ($ct) use($appCt) {
                return $appCt[$ct]['slug'];
            }, $contenttypes);
        }
        // Build our search results array
        $results = [];
        // First, attempt to search for the literal string, eg. "Lorum Ipsum"
        foreach ($contenttypes as $contenttype) {
            $ctconfig = $this->getContentType($contenttype);
            $fields = $ctconfig['fields'];
            $filter = null;
            if (is_array($filters) && isset($filters[$contenttype])) {
                $filter = $filters[$contenttype];
            }
            $subResults = $this->searchSingleContentType($query, $contenttype, $fields, $filter, true);
            $results = array_merge($results, $subResults);
        }
        // If that didn't produce results, search for "Lorum" or "Ipsum"
        if (empty($results)) {
            foreach ($contenttypes as $contenttype) {
                $ctconfig = $this->getContentType($contenttype);
                $fields = $ctconfig['fields'];
                $filter = null;
                if (is_array($filters) && isset($filters[$contenttype])) {
                    $filter = $filters[$contenttype];
                }
                $subResults = $this->searchSingleContentType($query, $contenttype, $fields, $filter, false);
                $results = array_merge($results, $subResults);
            }
        }
        // Sort the results
        usort($results, [$this, 'compareSearchWeights']);
        $noOfResults = count($results);
        $pageResults = [];
        if ($offset < $noOfResults) {
            $pageResults = array_slice($results, $offset, $limit);
        }
        return ['query' => $query, 'no_of_results' => $noOfResults, 'results' => $pageResults];
    }

Usage Example

Exemplo n.º 1
0
 public function testSearchContent()
 {
     $app = $this->getApp();
     $app['request'] = Request::create('/');
     $storage = new Storage($app);
     $result = $storage->searchContent('lorem');
     $this->assertGreaterThan(0, count($result));
     $this->assertTrue($result['query']['valid']);
     // Test invalid query fails
     $result = $storage->searchContent('x');
     $this->assertFalse($result);
     // Test filters
     $result = $storage->searchContent('lorem', ['showcases'], ['showcases' => ['title' => 'nonexistent']]);
     $this->assertTrue($result['query']['valid']);
     $this->assertEquals(0, $result['no_of_results']);
 }