forked from jaeksoft/opensearchserver-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_results.class.php
More file actions
209 lines (185 loc) · 5.8 KB
/
oss_results.class.php
File metadata and controls
209 lines (185 loc) · 5.8 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
* This file is part of OpenSearchServer PHP Client.
*
* Copyright (C) 2008-2013 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* OpenSearchServer PHP Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer PHP Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenSearchServer PHP Client. If not, see <http://www.gnu.org/licenses/>.
*/
if (!extension_loaded('SimpleXML')) {
trigger_error("OssApi won't work whitout SimpleXML extension", E_USER_ERROR); die();
}
/**
* @file
* Class to access OpenSearchServer API
* @author philcube <egosse@open-search-server.com>
* @package OpenSearchServer
*/
class OssResults {
/* @var SimpleXMLElement */
protected $result;
protected $resultFound;
protected $resultTime;
protected $resultRows;
protected $resultStart;
protected $resultCollapsedCount;
/**
* @param $result The data
* @param $model The list of fields
* @return OssApi
*/
public function __construct(SimpleXMLElement $result, $model = NULL) {
$this->result = $result;
$this->resultFound = (int)$this->result->result['numFound'];
$this->resultTime = (float)$this->result->result['time'] / 1000;
$this->resultRows = (int)$this->result->result['rows'];
$this->resultStart = (int)$this->result->result['start'];
$this->resultCollapsedCount = (int)$this->result->result['collapsedDocCount'];
if (!function_exists('OssApi_Dummy_Function')) {
function OssApi_Dummy_Function() {
}
}
}
public function getResultCollapsedCount() {
return $this->resultCollapsedCount;
}
public function getResult() {
return $this->result;
}
public function getResultFound() {
return $this->resultFound;
}
public function getResultTime() {
return $this->resultTime;
}
public function getResultRows() {
return $this->resultRows;
}
public function getResultStart() {
return $this->resultStart;
}
/**
* GETTER
*/
public function getField($position, $fieldName, $modeSnippet = FALSE, $highlightedOnly = FALSE, $joinPosition = NULL, $getMultipleValues = false) {
$field = NULL;
$joinPrefix = '';
if ($joinPosition != null) {
$joinPrefix = '/join[@paramPosition="jq' . (int)$joinPosition . '"]';
}
$doc = $this->result->xpath('result/doc[@pos="' . $position . '"]' . $joinPrefix);
if (isset($doc[0]) && is_array($doc)) {
$value = NULL;
if ($modeSnippet) {
if ($highlightedOnly) {
$value = $doc[0]->xpath('snippet[@name="' . $fieldName . '" and @highlighted="yes"]');
} else {
$value = $doc[0]->xpath('snippet[@name="' . $fieldName . '"]');
}
}
if (!isset($value) || count($value) == 0) {
$value = $doc[0]->xpath('field[@name="' . $fieldName . '"]');
}
if ($getMultipleValues && count($value)>1) {
$tempArray = array();
foreach($value as $key=>$elt) {
$tempArray[] = $elt;
}
$field = $tempArray;
}
elseif (isset($value[0])) {
$field = $value[0];
}
}
return $field;
}
public function getScore($position) {
$doc = $this->result->xpath('result/doc[@pos="' . $position . '"]');
if (isset($doc[0]) && is_array($doc)) {
return $doc[0]['score'];
}
return null;
}
/**
*
*/
public function getFields($position, $modeSnippet = FALSE) {
$doc = $this->result->xpath('result/doc[@pos="' . $position . '"]');
$fields = $doc[0]->xpath('field');
foreach ($fields as $field) {
$name = (string) $field[0]['name'];
$current[(string)$name] = (string) $field;
}
if ($modeSnippet) {
$snippets = $doc->xpath('snippet');
foreach ($snippets as $field) {
$name = (string) $field[0]['name'];
$current[(string)$name] = (string) $field;
}
}
return $current;
}
/**
*
* @param unknown_type $fieldName
* @return Ambigous <multitype:, NULL>
*/
public function getFacet($fieldName) {
$currentFacet = isset($fieldName)? $this->result->xpath('faceting/field[@name="' . $fieldName . '"]/facet'):NULL;
if (!isset($currentFacet) || ( isset($currentFacet) && $currentFacet === FALSE)) {
$currentFacet = array();
}
return $currentFacet;
}
/**
*
* @return unknown_type
*/
public function getFacets() {
$facets = array();
$allFacets = $this->result->xpath('faceting/field');
foreach ($allFacets as $each) {
$facets[] = $each[0]['name'];
}
return $facets;
}
/**
*
* @return Return the spellsuggest array.
*/
public function getSpellSuggestions($fieldName) {
$currentSpellCheck = isset($fieldName)? $this->result->xpath('spellcheck/field[@name="' . $fieldName . '"]/word/suggest'):NULL;
if (!isset($currentSpellCheck) || ( isset($currentSpellCheck) && $currentSpellCheck === FALSE)) {
$currentSpellCheck = array();
}
return $currentSpellCheck;
}
/**
*
* @return Return the spellsuggest terms.
*/
public function getSpellSuggest($fieldName) {
$spellCheckWord = isset($fieldName)? $this->result->xpath('spellcheck/field[@name="' . $fieldName . '"]/word'):NULL;
$queryExact = '';
if (isset($spellCheckWord) && $spellCheckWord != null) {
foreach ($spellCheckWord as $each) {
$queryExact .= $each[0]->suggest.' ';
}
}
return $queryExact;
}
}
?>