HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
search.ini.php
Go to the documentation of this file.
1 <?php
14 //Set default timezone if not set in host environment
15 @date_default_timezone_set(@date_default_timezone_get());
16 
17 require_once 'hce_cli_api.inc.php';
18 require_once 'hce_sphinx_api.inc.php';
19 
20 defined('LOG_MODE_JSON') or define('LOG_MODE_JSON', 3);
21 defined('RESPONSE_TIMEOUT_DEFAULT') or define('RESPONSE_TIMEOUT_DEFAULT', 5000);
22 
23 if(php_sapi_name()!=='cli' || !defined('STDIN')){
24  echo 'Only cli execution mode supported'.PHP_EOL;
25  exit(1);
26 }
27 
29 if(isset($args['h']) || isset($args['help']) || !isset($args['q'])){
30  echo 'Usage: '.
31  $argv[0].
32  ' [--q=query_string|"`query_string`">] [--f=<filters>] [--s=<sorting>] [--n=<max_queries_number>] [--r=<max_results>] [--d=<request_delay_ms>]'.
33  ' [--l=<log_mode{0-no,1-result,2-params,3-json,4-result+object}>]'.
34  ' [--t=<response_timeout_ms, '.RESPONSE_TIMEOUT_DEFAULT.' default>] [--host=<router_host>] [--port=<router_port>] [--cid=<client_id>]'.
35  ' [--route=<route string include of node names list comma separated>]'.
36  ' [--sphinx_timeout=<sphinx_searchd_timeout_ms '.HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT.' default>]'.PHP_EOL.
37  'filters - list of filter items delimited by semicolon. Filter item: "type{integer},field_name{string},value1,...,valueN{integers},exclude{0,1}"'.PHP_EOL.
38  'sort - list of sort parameters delimited by comma. Format: "algorithm{integer},field1[,...,fieldN]{strings},order{1,2}"'.PHP_EOL;
39  exit(1);
40 }
41 
42 $MAX_QUERIES=isset($args['n']) ? $args['n']+0 : '1';
43 $MAX_RESULTS=isset($args['r']) ? $args['r']+0 : '10';
44 $REQUEST_DELAY=isset($args['d']) ? $args['d']+0 : 0;
45 $LOG_MODE=isset($args['l']) ? $args['l']+0 : 2;
46 $RESPONSE_TIMEOUT=isset($args['t']) ? ($args['t']+0) : RESPONSE_TIMEOUT_DEFAULT;
47 $Connection_host=isset($args['host']) ? $args['host'] : 'localhost';
48 //$Connection_host='admindc03-10.snatz.com';
49 $Connection_port=isset($args['port']) ? $args['port'] : '5556';
50 $Route=isset($args['route']) ? $args['route'] : '';
52 //Query string
53 $query_string=isset($args['q']) ? str_replace('`', '"', stripslashes($args['q'])) : 'for';
54 //Sphinx request timeout
55 $sphinx_timeout=isset($args['sphinx_timeout']) ? ($args['sphinx_timeout']+0) : HCE_SPHINX_SEARCH_FIELD_TIMEOUT_DEFAULT;
56 
57 //Json filters array
58 $filters=array();
59 if(isset($args['f'])){
60  $filters1=explode(';', $args['f']);
61  foreach($filters1 as $filter1){
62  $filter1=explode(',', $filter1);
63  if(count($filter1)>2){
64  if($filter1[0]==0 || $filter1[0]==1 || $filter1[0]==2 || $filter1[0]==3){
65  $type=$filter1[0]+0;
66  }else{
67  continue;
68  }
69  //Values list
70  $values=array();
71  for($i=2; $i<count($filter1)-1; $i++){
72  $values[]=trim($filter1[$i]);
73  }
74  $filters[]=array(HCE_SPHINX_SEARCH_FIELD_FILTER_TYPE=>$type,
75  HCE_SPHINX_SEARCH_FIELD_FILTER_ATTRIB=>trim($filter1[1]),
76  HCE_SPHINX_SEARCH_FIELD_FILTER_VALUES=>$values,
77  HCE_SPHINX_SEARCH_FIELD_FILTER_EXCLUDE=>trim($filter1[count($filter1)-1])
78  );
79  }else{
80  echo 'Wrong number of filter parameters in filter item type:'.$filter1[0].', item ignored'.PHP_EOL;
81  }
82  }
83 }
84 
85 //Json order array
86 $order=array();
87 if(isset($args['s'])){
88  $order=explode(',', $args['s']);
89  if(count($order>2)){
90  $order=array(HCE_SPHINX_SEARCH_FIELD_ORDER_ALG=>($order[0]>0 ? $order[0] : '0'),
91  HCE_SPHINX_SEARCH_FIELD_ORDER_FIELDS=>array_slice($order, 1, count($order)-2),
92  HCE_SPHINX_SEARCH_FIELD_ORDER_BY=>($order[count($order)-1]=='0' ? HCE_SPHINX_SEARCH_FIELD_ORDER_BY_ASC : HCE_SPHINX_SEARCH_FIELD_ORDER_BY_DESC)
93  );
94  }else{
95  echo 'Wrong number of sort parameters, sort option ignored, default settings applied'.PHP_EOL;
96  }
97 }
98 
99 if($LOG_MODE==2){
100  echo 'Parameters:'.PHP_EOL.'query_string:"'.$query_string.'", filters:"'.json_encode($filters).'", max_queries_number:'.$MAX_QUERIES.', max_results:'.$MAX_RESULTS.
101  ', requet_delay_ms:'.$REQUEST_DELAY.', log_mode:'.$LOG_MODE.', response_timeout_ms:'.$RESPONSE_TIMEOUT.', router_host:'.$Connection_host.
102  ', router_port:'.$Connection_port.', client_id:"'.$Client_Identity.'"'.PHP_EOL.PHP_EOL;
103 }
104 
105 $ret_arrays_mask=(HCE_SPHINX_SEARCH_RET_TYPE_MI_INFO | HCE_SPHINX_SEARCH_RET_TYPE_RI_INFO | HCE_SPHINX_SEARCH_RET_TYPE_AT_INFO | HCE_SPHINX_SEARCH_RET_TYPE_WI_INFO).'';
107  HCE_SPHINX_SEARCH_FIELD_MAX_RESULTS=>$MAX_RESULTS,
108  HCE_SPHINX_SEARCH_FIELD_FILTERS=>$filters,
109  HCE_SPHINX_SEARCH_FIELD_ORDER=>$order,
110  HCE_SPHINX_SEARCH_FIELD_TIMEOUT=>$sphinx_timeout
111  ));
113 
114 if($LOG_MODE==2 || $LOG_MODE==4){
115  echo 'Request json:'.PHP_EOL.$Request_body.PHP_EOL.PHP_EOL;
116 }
117 
118 ?>