|
|
|
@ -67,6 +67,7 @@ class Client |
|
|
|
|
public function from($number = 0) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['from'] = $number ? ($number-1)*10 : 0; |
|
|
|
|
$this->queryParams['current_page'] = $number; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -88,7 +89,7 @@ class Client |
|
|
|
|
*/ |
|
|
|
|
public function equal($params = []) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['equal'][] = $params; |
|
|
|
|
$this->queryParams['equal'] = $params; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -99,7 +100,7 @@ class Client |
|
|
|
|
*/ |
|
|
|
|
public function in($params = []) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['in'][] = $params; |
|
|
|
|
$this->queryParams['in'] = $params; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -119,13 +120,15 @@ class Client |
|
|
|
|
*/ |
|
|
|
|
public function like($params = []) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['like'][] = $params; |
|
|
|
|
//$this->queryParams['like'][] = $params; |
|
|
|
|
$this->queryParams['like'] = $params; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function orlike($params = []) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['orlike'][] = $params; |
|
|
|
|
//$this->queryParams['orlike'][] = $params; |
|
|
|
|
$this->queryParams['orlike'] = $params; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -153,7 +156,7 @@ class Client |
|
|
|
|
|
|
|
|
|
public function order($params = []) |
|
|
|
|
{ |
|
|
|
|
$this->queryParams['order'][] = $params; |
|
|
|
|
$this->queryParams['order'] = $params; |
|
|
|
|
return $this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -239,21 +242,12 @@ class Client |
|
|
|
|
] |
|
|
|
|
], |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
], |
|
|
|
|
'highlight'=>[//高亮 |
|
|
|
|
"fields"=>[ |
|
|
|
|
"goods_name" => new \stdClass() |
|
|
|
|
// "activity_name"=>[ |
|
|
|
|
// "pre_tags"=>"<b style='color:red'>", |
|
|
|
|
// "post_tags"=>"</b>" |
|
|
|
|
// ] |
|
|
|
|
], |
|
|
|
|
"pre_tags"=>"<b style='color:red'>", |
|
|
|
|
"post_tags"=>"</b>" |
|
|
|
|
], |
|
|
|
|
|
|
|
|
|
'from' => $this->queryParams['from'] ?? 0, |
|
|
|
|
'size' => $this->queryParams['size'] ?? 10 |
|
|
|
|
'size' => $this->queryParams['size'] ?? 10, |
|
|
|
|
'current_page' => $this->queryParams['current_page'] ?? 1, |
|
|
|
|
'track_total_hits' => true, |
|
|
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
@ -404,7 +398,7 @@ class Client |
|
|
|
|
|
|
|
|
|
public function query($isTotal = false) |
|
|
|
|
{ |
|
|
|
|
// try { |
|
|
|
|
try { |
|
|
|
|
$this->parseQueryParams(); |
|
|
|
|
|
|
|
|
|
//dd($this->queryParams); |
|
|
|
@ -414,59 +408,53 @@ class Client |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($isTotal === true) { |
|
|
|
|
|
|
|
|
|
unset( |
|
|
|
|
|
|
|
|
|
$this->queryParams['from'], |
|
|
|
|
|
|
|
|
|
$this->queryParams['size'], |
|
|
|
|
|
|
|
|
|
$this->queryParams['_source'] |
|
|
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$count = $this->client->count($this->queryParams); |
|
|
|
|
|
|
|
|
|
return (int)$count['count']; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($this->queryParams)) { |
|
|
|
|
|
|
|
|
|
//高亮 |
|
|
|
|
$this->queryParams['body']['highlight'] = [ |
|
|
|
|
"fields"=>[ |
|
|
|
|
"goods_name" => new \stdClass() |
|
|
|
|
], |
|
|
|
|
"pre_tags"=>"<b style='color:red'>", |
|
|
|
|
"post_tags"=>"</b>" |
|
|
|
|
]; |
|
|
|
|
$result = $this->client->search($this->queryParams); |
|
|
|
|
echo "<pre>"; |
|
|
|
|
print_r($result); |
|
|
|
|
exit(); |
|
|
|
|
|
|
|
|
|
if (!empty($result['hits']['hits'])) { |
|
|
|
|
|
|
|
|
|
$return = []; |
|
|
|
|
|
|
|
|
|
foreach ($result['hits']['hits'] as $row) { |
|
|
|
|
|
|
|
|
|
$return[] = $row['_source']; |
|
|
|
|
|
|
|
|
|
$data = [ |
|
|
|
|
'current_page' => 1, |
|
|
|
|
'data' => [], |
|
|
|
|
'last_page' => 0, |
|
|
|
|
'per_page' => $this->queryParams['size'], |
|
|
|
|
'total' => 0, |
|
|
|
|
]; |
|
|
|
|
if (isset($result['hits']) && !empty($result['hits'])) { |
|
|
|
|
$list = $result['hits']['hits']; |
|
|
|
|
$arr = []; |
|
|
|
|
foreach ($list as $key => &$value) { |
|
|
|
|
$value['_source']['highlight_goods_name'] = $value['highlight']['goods_name'][0] ?? ""; |
|
|
|
|
$arr[] = $value['_source']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $return; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
return []; |
|
|
|
|
|
|
|
|
|
$data['current_page'] = $this->queryParams['current_page']; |
|
|
|
|
$data['data'] = $arr; |
|
|
|
|
$data['last_page'] = ceil($result['hits']['total']['value'] / $this->queryParams['size']); |
|
|
|
|
$data['per_page'] = $this->queryParams['size']; |
|
|
|
|
$data['total'] = $result['hits']['total']['value']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $data; |
|
|
|
|
} |
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
$msg = $e->getMessage(); |
|
|
|
|
//$msg = '服务器开小差了~'; |
|
|
|
|
throw new Exception($msg); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// } catch (\Exception $e) { |
|
|
|
|
// |
|
|
|
|
// $msg = $e->getMessage(); |
|
|
|
|
// |
|
|
|
|
// $msg = '服务器开小差了~'; |
|
|
|
|
// |
|
|
|
|
// throw new Exception($msg); |
|
|
|
|
// |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|