composer require shopwwi/webman-search
xunsearch 使用訊搜搜索選定器
composer require hightman/xunsearch
meilisearch 使用meilisearch選定器
composer require meilisearch/meilisearch-php
elasticsearch 使用elasticsearch選定器
composer require elasticsearch/elasticsearch
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/
php webman shopwwi:search
use \Shopwwi\WebmanSearch\Facade\Search;
// id為主鍵字段 index為索引
$search = Search::use('meilisearch',['id'=>'id','index'=>'index']);
php webman search:create:index // es搜索必須先執(zhí)行此命令創(chuàng)建索引
php webman search:update:index // 更新索引配置
php webman search:drop:index //刪除索引
- 寫入文檔
```php
$data = [
['id' => 1, 'title' => '我從來都不曾承認我其實是個大帥哥' ,'create_at' => 2022-03-24 08:08:08,'type' => 'A'],
['id' => 2, 'title' => '時間萬物除我之外' ,'create_at' => 2022-03-24 09:08:08,'type' => 'B'],
['id' => 3, 'title' => '你看見我的小熊了嗎?' ,'create_at' => 2022-03-24 10:08:08,'type' => 'B'],
['id' => 4, 'title' => '這是一個神奇的世界,因為你永遠不會知道我在哪' ,'create_at' => 2022-03-24 10:08:08,'type' => 'C']
]
$search->create($data); //批量寫入 支持單條 寫入一維數(shù)組即可
$data = [
['id' => 3, 'title' => '你看見我的小熊了嗎?哈哈哈']
];
$search->update($data); // 批量修改 主鍵必須存在 單條修改寫入一維數(shù)組
$search->destroy(3); //則會刪除id為3的數(shù)據(jù)
$search->destroy([2,3]); //批量刪除 id為2和3的數(shù)據(jù)
$search->clear();
use \Shopwwi\WebmanSearch\Facade\Search;
// 基礎(chǔ)查詢關(guān)鍵詞
$result = $search->q('我')->get();
// 全部查詢 默認只顯示20條
$result = $search->get();
//指定字段(默認指定所有)
$result = $search->select(['id','title'])->get();
//限定查詢數(shù)量 默認為20
$result = $search->limit(100)->q('我')->get();
//帶條件搜索(一定要設(shè)定可檢索字段 不然無效,一般在初始化新增之后)
// 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 存在當中
// orWhereIn(string $column, $values) static 搜索或者存在當然
// whereNotIn(string $column,array $values, string $boolean = 'AND') static 搜索不存在當中
// orWhereNotIn(string $column, $values) static 搜索或者不存在當中
$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ī)則根據(jù)其地理位置過濾結(jié)果
$result = $search->where('type','B')->whereRaw('_geoRadius(45.4628328, 9.1076931, 2000)')->limit(100)->q('我')->get();
// 分頁
$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));
獲取建議詞(如搜索s則會出現(xiàn)與s相關(guān)的漢字數(shù)組 ,meilisearch不支持,es請使用原sdk方法查詢)
$result = $search->limit(20)->q('s')->suggest();// 不帶統(tǒng)計數(shù),數(shù)量都為0
$result = $search->limit(20)->q('s')->suggest(true); // 帶統(tǒng)計數(shù)
$result = $search->first(2);
// 字段排序(一定要設(shè)定可排序字段 不然無效,一般在初始化新增之后)
//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方法(各方法請查看各選定器包說明文檔)
//返回所選定器本身SDK方法
$search = Search::use('meilisearch',['id'=>'id','index'=>'index'])->us();
//如meilisearch調(diào)用任務(wù)方法
$search->getTasks(); // 查詢所有任務(wù)
直接運行沒問題 打包Phar后出現(xiàn)找不到類的情況
Class 'Shopwwi\WebmanSearch\Command\SearchCreateIndexCommand' not found in phar:
'exclude_pattern' => '#^(?!.*(composer.json|/.github/|/.idea/|/.git/|/.setting/|/runtime/|/vendor-bin/|/build/|/vendor/webman/admin/|/public/cert|public/json/|public/upload/))(.*)$#',
'exclude_files' => [
'.env', 'LICENSE', 'composer.json', 'composer.lock', 'start.php', 'webman.phar', 'webman.bin'
]
看配置文件應(yīng)該是沒有被排除的 現(xiàn)在只有把command里的四個命令都取消了phar包才能正常運行
并且看了phar包里 composer相關(guān)依賴都是有的
這個是phar包下vendor
Fatal error: Declaration of Elastic\Elasticsearch\Response\Elasticsearch::withStatus(int $code, string $reasonPhrase = ''): Psr\Http\Message\Resp
onseInterface must be compatible with Psr\Http\Message\ResponseInterface::withStatus($code, $reasonPhrase = '') in D:\php_project\dianwai_api\wz_
im_message\vendor\elasticsearch\elasticsearch\src\Response\Elasticsearch.php on line 87
Worker process terminated with ERROR: E_COMPILE_ERROR "Declaration of Elastic\Elasticsearch\Response\Elasticsearch::withStatus(int $code, string
$reasonPhrase = ''): Psr\Http\Message\ResponseInterface must be compatible with Psr\Http\Message\ResponseInterface::withStatus($code, $reasonPhra
se = '') in D:\php_project\dianwai_api\wz_im_message\vendor\elasticsearch\elasticsearch\src\Response\Elasticsearch.php on line 87"
process D:\php_project\dianwai_api\wz_im_message\start.php terminated and try to restart
小項目想直接用xunsearch,報錯。
PHP 8.1
webman 1.5.6
hightman/xunsearch 1.4.7
xunsearch-server 1.4.17
在config/.../search/下新建ini/goods.ini
業(yè)務(wù)代碼
<?php
declare (strict_types=1);
namespace app\admin\controller\v1;
use Shopwwi\WebmanSearch\Facade\Search;
class SearchController extends BaseController {
public function test() {
$search = Search::use('xunsearch', ['id' => 'id', 'index' => 'goods']);
$data = [
['id' => 1, 'title' => '我從來都不曾承認我其實是個大帥哥', 'goods_id' => '123', 'store_id' => 10, 'create_at' => '2022-03-24 08:08:08', 'type' => 'A'],
['id' => 2, 'title' => '時間萬物除我之外', 'goods_id' => '132', 'store_id' => 10, 'create_at' => '2022-03-24 09:08:08', 'type' => 'B'],
['id' => 3, 'title' => '你看見我的小熊了嗎?', 'goods_id' => '153', 'store_id' => 11, 'create_at' => '2022-03-24 10:08:08', 'type' => 'B'],
['id' => 4, 'title' => '這是一個神奇的世界,因為你永遠不會知道我在哪', 'goods_id' => '163', 'store_id' => 12, 'create_at' => '2022-03-24 10:08:08', 'type' => 'C']
];
$search->create($data);
$res = $search->q('我')->get();
d($res);
}
}
PHP Fatal error: During inheritance of ArrayAccess: Uncaught [XSErrorException] vendor/hightman/xunsearch/lib/XSDocument.class.php(300): Return type of XSDocument::offsetGet($name) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice(8192) in /www/wwwroot/admin.webman.com/vendor/hightman/xunsearch/lib/XSDocument.class.php on line 43
Fatal error: During inheritance of ArrayAccess: Uncaught [XSErrorException] vendor/hightman/xunsearch/lib/XSDocument.class.php(300): Return type of XSDocument::offsetGet($name) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice(8192) in /www/wwwroot/admin.webman.com/vendor/hightman/xunsearch/lib/XSDocument.class.php on line 43
Worker[1235] process terminated with ERROR: E_ERROR "During inheritance of ArrayAccess: Uncaught [XSErrorException] vendor/hightman/xunsearch/lib/XSDocument.class.php(300): Return type of XSDocument::offsetGet($name) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice(8192) in /www/wwwroot/admin.webman.com/vendor/hightman/xunsearch/lib/XSDocument.class.php on line 43"
worker[webman:1235] exit with status 65280
收藏,后面估計要用到了