/
class.SolrSearchResults.inc.php
126 lines (105 loc) · 2.44 KB
/
class.SolrSearchResults.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* Wrapper class for Solarium results.
*
* PHP version 5
*
* @license http://www.gnu.org/licenses/gpl.html GPL 3
* @version 1.0
* @link http://www.statedecoded.com/
* @since 0.9
*/
class SolrSearchResults implements SearchResultsInterface
{
public $query;
public $results;
public $spelling;
public function __construct($query, $results)
{
$this->query = $query;
$this->results = $results;
$this->fixed_results = $this->fix_results($results);
$this->spelling = $results->getSpellcheck();
}
public function get_results()
{
return $this->fixed_results;
}
public function get_count()
{
return $this->results->getNumFound();
}
public function get_fixed_spelling()
{
/*
* If we know something is misspelled.
*/
if ($this->spelling->getCorrectlySpelled() === FALSE)
{
/*
* Step through each term that appears to be misspelled, and create a modified query string.
*/
$clean_spelling = $this->query['q'];
foreach($this->spelling as $suggestion)
{
$str_start = $suggestion->getStartOffset();
$str_end = $suggestion->getEndOffset();
$str_length = $str_end - $str_start;
$clean_spelling = str_splice($clean_spelling, $str_start, $str_length, $suggestion->getWord());
}
/*
* If our result is the same as the original, we couldn't find a better suggestion.
* In that case, return false. This prevents false positives.
*/
if($clean_spelling === $this->query['q'])
{
return FALSE;
}
else
{
return $clean_spelling;
}
}
else
{
return FALSE;
}
}
/*
* Reassemble the query string for the fixed spelling.
*/
public function get_fixed_query()
{
// Get our existing query.
$args = $this->query;
// Reset back to page 1.
$args['page'] = 1;
// Fix the query term.
$args['q'] = $this->get_fixed_spelling();
return http_build_query($args);
}
protected function fix_results($results)
{
$highlighted = $results->getHighlighting();
$fixed_results = array();
foreach($results as $result)
{
$temp_result = new stdClass();
foreach($result as $key => $value)
{
$temp_result->$key = $value;
}
// The default type of object is a law.
if(!isset($temp_result->object_type)) {
$temp_result->object_type = 'law';
}
if($highlighted)
{
// Add highlighting
$temp_result->highlight = $highlighted->getResult($result->id);
}
$fixed_results[] = $temp_result;
}
return $fixed_results;
}
}