HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
hce_sphinx_api.inc.php
Go to the documentation of this file.
1 <?php
14 require_once 'hce_node_api.inc.php';
15 
19 defined('SPHINX_JSON_USE_BASE64') or define('SPHINX_JSON_USE_BASE64', 0);
23 defined('HCE_SPHINX_CMD_TYPE_ADMIN') or define('HCE_SPHINX_CMD_TYPE_ADMIN', 2);
27 defined('HCE_SPHINX_TIMEOUT') or define('HCE_SPHINX_TIMEOUT', 1500);
31 defined('HCE_SPHINX_ERROR_OK') or define('HCE_SPHINX_ERROR_OK', 0);
32 defined('HCE_SPHINX_ERROR_CREATE_MESSAGE') or define('HCE_SPHINX_ERROR_CREATE_MESSAGE', -100);
33 defined('HCE_SPHINX_ERROR_PARSE_RESPONSE') or define('HCE_SPHINX_ERROR_PARSE_RESPONSE', -101);
34 defined('HCE_SPHINX_ERROR_CREATE_CONNECTION') or define('HCE_SPHINX_ERROR_CREATE_CONNECTION', -102);
35 defined('HCE_SPHINX_ERROR_EMPTY_COMMAND') or define('HCE_SPHINX_ERROR_EMPTY_COMMAND', -103);
36 defined('HCE_SPHINX_ERROR_MESSAGE_NOT_FOUND') or define('HCE_SPHINX_ERROR_MESSAGE_NOT_FOUND', -104);
37 defined('HCE_SPHINX_ERROR_WRONG_PARAMETERS') or define('HCE_SPHINX_ERROR_WRONG_PARAMETERS', -105);
38 
42 defined('HCE_SPHINX_CMD_INDEX_CREATE') or define('HCE_SPHINX_CMD_INDEX_CREATE', 'INDEX_CREATE');
43 defined('HCE_SPHINX_CMD_INDEX_CHECK') or define('HCE_SPHINX_CMD_INDEX_CHECK', 'INDEX_CHECK');
44 defined('HCE_SPHINX_CMD_INDEX_STORE_DATA_FILE') or define('HCE_SPHINX_CMD_INDEX_STORE_DATA_FILE', 'INDEX_STORE_DATA_FILE');
45 defined('HCE_SPHINX_CMD_INDEX_STORE_SCHEMA_FILE') or define('HCE_SPHINX_CMD_INDEX_STORE_SCHEMA_FILE', 'INDEX_STORE_SCHEMA_FILE');
46 defined('HCE_SPHINX_CMD_INDEX_REBUILD') or define('HCE_SPHINX_CMD_INDEX_REBUILD', 'INDEX_REBUILD');
47 defined('HCE_SPHINX_CMD_INDEX_SET_DATA_DIR') or define('HCE_SPHINX_CMD_INDEX_SET_DATA_DIR', 'INDEX_SET_DATA_DIR');
48 defined('HCE_SPHINX_CMD_INDEX_START') or define('HCE_SPHINX_CMD_INDEX_START', 'INDEX_START');
49 defined('HCE_SPHINX_CMD_INDEX_STOP') or define('HCE_SPHINX_CMD_INDEX_STOP', 'INDEX_STOP');
50 defined('HCE_SPHINX_CMD_INDEX_MERGE') or define('HCE_SPHINX_CMD_INDEX_MERGE', 'INDEX_MERGE');
51 defined('HCE_SPHINX_CMD_INDEX_DELETE_DATA_FILE') or define('HCE_SPHINX_CMD_INDEX_DELETE_DATA_FILE', 'INDEX_DELETE_DATA_FILE');
52 defined('HCE_SPHINX_CMD_INDEX_DELETE_SCHEMA_FILE') or define('HCE_SPHINX_CMD_INDEX_DELETE_SCHEMA_FILE', 'INDEX_DELETE_SCHEMA_FILE');
53 defined('HCE_SPHINX_CMD_INDEX_APPEND_DATA_FILE') or define('HCE_SPHINX_CMD_INDEX_APPEND_DATA_FILE', 'INDEX_APPEND_DATA_FILE');
54 defined('HCE_SPHINX_CMD_INDEX_DELETE_DOC') or define('HCE_SPHINX_CMD_INDEX_DELETE_DOC', 'INDEX_DELETE_DOC');
55 defined('HCE_SPHINX_CMD_INDEX_DELETE_DOC_NUMBER') or define('HCE_SPHINX_CMD_INDEX_DELETE_DOC_NUMBER', 'INDEX_DELETE_DOC_NUMBER');
56 defined('HCE_SPHINX_CMD_INDEX_PACK_DOC_DATA') or define('HCE_SPHINX_CMD_INDEX_PACK_DOC_DATA', 'INDEX_PACK_DOC_DATA');
57 defined('HCE_SPHINX_CMD_INDEX_REMOVE') or define('HCE_SPHINX_CMD_INDEX_REMOVE', 'INDEX_REMOVE');
58 defined('HCE_SPHINX_CMD_INDEX_COPY') or define('HCE_SPHINX_CMD_INDEX_COPY', 'INDEX_COPY');
59 defined('HCE_SPHINX_CMD_INDEX_RENAME') or define('HCE_SPHINX_CMD_INDEX_RENAME', 'INDEX_RENAME');
60 defined('HCE_SPHINX_CMD_INDEX_SET_CONFIG_VAR') or define('HCE_SPHINX_CMD_INDEX_SET_CONFIG_VAR', 'INDEX_SET_CONFIG_VAR');
61 defined('HCE_SPHINX_CMD_INDEX_MAX_DOC_ID') or define('HCE_SPHINX_CMD_INDEX_MAX_DOC_ID', 'INDEX_MAX_DOC_ID');
62 defined('HCE_SPHINX_CMD_INDEX_STATUS') or define('HCE_SPHINX_CMD_INDEX_STATUS', 'INDEX_STATUS');
63 defined('HCE_SPHINX_CMD_INDEX_STATUS_SEARCHD') or define('HCE_SPHINX_CMD_INDEX_STATUS_SEARCHD', 'INDEX_STATUS_SEARCHD');
64 defined('HCE_SPHINX_CMD_INDEX_DATA_LIST') or define('HCE_SPHINX_CMD_INDEX_DATA_LIST', 'INDEX_DATA_LIST');
65 defined('HCE_SPHINX_CMD_INDEX_CONNECT') or define('HCE_SPHINX_CMD_INDEX_CONNECT', 'INDEX_CONNECT');
66 defined('HCE_SPHINX_CMD_INDEX_DISCONNECT') or define('HCE_SPHINX_CMD_INDEX_DISCONNECT', 'INDEX_DISCONNECT');
67 
71 defined('HCE_SPHINX_SEARCH_FIELD_Q') or define('HCE_SPHINX_SEARCH_FIELD_Q', 'q');
72 defined('HCE_SPHINX_SEARCH_FIELD_FILTERS') or define('HCE_SPHINX_SEARCH_FIELD_FILTERS', 'filters');
73 defined('HCE_SPHINX_SEARCH_FIELD_PARAMETERS') or define('HCE_SPHINX_SEARCH_FIELD_PARAMETERS', 'parameters');
74 defined('HCE_SPHINX_SEARCH_FIELD_QUERY_ID') or define('HCE_SPHINX_SEARCH_FIELD_QUERY_ID', 'queryId');
75 defined('HCE_SPHINX_SEARCH_FIELD_JSON_TYPE') or define('HCE_SPHINX_SEARCH_FIELD_JSON_TYPE', 'JsonType');
76 defined('HCE_SPHINX_SEARCH_FIELD_TYPE') or define('HCE_SPHINX_SEARCH_FIELD_TYPE', 'type');
77 defined('HCE_SPHINX_SEARCH_FIELD_DATA') or define('HCE_SPHINX_SEARCH_FIELD_DATA', 'data');
78 defined('HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS') or define('HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS', 'max_results');
79 defined('HCE_SPHINX_SEARCH_FIELD_SORT_MODE') or define('HCE_SPHINX_SEARCH_FIELD_SORT_MODE', 'sort_mode');
80 defined('HCE_SPHINX_SEARCH_FIELD_SORT_BY') or define('HCE_SPHINX_SEARCH_FIELD_SORT_BY', 'sort_by');
81 defined('HCE_SPHINX_SEARCH_FIELD_TYPE_MASK') or define('HCE_SPHINX_SEARCH_FIELD_TYPE_MASK', 'type_mask');
82 defined('HCE_SPHINX_SEARCH_FIELD_ID') or define('HCE_SPHINX_SEARCH_FIELD_ID', 'id');
83 defined('HCE_SPHINX_SEARCH_FIELD_ORDER') or define('HCE_SPHINX_SEARCH_FIELD_ORDER', 'order');
84 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_ALG') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_ALG', 'algorithm');
85 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_ALG_0') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_ALG_0', '0');
86 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_FIELDS') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_FIELDS', 'fields');
87 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_BY') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_BY', 'order_by');
88 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_ASC') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_ASC', '1');
89 defined('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_DESC') or define('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_DESC', '2');
90 defined('HCE_SPHINX_SEARCH_FIELD_OFFSET') or define('HCE_SPHINX_SEARCH_FIELD_OFFSET', 'offset');
91 defined('HCE_SPHINX_SEARCH_FIELD_LIMIT') or define('HCE_SPHINX_SEARCH_FIELD_LIMIT', 'limit');
92 defined('HCE_SPHINX_SEARCH_FIELD_CUTOFF') or define('HCE_SPHINX_SEARCH_FIELD_CUTOFF', 'cutoff');
93 defined('HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS') or define('HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS', 'return_jason_ext_fields');
94 defined('HCE_SPHINX_SEARCH_FIELD_FILTER_TYPE') or define('HCE_SPHINX_SEARCH_FIELD_FILTER_TYPE', 'type');
95 defined('HCE_SPHINX_SEARCH_FIELD_FILTER_ATTRIB') or define('HCE_SPHINX_SEARCH_FIELD_FILTER_ATTRIB', 'attribute');
96 defined('HCE_SPHINX_SEARCH_FIELD_FILTER_VALUES') or define('HCE_SPHINX_SEARCH_FIELD_FILTER_VALUES', 'values');
97 defined('HCE_SPHINX_SEARCH_FIELD_FILTER_EXCLUDE') or define('HCE_SPHINX_SEARCH_FIELD_FILTER_EXCLUDE', 'exclude');
98 for($i=0; $i<6; $i++){
99  defined('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_'.$i) or define('HCE_SPHINX_SEARCH_FIELD_ORDER_BY_'.$i, $i.'');
100 }
101 defined('HCE_SPHINX_SEARCH_FIELD_TTL') or define('HCE_SPHINX_SEARCH_FIELD_TTL', 'ttl');
102 defined('HCE_SPHINX_SEARCH_FIELD_TTL_DEFAULT') or define('HCE_SPHINX_SEARCH_FIELD_TTL_DEFAULT', '1000');
103 defined('HCE_SPHINX_SEARCH_FIELD_TIMEOUT') or define('HCE_SPHINX_SEARCH_FIELD_TIMEOUT', 'timeout');
104 defined('HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT') or define('HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT', '10000');
105 defined('HCE_SPHINX_SEARCH_FIELD_JSON_TYPE_DEFAULT') or define('HCE_SPHINX_SEARCH_FIELD_JSON_TYPE_DEFAULT', '15');
106 defined('HCE_SPHINX_SEARCH_FIELD_ERROR') or define('HCE_SPHINX_SEARCH_FIELD_ERROR', 'error');
107 
111 defined('HCE_SPHINX_FO_SEARCH') or define('HCE_SPHINX_FO_SEARCH', '0');
112 defined('HCE_SPHINX_FO_INDEX') or define('HCE_SPHINX_FO_INDEX', '1');
113 defined('HCE_SPHINX_FO_ADMIN') or define('HCE_SPHINX_FO_ADMIN', '2');
114 defined('HCE_SPHINX_SEARCH_RET_TYPE_MI_INFO') or define('HCE_SPHINX_SEARCH_RET_TYPE_MI_INFO', 1);
115 defined('HCE_SPHINX_SEARCH_RET_TYPE_RI_INFO') or define('HCE_SPHINX_SEARCH_RET_TYPE_RI_INFO', 2);
116 defined('HCE_SPHINX_SEARCH_RET_TYPE_AT_INFO') or define('HCE_SPHINX_SEARCH_RET_TYPE_AT_INFO', 4);
117 defined('HCE_SPHINX_SEARCH_RET_TYPE_WI_INFO') or define('HCE_SPHINX_SEARCH_RET_TYPE_WI_INFO', 8);
118 defined('HCE_SPHINX_SEARCH_MAX_RESULTS_FROM_NODE_DEFAULT') or define('HCE_SPHINX_SEARCH_MAX_RESULTS_FROM_NODE_DEFAULT', '10');
119 defined('HCE_SPHINX_SEARCH_SORT_BY_DEFAULT') or define('HCE_SPHINX_SEARCH_SORT_BY_DEFAULT', '');
120 //Sort modes
121 defined('HCE_SPHINX_SEARCH_SORT_MODE_NONE') or define('HCE_SPHINX_SEARCH_SORT_MODE_NONE', '0');
122 defined('HCE_SPHINX_SEARCH_SORT_MODE_ASC') or define('HCE_SPHINX_SEARCH_SORT_MODE_ASC', '1');
123 defined('HCE_SPHINX_SEARCH_SORT_MODE_DESC') or define('HCE_SPHINX_SEARCH_SORT_MODE_DESC', '2');
124 
128 defined('HCE_SPHINX_SEARCH_RESULTS_MI') or define('HCE_SPHINX_SEARCH_RESULTS_MI', 'MI');
129 defined('HCE_SPHINX_SEARCH_RESULTS_AT') or define('HCE_SPHINX_SEARCH_RESULTS_AT', 'At');
130 defined('HCE_SPHINX_SEARCH_RESULTS_WI') or define('HCE_SPHINX_SEARCH_RESULTS_WI', 'WI');
131 defined('HCE_SPHINX_SEARCH_RESULTS_RI') or define('HCE_SPHINX_SEARCH_RESULTS_RI', 'RI');
132 defined('HCE_SPHINX_SEARCH_RESULTS_ID') or define('HCE_SPHINX_SEARCH_RESULTS_ID', 'Id');
133 defined('HCE_SPHINX_SEARCH_RESULTS_WEIGHT') or define('HCE_SPHINX_SEARCH_RESULTS_WEIGHT', 'W');
134 defined('HCE_SPHINX_SEARCH_RESULTS_SWEIGHT') or define('HCE_SPHINX_SEARCH_RESULTS_SWEIGHT', 'sw');
135 
136 /*
137  * @desc send Sphinx admin request message using zmq-based ACN API
138  * @param $request_array - array with 'id' and 'request' items; id - is an unique request Id, if not set will be generated; 'request' - is a message string in json format with command content
139  * $connection_array - ACN connection array is null - new admin type will be created
140  *
141  * @return ACN Sphinx API result array; array(HCE_SPHINX_SEARCH_FIELD_ERROR=0, 'response', 'hce_connection_array'); If HCE_SPHINX_SEARCH_FIELD_ERROR item is zero - command executed successfully, 'response' item contains response json
142  * and error code in other cases: HCE_SPHINX_ERROR_EMPTY_COMMAND - The command json is empty or not set, HCE_SPHINX_ERROR_CREATE_CONNECTION - error connection create;
143  * HCE_SPHINX_ERROR_MESSAGE_NOT_FOUND - Response message not found by Id
144  */
145  function hce_sphinx_admin_request($request_array, $connection_array=null, $response_timeout=HCE_MSG_RESPONSE_TIMEOUT){
146  //Init returned ACN Sphinx API result array
147  $ret=array(HCE_SPHINX_SEARCH_FIELD_ERROR=>HCE_SPHINX_ERROR_OK, 'response'=>null, 'hce_connection_array'=>null);
148 
149  if(strlen($request_array['request'])>0){
150  if($connection_array===null){
151  //Create and init acn connection array from default
152  $ret['hce_connection_array']=hce_connection_create(array('host'=>HCE_HOST_ADMIN_DEFAULT, 'port'=>HCE_PORT_ADMIN_DEFAULT, 'type'=>HCE_CONNECTION_TYPE_ADMIN,
153  'identity'=>hce_unique_client_id()
154  ));
155  }else{
156  $ret['hce_connection_array']=$connection_array;
157  }
158  if(isset($ret['hce_connection_array'][HCE_SPHINX_SEARCH_FIELD_ERROR]) && $ret['hce_connection_array'][HCE_SPHINX_SEARCH_FIELD_ERROR]==HCE_PROTOCOL_ERROR_OK){
159  //Make request id
160  if(empty($request_array['id'])){
161  $request_array['id']=hce_unique_message_id();
162  }
163  //Send message
164  hce_message_send($ret['hce_connection_array'], array('id'=>$request_array['id'], 'body'=>$request_array['request']));
165 
166  //Receive response message(s)
167  $hce_responses=hce_message_receive($ret['hce_connection_array'], $response_timeout);
168 
169  //Process message(s)
170  if($hce_responses[HCE_SPHINX_SEARCH_FIELD_ERROR]==HCE_PROTOCOL_ERROR_OK){
171  //Response message not found by Id
172  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=HCE_SPHINX_ERROR_MESSAGE_NOT_FOUND;
173  foreach($hce_responses['messages'] as $hce_message){
174  //Find proper response
175  if($hce_message['id']==$request_array['id']){
176  $ret['response']=$hce_message['body'];
177  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=HCE_SPHINX_ERROR_OK;
178  break;
179  }
180  }
181  }else{
182  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=$hce_responses[HCE_SPHINX_SEARCH_FIELD_ERROR];
183  }
184  }else{
185  //Wrong connection or error create connection
186  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=HCE_SPHINX_ERROR_CREATE_CONNECTION;
187  }
188  if($connection_array===null){
189  //Free ACN connection resources
190  hce_connection_delete($ret['hce_connection_array']);
191  }
192  }else{
193  //The command json is empty or not set
194  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=HCE_SPHINX_ERROR_EMPTY_COMMAND;
195  }
196 
197  return $ret;
198  }
199 
200 /*
201  * @desc create Sphinx admin request in json format
202  * @param $command - command name; $parameters_array - array of items to construct json formatted admin request;
203  *
204  * @return ACN Sphinx admin request string in json format if success or null in case of error
205  */
207  //Encode for POCO json implementation compatibility reason
208  return json_encode(array('command'=>$command, 'options'=>json_encode($parameters_array)));
209  }
210 
211 /*
212  * @desc parse Sphinx admin response in json format
213  * @param $response_body - content of response json string
214  *
215  * @return ACN Sphinx admin request response array
216  */
217  function hce_sphinx_parse_response($response_body){
218  return json_decode($response_body, true);
219  }
220 
221 /*
222  * @desc create Sphinx admin request message body in json format according with admin request type HCE_SPHINX_CMD_TYPE_ADMIN
223  * @param $response_body - content of response json string
224  *
225  * @return ACN Sphinx admin request message
226  */
227  function hce_sphinx_admin_message_create($admin_request_body){
228  return hce_admin_message_create(HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_ADMIN_CMD_SPHINX, json_encode(array('type'=>HCE_SPHINX_CMD_TYPE_ADMIN, 'data'=>$admin_request_body)));
229  }
230 
231 /*
232  * @desc execute Sphinx admin request
233  * @param $command - command name corrsponded with command_string; $options_array - array of options corresponded with command_options_string; $timeout - response timeout
234  *
235  * @return array of Sphinx admin response if success or negative value if error; HCE_SPHINX_ERROR_CREATE_MESSAGE - error create message,
236  * HCE_SPHINX_ERROR_PARSE_RESPONSE - response parse error, or hce_sphinx_admin_request() call error
237  */
238  function hce_sphinx_exec($command, $options_array, $connection_array, $timeout){
239  $ret=array();
240 
242  if(empty($message)){
243  //Error create message
244  $ret=HCE_SPHINX_ERROR_CREATE_MESSAGE;
245  }else{
246  $response=hce_sphinx_admin_request(array('request'=>$message), $connection_array, $timeout);
247  if($response[HCE_SPHINX_SEARCH_FIELD_ERROR]==HCE_SPHINX_ERROR_OK){
248  $ret=hce_sphinx_parse_response($response['response']);
249  if($ret===null || !is_array($ret)){
250  //Response parse error
251  $ret=HCE_SPHINX_ERROR_PARSE_RESPONSE;
252  }
253  }else{
254  $ret=$response[HCE_SPHINX_SEARCH_FIELD_ERROR];
255  }
256  }
257 
258  return $ret;
259  }
260 
261 
262 /*
263  * @desc make Sphinx search request json message
264  * @param $parameters_array - parameters array corresponds to the Sphinx FO protocol Functional_object_message_format.docx
265  * array(HCE_SPHINX_SEARCH_FIELD_Q=>'query string',
266  * HCE_SPHINX_SEARCH_FIELD_PARAMETERS=>array(array(HCE_SPHINX_SEARCH_FIELD_QUERY_ID=>123), array(HCE_SPHINX_SEARCH_FIELD_JSON_TYPE=>15))
267  * )
268  *
269  * @return string json encoded message or negative value in case of error: HCE_SPHINX_ERROR_WRONG_PARAMETERS - wrong parameters array structure
270  */
272  $ret=array(HCE_SPHINX_SEARCH_FIELD_TYPE=>HCE_SPHINX_FO_SEARCH, HCE_SPHINX_SEARCH_FIELD_DATA=>null);
273  $ret1=array(HCE_SPHINX_SEARCH_FIELD_TYPE=>HCE_HANDLER_TYPE_SPHINX, HCE_SPHINX_SEARCH_FIELD_DATA=>null, HCE_SPHINX_SEARCH_FIELD_TTL=>HCE_SPHINX_SEARCH_FIELD_TTL_DEFAULT);
274 
275  if(!isset($parameters_array[HCE_SPHINX_SEARCH_FIELD_Q]) ||
276  !isset($parameters_array[HCE_SPHINX_SEARCH_FIELD_FILTERS]) ||
277  !isset($parameters_array[HCE_SPHINX_SEARCH_FIELD_PARAMETERS]) ||
278  !isset($parameters_array[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][0][HCE_SPHINX_SEARCH_FIELD_QUERY_ID]) ||
279  !isset($parameters_array[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][1][HCE_SPHINX_SEARCH_FIELD_JSON_TYPE])
280  ){
281  $ret=HCE_SPHINX_ERROR_WRONG_PARAMETERS;
282  }else{
283  if(SPHINX_JSON_USE_BASE64){
284  $parameters_array[HCE_SPHINX_SEARCH_FIELD_Q]=base64_encode($parameters_array[HCE_SPHINX_SEARCH_FIELD_Q]);
285  $parameters_array[HCE_SPHINX_SEARCH_FIELD_FILTERS]=base64_encode(json_encode($parameters_array[HCE_SPHINX_SEARCH_FIELD_FILTERS]));
286  }else{
287  $parameters_array[HCE_SPHINX_SEARCH_FIELD_Q]=$parameters_array[HCE_SPHINX_SEARCH_FIELD_Q];
288  $parameters_array[HCE_SPHINX_SEARCH_FIELD_FILTERS]=json_encode($parameters_array[HCE_SPHINX_SEARCH_FIELD_FILTERS]);
289  }
290  $ret[HCE_SPHINX_SEARCH_FIELD_DATA]=json_encode($parameters_array);
291  }
292  if(SPHINX_JSON_USE_BASE64){
293  $ret1[HCE_SPHINX_SEARCH_FIELD_DATA]=base64_encode(json_encode($ret));
294  }else{
295  $ret1[HCE_SPHINX_SEARCH_FIELD_DATA]=json_encode($ret);
296  }
297  $ret=$ret1;
298 
299  return json_encode($ret);
300  }
301 
302 /*
303  * @desc make Sphinx search request json message
304  * @param $query_string - search string in Sphinx query string format. It will be provided directly to Sphinx searchd to process
305  * $parameters - array of parameters:
306  * id - unique search query identifier, used to identify the query in the cluster. If not set or NULL - will be generated
307  * type_mask - mask to define the information that can be returned from Sphinx search in result. Defined as return_json_type_mask in
308  * Functional_object_message_format.docx.
309  * default value is HCE_SPHINX_SEARCH_RET_TYPE_ID_INFO|HCE_SPHINX_SEARCH_RET_TYPE_DOC_INFO|HCE_SPHINX_SEARCH_RET_TYPE_STAT_INFO
310  * max_results - max results number to return
311  * sort_mode - sort mode, 0 - no sort, 1 - ASC, 2 - DESC
312  * sort_by - field name to use as sort criterion, "weight" - by default
313  *
314  * @return parameters array ready to be used in hce_sphinx_create_search_json() call or negative value in case of error: HCE_SPHINX_ERROR_WRONG_PARAMETERS - query string not set or empty
315  */
316  function hce_sphinx_search_prepare_parameters($query_string, $parameters=null, $order=null){
317  $ret=array(HCE_SPHINX_SEARCH_FIELD_Q=>null,
318  HCE_SPHINX_SEARCH_FIELD_FILTERS=>null,
319  HCE_SPHINX_SEARCH_FIELD_PARAMETERS=>array(array(HCE_SPHINX_SEARCH_FIELD_QUERY_ID=>null),
320  array(HCE_SPHINX_SEARCH_FIELD_JSON_TYPE=>HCE_SPHINX_SEARCH_FIELD_JSON_TYPE_DEFAULT),
321  array(HCE_SPHINX_SEARCH_FIELD_SORT_BY=>HCE_SPHINX_SEARCH_SORT_BY_DEFAULT),
322  array(HCE_SPHINX_SEARCH_FIELD_ORDER_BY=>HCE_SPHINX_SEARCH_FIELD_ORDER_BY_0),
323  array(HCE_SPHINX_SEARCH_FIELD_OFFSET=>'0'),
324  array(HCE_SPHINX_SEARCH_FIELD_LIMIT=>'0'),
325  array(HCE_SPHINX_SEARCH_FIELD_CUTOFF=>'0'),
326  array(HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS=>array()),
327  array(HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS=>HCE_SPHINX_SEARCH_MAX_RESULTS_FROM_NODE_DEFAULT),
328  array(HCE_SPHINX_SEARCH_FIELD_TIMEOUT=>HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT)
329  ),
330 
331  HCE_SPHINX_SEARCH_FIELD_ORDER=>array(array(HCE_SPHINX_SEARCH_FIELD_ORDER_ALG=>HCE_SPHINX_SEARCH_FIELD_ORDER_ALG_0), array(HCE_SPHINX_SEARCH_FIELD_ORDER_FIELDS=>array()))
332  );
333 
334  //Parameters array is not set
335  if($parameters===null){
336  $parameters=array(HCE_SPHINX_SEARCH_FIELD_ID=>null, HCE_SPHINX_SEARCH_FIELD_TYPE_MASK=>null, HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS=>HCE_SPHINX_SEARCH_MAX_RESULTS_FROM_NODE_DEFAULT,
337  HCE_SPHINX_SEARCH_FIELD_SORT_MODE=>HCE_SPHINX_SEARCH_SORT_MODE_ASC, HCE_SPHINX_SEARCH_FIELD_SORT_BY=>HCE_SPHINX_SEARCH_SORT_BY_DEFAULT);
338  }
339  //Default initialization
340  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_ID])){
341  //Generate unique query ID
342  $parameters[HCE_SPHINX_SEARCH_FIELD_ID]=sprintf('%u', crc32(hce_unique_message_id()));
343  }
344  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_TYPE_MASK])){
345  //Include only document's Id and weight info
346  $parameters[HCE_SPHINX_SEARCH_FIELD_TYPE_MASK]=HCE_SPHINX_SEARCH_RET_TYPE_MI_INFO;
347  }
348  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_SORT_MODE])){
349  //Sort mode ASC
350  $parameters[HCE_SPHINX_SEARCH_FIELD_SORT_MODE]=HCE_SPHINX_SEARCH_SORT_MODE_ASC;
351  }
352  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_SORT_BY])){
353  //Sort by "weight"
354  $parameters[HCE_SPHINX_SEARCH_FIELD_SORT_BY]=HCE_SPHINX_SEARCH_SORT_BY_DEFAULT;
355  }
356  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_OFFSET])){
357  //Offset 0
358  $parameters[HCE_SPHINX_SEARCH_FIELD_OFFSET]='0';
359  }
360  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_LIMIT])){
361  //Resources to erturn 10
362  $parameters[HCE_SPHINX_SEARCH_FIELD_LIMIT]='0';
363  }
364  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_CUTOFF])){
365  //Sphinx specification
366  $parameters[HCE_SPHINX_SEARCH_FIELD_CUTOFF]='0';
367  }
368  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS])){
369  //Empty list of fields lead that all defined in schema fields returned
370  $parameters[HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS]=array();
371  }
372  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_FILTERS])){
373  //Empty filters array
374  $parameters[HCE_SPHINX_SEARCH_FIELD_FILTERS]=array();
375  }
376  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_ORDER_BY])){
377  //Empty sort order by for Sphinx
378  $parameters[HCE_SPHINX_SEARCH_FIELD_ORDER_BY]=HCE_SPHINX_SEARCH_FIELD_ORDER_BY_0;
379  }
380 /*
381  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_TTL])){
382  //Empty ttl
383  $parameters[HCE_SPHINX_SEARCH_FIELD_TTL]=HCE_SPHINX_SEARCH_FIELD_TTL_DEFAULT;
384  }
385 */
386  if(!isset($parameters[HCE_SPHINX_SEARCH_FIELD_TIMEOUT])){
387  //Empty timeout
388  $parameters[HCE_SPHINX_SEARCH_FIELD_TIMEOUT]=HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT;
389  }
390 
391  //Order array is not set
392  if($order===null){
393  $order=array(array(HCE_SPHINX_SEARCH_FIELD_ORDER_ALG=>HCE_SPHINX_SEARCH_FIELD_ORDER_ALG_0),
394  array(HCE_SPHINX_SEARCH_FIELD_ORDER_FIELDS=>array()),
395  array(HCE_SPHINX_SEARCH_FIELD_ORDER_BY=>HCE_SPHINX_SEARCH_FIELD_ORDER_BY_DESC));
396  }
397 
398  //Set main fields
399  $ret[HCE_SPHINX_SEARCH_FIELD_Q]=$query_string;
400  $ret[HCE_SPHINX_SEARCH_FIELD_FILTERS]=$parameters[HCE_SPHINX_SEARCH_FIELD_FILTERS];
401 
402  //Set parameters
403  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][0][HCE_SPHINX_SEARCH_FIELD_QUERY_ID]=$parameters[HCE_SPHINX_SEARCH_FIELD_ID];
404  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][1][HCE_SPHINX_SEARCH_FIELD_JSON_TYPE]=$parameters[HCE_SPHINX_SEARCH_FIELD_TYPE_MASK];
405  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][2][HCE_SPHINX_SEARCH_FIELD_SORT_BY]=$parameters[HCE_SPHINX_SEARCH_FIELD_SORT_BY];
406  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][3][HCE_SPHINX_SEARCH_FIELD_ORDER_BY]=$parameters[HCE_SPHINX_SEARCH_FIELD_ORDER_BY];
407  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][4][HCE_SPHINX_SEARCH_FIELD_OFFSET]=$parameters[HCE_SPHINX_SEARCH_FIELD_OFFSET];
408  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][5][HCE_SPHINX_SEARCH_FIELD_LIMIT]=$parameters[HCE_SPHINX_SEARCH_FIELD_LIMIT];
409  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][6][HCE_SPHINX_SEARCH_FIELD_CUTOFF]=$parameters[HCE_SPHINX_SEARCH_FIELD_CUTOFF];
410  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][7][HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS]=$parameters[HCE_SPHINX_SEARCH_FIELD_RET_EXT_FIELDS];
411  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][8][HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS]=$parameters[HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS];
412 // $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][9][HCE_SPHINX_SEARCH_FIELD_TTL]=$parameters[HCE_SPHINX_SEARCH_FIELD_TTL];
413  $ret[HCE_SPHINX_SEARCH_FIELD_PARAMETERS][9][HCE_SPHINX_SEARCH_FIELD_TIMEOUT]=$parameters[HCE_SPHINX_SEARCH_FIELD_TIMEOUT];
414 
415  //Set order
416  $ret[HCE_SPHINX_SEARCH_FIELD_ORDER]=$order;
417 
418  return $ret;
419  }
420 
421 
422 /*
423  * @desc parse Sphinx search response in json format
424  * @param $response_body - content of response message json string
425  *
426  * @return ACN Sphinx search response array
427  * error codes: 1 - Error of message type, is not Sphinx or general json parsing error, 2 - Error of message structure, no HCE_SPHINX_SEARCH_FIELD_DATA field
428  */
429  function hce_sphinx_search_parse_json($response_body){
430  $ret=array(HCE_SPHINX_SEARCH_FIELD_ERROR=>0, HCE_SPHINX_SEARCH_FIELD_DATA=>null, HCE_SPHINX_SEARCH_FIELD_TYPE=>0, HCE_SPHINX_SEARCH_FIELD_TTL=>0);
431 
432  $tmp=@json_decode($response_body, true);
433 
434  //Set type
435  if(isset($tmp[HCE_SPHINX_SEARCH_FIELD_TYPE])){
436  $ret[HCE_SPHINX_SEARCH_FIELD_TYPE]=$tmp[HCE_SPHINX_SEARCH_FIELD_TYPE];
437  }
438 
439  //Set ttl
440  if(isset($tmp[HCE_SPHINX_SEARCH_FIELD_TTL])){
441  $ret[HCE_SPHINX_SEARCH_FIELD_TTL]=$tmp[HCE_SPHINX_SEARCH_FIELD_TTL];
442  }
443 
444  //Set data
445  if(isset($tmp[HCE_SPHINX_SEARCH_FIELD_TYPE]) && $tmp[HCE_SPHINX_SEARCH_FIELD_TYPE]==HCE_HANDLER_TYPE_SPHINX){
446  if(isset($tmp[HCE_SPHINX_SEARCH_FIELD_DATA])){
447  if(SPHINX_JSON_USE_BASE64){
448  $ret[HCE_SPHINX_SEARCH_FIELD_DATA]=json_decode(base64_decode($tmp[HCE_SPHINX_SEARCH_FIELD_DATA]), true);
449  }else{
450  $ret[HCE_SPHINX_SEARCH_FIELD_DATA]=json_decode($tmp[HCE_SPHINX_SEARCH_FIELD_DATA], true);
451  }
452  }else{
453  //Error of message structure, no data field
454  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=2;
455  }
456  }else{
457  //Error of message type, is not Sphinx or general json parsing error
458  $ret[HCE_SPHINX_SEARCH_FIELD_ERROR]=1;
459  }
460 
461  return $ret;
462  }
463 
464 /*
465  * @desc get data field value from Sphinx search result object
466  * @param $response_body - content of response message json string
467  *
468  * @return ACN Sphinx search response array
469  * error codes: 1 - Error of message type, is not Sphinx or general json parsing error, 2 - Error of message structure, no HCE_SPHINX_SEARCH_FIELD_DATA field
470  */
471  function hce_sphinx_search_result_get($result, $struct_name, $field_name=null, $index=null){
472  $ret=0;
473 
474  if(isset($result[HCE_SPHINX_SEARCH_FIELD_DATA])){
475  if($struct_name==HCE_SPHINX_SEARCH_RESULTS_MI && isset($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_MI])){
476  //Match info data
477  if($index!==null){
478  $mi=$result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_MI];
479  if(isset($mi[$index])){
480  if($field_name==HCE_SPHINX_SEARCH_RESULTS_WEIGHT || $field_name==HCE_SPHINX_SEARCH_RESULTS_ID){
481  $ret=$mi[$index][$field_name];
482  }else{
483  foreach($mi[$index][HCE_SPHINX_SEARCH_RESULTS_AT] as $attribute){
484  if(isset($attribute[$field_name])){
485  $ret=$attribute[$field_name];
486  break;
487  }
488  }
489  }
490  }
491  }else{
492  $ret=count($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_MI]);
493  }
494  }elseif($struct_name==HCE_SPHINX_SEARCH_RESULTS_RI && isset($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI])){
495  if($index!==null && isset($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI][$index][$field_name])){
496  $ret=$result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI][$index][$field_name];
497  }else{
498  $ret=count($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI]);
499  }
500  }elseif($struct_name==HCE_SPHINX_SEARCH_RESULTS_WI && $index!==null && isset($result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI][$index][HCE_SPHINX_SEARCH_RESULTS_WI])){
501  $ret=$result[HCE_SPHINX_SEARCH_FIELD_DATA][HCE_SPHINX_SEARCH_RESULTS_RI][$index][HCE_SPHINX_SEARCH_RESULTS_WI];
502  }
503  }
504 
505  return $ret;
506  }
507 
508 ?>