Skip to content

Commit 89b11d7

Browse files
committed
update
1 parent 114f96b commit 89b11d7

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ php webman search:drop:index //删除索引
129129
$result = $search->where('type','B')->limit(20)->highlight(['title'])->q('我')->paginate(\request()->input('page',1));
130130

131131
```
132+
- elasticSearch新升级whereRaw特定写法 支持query下的全部写法
133+
```php
134+
$search->whereRaw(['bool'=>['filter'=>[['term'=>['color'=>'red']],['term'=>['color'=>'blue']]]]]);
135+
$search->whereRaw(['bool'=>['must'=>[['term'=>['color'=>'red']],['term'=>['color'=>'blue']]]]]);
136+
// 如需使用aggs
137+
$result = $search->aggs(['avg_price'=>['avg'=>['field'=>'price']]])->q('华为')->get();
138+
// 获取原文返回内容
139+
$result->raw
140+
```
141+
132142
- 获取建议词(如搜索s则会出现与s相关的汉字数组 ,meilisearch不支持,es请使用原sdk方法查询)
133143
```php
134144

src/Adapter/ElasticSearch.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ElasticSearch
3838
protected $sort = [];
3939
protected $ignores = [];
4040
protected $attributesToHighlight = [];
41+
protected $attributesAggregations = [];
4142
protected $searchable = [];
4243
protected $facetsDistribution = [];
4344
protected $query = '';
@@ -316,6 +317,10 @@ public function highlight(array $attributes = [])
316317
$this->attributesToHighlight = $attributes;
317318
return $this;
318319
}
320+
public function aggs(array $attributes = []){
321+
$this->attributesAggregations = $attributes;
322+
return $this;
323+
}
319324

320325
public function facets(array $attributes = ['*'])
321326
{
@@ -416,10 +421,11 @@ protected function getFirst($result = [])
416421
$model = new Collection($result["_source"]);
417422
// match earlier version
418423
$model->_index = $result["_index"];
419-
$model->_type = $result["_type"];
424+
$model->_type = $result["_type"] ?? '';
420425
$model->_id = $result["_id"];
421426
$model->_score = $result["_score"] ?? [];
422427
$model->_highlight = isset($result["highlight"]) ? $result["highlight"] : [];
428+
$model->raw = $result;
423429
$new = $model;
424430
} else {
425431
$new = NULL;
@@ -446,6 +452,7 @@ protected function getAll($result = []){
446452
}
447453
$collect = new Collection([]);
448454
$collect->items = $new;
455+
$collect->raw = $result;
449456
$total = $result["hits"]["total"];
450457
$collect->total = is_array($total) ? $total["value"] : $total;
451458
$collect->page = request() ? request()->input('page',1) : $this->page;
@@ -487,7 +494,7 @@ protected function filters()
487494
$body["query"]["bool"]["filter"][] = ["range" => [$item['column'] => ["lte" => $item['value']]]];
488495
}
489496
if ($item['operator'] == "like") {
490-
$body["query"]["bool"]["must"][] = ["match" => [$item['column'] => $item['value']]];
497+
$body["query"]["bool"]["filter"][] = ["match" => [$item['column'] => $item['value']]];
491498
}
492499
if ($item['operator'] == "exists") {
493500
if (!$item['value']) {
@@ -504,10 +511,10 @@ protected function filters()
504511
$body["query"]["bool"]["must_not"][] = ["terms" => [$item['column'] => $item['value']]];
505512
break;
506513
case 'Raw':
507-
514+
$body["query"] = array_merge_recursive($body["query"] ?? [],$item['sql']) ;
508515
break;
509516
case 'Between':
510-
$body["query"]["bool"]["filter"][] = ["range" => [$item['column'] => ["gte" => $item['values'][0], "lte" => $item['values'][1]]]];
517+
$body["query"]["bool"]["must"][] = ["range" => [$item['column'] => ["gte" => $item['values'][0], "lte" => $item['values'][1]]]];
511518
break;
512519
case 'NotBetween':
513520
$body["query"]["bool"]["must_not"][] = ["range" => [$item['column'] => ["gte" => $item['values'][0], "lte" => $item['values'][1]]]];
@@ -528,9 +535,12 @@ protected function filters()
528535
if(count($body["query"]) == 0){
529536
unset($body["query"]);
530537
}
531-
// if(count($this->attributesToHighlight)){
532-
// $body["highlight"]['fields'] = $this->attributesToHighlight;
533-
// }
538+
if(!empty($this->attributesToHighlight)){
539+
$body["highlight"]['fields'] = $this->attributesToHighlight;
540+
}
541+
if(!empty($this->attributesAggregations)){
542+
$body['aggs'] = $this->attributesAggregations;
543+
}
534544
if (count($this->sort)) {
535545
$sortFields = array_key_exists("sort", $body) ? $body["sort"] : [];
536546
$body["sort"] = array_unique(array_merge($sortFields, $this->sort), SORT_REGULAR);

0 commit comments

Comments
 (0)