spatie / searchindex
存储和检索 Elasticsearch 或 Algolia 中的对象
Requires
- php: >=5.5.0
- illuminate/support: ^5.1
Requires (Dev)
- algolia/algoliasearch-client-php: ^1.6
- elasticsearch/elasticsearch: ^1.3
- phpspec/phpspec: ^2.1
- phpunit/phpunit: ^4.8.36
Suggests
- algolia/algoliasearch-client-php: Install if you want use Algolia as your search index
- elasticsearch/elasticsearch: Install if you want use Elasticsearch as your search index. Use version constraint ^1.3
README
从搜索索引存储和检索对象
这是一个有见地的 Laravel 5.1 包,用于从搜索索引存储和检索对象。目前支持 Elasticsearch 和 Algolia。
安装此包后,可以轻松地对对象进行索引和检索
//$product is an object that implements the Searchable interface SearchIndex::upsertToIndex($product); SearchIndex::getResults('look for this');
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里。
支持我们
通过观看我们的付费视频课程,了解如何创建此类包
我们投入了大量资源来创建 最佳开源包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢您从家乡寄给我们明信片,说明您正在使用我们的哪个包。您可以在我们的 联系页面 上找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上。
明信片软件
您可以自由使用此包(它是 MIT 许可),但如果它进入您的生产环境,则必须从您的家乡寄给我们一张明信片,说明您正在使用我们的哪个包。
我们的地址是:Spatie,Kruikstraat 22,2018 安特卫普,比利时。
最佳明信片将发布在我们的网站上的开源页面上。
安装
此包可以通过 Composer 安装。
composer require spatie/searchindex
您必须安装此服务提供者。
// config/app.php 'providers' => [ ... Spatie\SearchIndex\SearchIndexServiceProvider::class, ];
此包还包含一个门面,它提供了一种调用类的方法。
// config/app.php 'aliases' => [ ... 'SearchIndex' => Spatie\SearchIndex\SearchIndexFacade::class, ]
您可以使用以下命令发布配置文件:
php artisan vendor:publish --provider="Spatie\SearchIndex\SearchIndexServiceProvider"
配置文件中的选项使用合理的默认值设置,应该很容易理解。
下一个安装步骤取决于您是想使用 Elasticsearch 还是 Algolia。
Elasticsearch
要使用 Elasticsearch,您必须安装官方 1.x 系列的低级别客户端
composer require elasticsearch/elasticsearch "^1.3"
您还应该有一个安装了 Elasticsearch 的服务器。如果您想在您的本地开发机器上安装它,可以使用来自出色的 Vaprobash 仓库 的以下说明 这些说明。
Algolia
要使用 Algolia,您必须安装官方的低级别客户端
composer require algolia/algoliasearch-client-php
使用方法
准备你的对象
你想要存储在索引中的对象应该实现提供的 Spatie\SearchIndex\Searchable
接口。
namespace Spatie\SearchIndex; interface Searchable { /** * Returns an array with properties which must be indexed * * @return array */ public function getSearchableBody(); /** * Return the type of the searchable subject * * @return string */ public function getSearchableType(); /** * Return the id of the searchable subject * * @return string */ public function getSearchableId();
以下是一个如何使用 Eloquent 模型实现的示例
class Product extends Eloquent implements Searchable { ... /** * Returns an array with properties which must be indexed * * @return array */ public function getSearchableBody() { $searchableProperties = [ 'name' => $this->name, 'brand' => $this->brand->name, 'category' => $this->category->name ]; return $searchableProperties; } /** * Return the type of the searchable subject * * @return string */ public function getSearchableType() { return 'product'; } /** * Return the id of the searchable subject * * @return string */ public function getSearchableId() { return $this->id; } }
搜索索引将使用返回的 searchableType 和 searchableId 来识别索引中的对象。
将对象添加到索引中
如果你使用的是外观,那就更简单了。
//$product is an object that implements the Searchable interface SearchIndex::upsertToIndex($product);
更新索引中的对象
你可能已经猜到了。
//$product is an object that implements the Searchable interface SearchIndex::upsertToIndex($product);
从索引中删除对象
是的,很简单。
//$product is an object that implements the Searchable interface SearchIndex::removeFromIndex($product);
或者你也可以通过传递类型和 id 来从索引中删除对象
SearchIndex::removeFromIndexByTypeAndId('product', 1);
当你已经删除了模型时,这很有用。
清空整个索引
如果你可以用你的 Facebook 账户做到这一点就好了。
SearchIndex::clearIndex();
在索引上执行搜索
你可以使用这个方法检索搜索结果
SearchIndex::getResults($query);
Elasticsearch
$query
应该是一个符合Elasticsearch 文档中提供的方案的数组。
一个执行模糊搜索的查询,它操作索引中的所有字段可能看起来像这样
$query = [ 'body' => [ 'from' => 0, 'size' => 500, 'query' => [ 'fuzzy_like_this' => [ '_all' => [ 'like_text' => 'look for this', 'fuzziness' => 0.5, ], ], ], ] ];
返回的搜索结果是简单的数组中序列化的 Elasticsearch 响应元素。你可以在官方 Elasticsearch 文档中看到一个示例。
Algolia
你可以传递一个字符串来搜索索引
SearchIndex::getResults('look for this');
要执行更复杂的查询,可以传递一个数组。阅读官方文档以了解可以做什么。
所有其他操作
对于所有其他操作,你可以获取底层的客户端
SearchIndex::getClient(); // will return the Elasticsearch or Algolia client.
查询助手
如果你使用 Algolia,你可以使用 SearchQuery
-对象来执行搜索。
use Spatie\SearchIndex\Query\Algolia\SearchIndex(); $searchQuery = new SearchQuery(); $searchQuery->searchFor('my query') ->withFacet('facetName', 'facetValue'); //a searchQuery object may be passed to the getResults-function directly. SearchIndex::getResults($searchQuery);
测试
此包附带一组单元测试。每次包更新时,Travis CI 都会自动运行它们。
你也可以手动运行它们。你首先需要运行 composer install --dev
来安装 phpspec。完成这些后,你可以使用 vendor/bin/phpspec run
运行测试。
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果您发现任何与安全相关的问题,请通过电子邮件security@spatie.be联系,而不是使用问题跟踪器。
致谢
关于Spatie
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 在这里。