Passed
Push — master ( d7fd6a...9124ff )
by Robbie
11:58
created

CwpSearchEngine::search()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 6.7272
cc 7
eloc 16
nc 10
nop 6
1
<?php
2
3
/**
4
 * Provides interface for generating search results for a SolrIndex
5
 */
6
class CwpSearchEngine extends Object {
7
	
8
	/**
9
	 * Default search options
10
	 *
11
	 * @var array
12
	 * @config
13
	 */
14
	private static $search_options = array(
15
		'hl' => 'true'
16
	);
17
	
18
	/**
19
	 * Additional search options to send to search when spellcheck
20
	 * is included
21
	 *
22
	 * @var array
23
	 * @config 
24
	 */
25
	private static $spellcheck_options = array(
26
		'spellcheck' => 'true',
27
		'spellcheck.collate' => 'true',
28
		// spellcheck.dictionary can also be configured to use '_spellcheck'
29
		// dictionary when indexing fields under the _spellcheckText column
30
		'spellcheck.dictionary' => 'default'
31
	);
32
	
33
	/**
34
	 * Build a SearchQuery for a new search
35
	 * 
36
	 * @param string $keywords
37
	 * @param array $classes
38
	 * @return SearchQuery
39
	 */
40
	protected function getSearchQuery($keywords, $classes) {
41
		$query = new SearchQuery();
0 ignored issues
show
Bug introduced by
The type SearchQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
		$query->classes = $classes;
43
		$query->search($keywords);
44
		$query->exclude('SiteTree_ShowInSearch', 0);
45
        $query->exclude('File_ShowInSearch', 0);
46
47
		// Artificially lower the amount of results to prevent too high resource usage.
48
		// on subsequent canView check loop.
49
		$query->limit(100);
50
		return $query;
51
	}
52
	
53
	/**
54
	 * Get solr search options for this query
55
	 * 
56
	 * @param bool $spellcheck True if we should include spellcheck support
57
	 * @return array
58
	 */
59
	protected function getSearchOptions($spellcheck) {
60
		$options = $this->config()->search_options;
61
		if($spellcheck) {
62
			$options = array_merge($options, $this->config()->spellcheck_options);
0 ignored issues
show
Bug introduced by
It seems like $this->config()->spellcheck_options can also be of type integer and string and boolean and double; however, parameter $array2 of array_merge() does only seem to accept null|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
			$options = array_merge($options, /** @scrutinizer ignore-type */ $this->config()->spellcheck_options);
Loading history...
Bug introduced by
It seems like $options can also be of type integer and string and boolean and double; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
			$options = array_merge(/** @scrutinizer ignore-type */ $options, $this->config()->spellcheck_options);
Loading history...
63
		}
64
		return $options;
65
	}
66
	
67
	/**
68
	 * Get results for a search term
69
	 * 
70
	 * @param string $keywords
71
	 * @param array $classes
72
	 * @param SolrIndex $searchIndex
73
	 * @param int $limit Max number of results for this page
74
	 * @param int $start Skip this number of records
75
	 * @param bool $spellcheck True to enable spellcheck
76
	 * @return CwpSearchResult
77
	 */
78
	protected function getResult($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $spellcheck = false) {
79
		// Prepare options
80
		$query = $this->getSearchQuery($keywords, $classes);
81
		$options = $this->getSearchOptions($spellcheck);
82
		
83
		// Get results
84
		$solrResult = $searchIndex->search(
85
			$query,
86
			$start,
87
			$limit,
88
			$options
89
		);
90
		
91
		return CwpSearchResult::create($keywords, $solrResult);
92
	}
93
	
94
	/**
95
	 * Get a CwpSearchResult for a given criterea
96
	 * 
97
	 * @param string $keywords
98
	 * @param array $classes
99
	 * @param SolrIndex $searchIndex
100
	 * @param int $limit Max number of results for this page
101
	 * @param int $start Skip this number of records
102
	 * @param bool $followSuggestions True to enable suggested searches to be returned immediately
103
	 * @return CwpSearchResult|null
104
	 */
105
	public function search($keywords, $classes, $searchIndex, $limit = -1, $start = 0, $followSuggestions = false) {
106
		if(empty($keywords)) {
107
			return null;
108
		}
109
110
		try {
111
			// Begin search
112
			$result = $this->getResult($keywords, $classes, $searchIndex, $limit, $start, true);
113
			
114
			// Return results if we don't need to refine this any further
115
			if(!$followSuggestions || $result->hasResults() || !$result->getSuggestion()) {
116
				return $result;
117
			}
118
			
119
			// Perform new search with the suggested terms
120
			$suggested = $result->getSuggestion();
121
			$newResult = $this->getResult($suggested, $classes, $searchIndex, $limit, $start, false);
122
			$newResult->setOriginal($keywords);
123
124
			// Compare new results to the original query
125
			if($newResult->hasResults()) {
126
				return $newResult;
127
			} else {
128
				return $result;
129
			}
130
131
		} catch(Exception $e) {
132
			SS_Log::log($e, SS_Log::WARN);
133
		}
134
		
135
		return null;
136
	}
137
}
138