/** * Performs the search against the snippets in the system * * @param {string} $keywords Keywords to search for * @param {int} $languageID Language to filter to * @param {int} $folderID Folder to filter to * @return {DataList} Data list pointing to the snippets in the results */ public function doSnippetSearch($keywords, $languageID = false, $folderID = false) { $searchIndex = singleton('CodeBankSolrIndex'); $searchQuery = new SearchQuery(); $searchQuery->classes = array(array('class' => 'Snippet', 'includeSubclasses' => true)); //Add language filtering if ($languageID !== false && $languageID > 0) { $searchQuery->filter('Snippet_LanguageID', $languageID); } //Add language filtering if ($folderID !== false && $folderID > 0) { $searchQuery->filter('Snippet_FolderID', $folderID); } //Configure search $searchQuery->search($keywords, null, array('Snippet_Title' => 2, 'Snippet_Description' => 1)); return $searchIndex->search($searchQuery, null, null)->Matches->getList(); }
/** * This is an intermediary to bridge the search form input * and the SearchQuery class. It allows us to have other * drivers that may not use the FullTextSearch module. * * @param string $keywords * @param array $filters [optional] * @param array $facetSpec [optional] * @param int $start [optional] * @param int $limit [optional] * @param string $sort [optional] * @return ArrayData */ public function searchFromVars($keywords, array $filters = array(), array $facetSpec = array(), $start = -1, $limit = -1, $sort = 'score desc') { $query = new SearchQuery(); $params = array('sort' => $sort); // swap out title search if ($params['sort'] == 'SiteTree_Title') { $params['sort'] = '_titleSort'; } // search by keywords $query->search(empty($keywords) ? '*:*' : $keywords); // search by filter foreach ($filters as $k => $v) { if (isset($this->fieldMap[$k])) { if (is_string($v) && preg_match('/^RANGE\\~(.+)\\~(.+)$/', $v, $m)) { // Is it a range value? $range = new SearchQuery_Range($m[1], $m[2]); $query->filter($this->fieldMap[$k], $range); } else { // Or a normal scalar value $query->filter($this->fieldMap[$k], $v); } } } // add facets $facetSpec = FacetHelper::inst()->expandFacetSpec($facetSpec); $params += $this->buildFacetParams($facetSpec); // TODO: add spellcheck return $this->search($query, $start, $limit, $params, $facetSpec); }
/** * Builds a search query from a give search term. * @return SearchQuery */ protected function getSearchQuery($keywords) { $categoryIDs = array(); $categoryFilterID = $this->request->requestVar(self::$search_category_key); $categories = $this->Categories(); if ($categories->count() == 0) { $categories = FAQ::getRootCategory()->Children(); } $filterCategory = $categories->filter('ID', $categoryFilterID)->first(); $categoryIDs = array(); if ($filterCategory && $filterCategory->exists()) { $categoryIDs = $this->getSelectedIDs(array($filterCategory)); } else { $categoryIDs = $this->Categories()->column('ID'); } $query = new SearchQuery(); $query->classes = self::$classes_to_search; if (count($categoryIDs) > 0) { $query->filter('FAQ_Category_ID', array_filter($categoryIDs, 'intval')); } $query->search($keywords); // Artificially lower the amount of results to prevent too high resource usage. // on subsequent canView check loop. $query->limit(100); return $query; }