??全文搜索(支持elasticsearch,meilisearch,xunsearch)

安裝
composer require shopwwi/webman-search
- 如果覺(jué)得方便了你 不要吝嗇你的小星星哦 tycoonSong8988354@qq.com
使用方法
搜索選定器
-
xunsearch 使用訊搜搜索選定器
composer require hightman/xunsearch
-
meilisearch 使用meilisearch選定器
composer require meilisearch/meilisearch-php
-
elasticsearch 使用elasticsearch選定器(需8.0以上版本)
composer require elasticsearch/elasticsearch
搜索選定器服務(wù)端安裝
-
meilisearch 安裝方法查看 https://docs.meilisearch.com/learn/getting_started/quick_start.html
-
xunsearch 安裝方法查看 http://www.xunsearch.com/doc/php/guide/start.installation
-
elasticsearch 安裝方法查看 https://www.elastic.co/
搜索配置
-
配置config/plugin/shopwwi/search/app.php 內(nèi)配置 默認(rèn)不需要配置
-
生成密鑰(如果生成了 啟動(dòng)meilisearch服務(wù)端的時(shí)候記得保持一致,其它搜索器不需要)
php webman shopwwi:search
-
選定器調(diào)用(默認(rèn)調(diào)用config設(shè)置的默認(rèn))
use \Shopwwi\WebmanSearch\Facade\Search; // id為主鍵字段 index為索引 $search = Search::use('meilisearch',['id'=>'id','index'=>'index']);
-
執(zhí)行命令方法
php webman search:create:index // es搜索必須先執(zhí)行此命令創(chuàng)建索引
php webman search:update:index // 更新索引配置
php webman search:drop:index //刪除索引
- 寫(xiě)入文檔
```php
$data = [
['id' => 1, 'title' => '我從來(lái)都不曾承認(rèn)我其實(shí)是個(gè)大帥哥' ,'create_at' => 2022-03-24 08:08:08,'type' => 'A'],
['id' => 2, 'title' => '世間萬(wàn)物除我之外' ,'create_at' => 2022-03-24 09:08:08,'type' => 'B'],
['id' => 3, 'title' => '你看見(jiàn)我的小熊了嗎?' ,'create_at' => 2022-03-24 10:08:08,'type' => 'B'],
['id' => 4, 'title' => '這是一個(gè)神奇的世界,因?yàn)槟阌肋h(yuǎn)不會(huì)知道我在哪' ,'create_at' => 2022-03-24 10:08:08,'type' => 'C']
]
$search->create($data); //批量寫(xiě)入 支持單條 寫(xiě)入一維數(shù)組即可
- 更新文檔
$data = [
['id' => 3, 'title' => '你看見(jiàn)我的小熊了嗎?哈哈哈']
];
$search->update($data); // 批量修改 主鍵必須存在 單條修改寫(xiě)入一維數(shù)組
- 刪除文檔
$search->destroy(3); //則會(huì)刪除id為3的數(shù)據(jù)
$search->destroy([2,3]); //批量刪除 id為2和3的數(shù)據(jù)
- 刪除索引
$search->clear();
- 數(shù)據(jù)查詢
use \Shopwwi\WebmanSearch\Facade\Search;
// 基礎(chǔ)查詢關(guān)鍵詞
$result = $search->q('我')->get();
// 全部查詢 默認(rèn)只顯示20條
$result = $search->get();
//指定字段(默認(rèn)指定所有)
$result = $search->select(['id','title'])->get();
//限定查詢數(shù)量 默認(rèn)為20
$result = $search->limit(100)->q('我')->get();
//帶條件搜索(一定要設(shè)定可檢索字段 不然無(wú)效,一般在初始化新增之后)
// where($column, $operator = null, $value = null, string $boolean = 'AND') static 搜索
// orWhere($column, $operator = null, $value = null) static 搜索或者
Search::updateFilterableAttributes(['id','title','type','create_at']);
$result = $search->where('type','B')->limit(100)->q('我')->get();
$result = $search->where('type','!=','B')->limit(100)->q('我')->get();
$result = $search->orWhere('type','B')->limit(100)->q('我')->get();
// whereIn($column, $values, string $boolean = 'AND', bool $not = false) static 存在當(dāng)中
// orWhereIn(string $column, $values) static 搜索或者存在當(dāng)然
// whereNotIn(string $column,array $values, string $boolean = 'AND') static 搜索不存在當(dāng)中
// orWhereNotIn(string $column, $values) static 搜索或者不存在當(dāng)中
$result = $search->whereIn('type',['A','B'])->limit(100)->q('我')->get();
$result = $search->whereNotIn('type',['A','B'])->limit(100)->q('我')->get();
$result = $search->orWhereIn('type',['A','B'])->limit(100)->q('我')->get();
$result = $search->orWhereNotIn('type',['A','B'])->limit(100)->q('我')->get();
// whereRaw($sql, $boolean = 'AND') static 原生數(shù)據(jù)查詢
$result = $search->where('type','B')->whereRaw('(id = 1 OR id = 2)')->limit(100)->q('我')->get();
//whereBetween($column,array $values, string $boolean = 'AND')
$result = $search->whereBetween('id',[1,5])->limit(100)->q('我')->get();
//如果您的文檔包含_geo數(shù)據(jù),您可以使用_geoRadius內(nèi)置過(guò)濾規(guī)則根據(jù)其地理位置過(guò)濾結(jié)果
$result = $search->where('type','B')->whereRaw('_geoRadius(45.4628328, 9.1076931, 2000)')->limit(100)->q('我')->get();
// 分頁(yè)
$result = $search->where('type','B')->limit(20)->q('我')->paginate(\request()->input('page',1));
//關(guān)鍵詞高亮
$result = $search->where('type','B')->limit(20)->highlight(['title'])->q('我')->paginate(\request()->input('page',1));
-
elasticSearch新升級(jí)whereRaw特定寫(xiě)法 支持query下的全部寫(xiě)法
$search->whereRaw(['bool'=>['filter'=>[['term'=>['color'=>'red']],['term'=>['color'=>'blue']]]]]); $search->whereRaw(['bool'=>['must'=>[['term'=>['color'=>'red']],['term'=>['color'=>'blue']]]]]); // 如需使用aggs $result = $search->aggs(['avg_price'=>['avg'=>['field'=>'price']]])->q('華為')->get(); // 獲取原文返回內(nèi)容 $result->raw
-
獲取建議詞(如搜索s則會(huì)出現(xiàn)與s相關(guān)的漢字?jǐn)?shù)組 ,meilisearch不支持,es請(qǐng)使用原sdk方法查詢)
$result = $search->limit(20)->q('s')->suggest();// 不帶統(tǒng)計(jì)數(shù),數(shù)量都為0 $result = $search->limit(20)->q('s')->suggest(true); // 帶統(tǒng)計(jì)數(shù)
-
獲取指定文檔
$result = $search->first(2);
- 字段排序
// 字段排序(一定要設(shè)定可排序字段 不然無(wú)效,一般在初始化新增之后)
//1.使用orderBy方法
$result = $search->where('type','B')->orderBy('create_at','desc')->orderBy('id')->limit(100)->q('我')->get();
//2.全局設(shè)定
$result = $search->where('type','B')->limit(100)->q('我')->get();
-
獲取原SDK方法(各方法請(qǐng)查看各選定器包說(shuō)明文檔)
//返回所選定器本身SDK方法 $search = Search::use('meilisearch',['id'=>'id','index'=>'index'])->us(); //如meilisearch調(diào)用任務(wù)方法 $search->getTasks(); // 查詢所有任務(wù)