aloko / elasticquent5
优雅地通过Laravel 5 eloquently访问Elasticsearch类型
Requires
- php: >=5.5.9
- elasticsearch/elasticsearch: ~1.0
- illuminate/support: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is auto-updated.
Last update: 2024-09-18 11:59:14 UTC
README
Elasticsearch for Eloquent Laravel 5 Models
注意: Aloko\Elasticquent5 是基于 adamfairholm/Elasticquent 构建的,后者是为 Laravel 4.x 定制的。由于 该包的作者 没有合并更改以兼容 Laravel 5 的拉取请求,我决定发布这个版本。
Elasticquent 通过将它们映射到 Elasticsearch 类型,使使用 Elasticsearch 和 Eloquent 模型变得更容易。您可以使用默认设置,或者在模型中定义 Elasticsearch 如何索引和搜索您的 Eloquent 模型。
Elasticquent 使用 官方 Elasticsearch PHP API。要开始使用,您应该具备基本的 Elasticsearch 工作原理知识(索引、类型、映射等)。此版本适用于 Elasticsearch 1.x。
内容
概述
Elasticquent 允许您轻松地将 Eloquent 模型索引和搜索其内容。
$books = Book::where('id', '<', 200)->get();
$books->addToIndex();
当您进行搜索时,您将得到一个带有特殊 Elasticsearch 功能的 Eloquent 集合,而不是一个普通的搜索结果数组。
$books = Book::search('Moby Dick')->get();
echo $books->totalHits();
此外,您仍然可以使用所有 Eloquent 集合功能
$books = $books->filter(function($book)
{
return $book->hasISBN();
});
查看其余文档,了解如何开始使用 Elasticsearch 和 Elasticquent!
Elasticquent 的工作原理
当使用数据库时,Eloquent 模型是从数据库表读取的数据填充的。使用 Elasticquent 时,模型是通过在 Elasticsearch 中索引的数据填充的。使用 Elasticsearch 进行搜索的整个想法是它速度快,负载轻,因此您模型的功能将由为您的文档已索引的数据决定。
设置
在开始使用 Elasticquent 之前,请确保您已安装 Elasticsearch。
步骤 1 - 安装
composer require aloko/elasticquent5
步骤 2 - 添加服务提供者
将以下行添加到您的 config/app.php 文件的 providers
数组中
Aloko\Elasticquent\ElasticquentServiceProvider::class
步骤 3 - 发布配置文件
在您的命令行中输入以下行
php artisan vendor:publish --provider="Aloko\Elasticquent\ElasticquentServiceProvider" --tag="config"
此文件将包含您 Elasticsearch 实例的不同配置(例如主机、日志文件、默认索引等),文件将位于您的 config/elasticquent.php 中。您可以将其更改为您喜欢的配置。
最终步骤 - 将其添加到模型中
use Aloko\Elasticquent\ElasticquentTrait; class Book extends Eloquent { use ElasticquentTrait; }
现在您的Eloquent模型有一些额外的方法,这些方法可以更轻松地使用Elasticsearch索引模型的数据。
下一步
索引和映射
虽然您当然可以通过Elasticsearch API构建索引和映射,但您也可以使用一些辅助方法直接从模型中构建索引和类型。
如果您想简单地创建索引,Elastiquent模型提供了一个功能。
Book::createIndex($shards = null, $replicas = null);
对于映射,您可以在模型中设置一个mappingProperties
属性,并使用一些映射函数。
protected $mappingProperties = array( 'title' => array( 'type' => 'string', 'analyzer' => 'standard' ) );
如果您想根据映射属性设置模型类型的映射,可以使用以下方法。
Book::putMapping($ignoreConflicts = true);
要删除映射
Book::deleteMapping();
要重建映射(删除并重新添加,当您对映射进行重要更改时很有用)
Book::rebuildMapping();
您还可以获取类型映射并检查其是否存在。
Book::mappingExists();
Book::getMapping();
设置自定义索引名称
Elastiquent将使用default
作为您的索引名称,但您可以通过编辑config/elasticquent.php配置文件来设置自定义索引名称:
<?php return array( /* |-------------------------------------------------------------------------- | Default Index Name |-------------------------------------------------------------------------- | | This is the index name that Elastiquent will use for all | Elastiquent models. */ 'default_index' => 'my_custom_index_name', );
设置自定义类型名称
默认情况下,Elasticquent将使用模型的表名作为索引的名称。如果您想覆盖它,可以使用getTypeName
函数。将此函数添加到您的模型中(例如Book)。
function getTypeName() { return 'my_books'; }
要检查Elastiquent模型的类型是否存在,使用typeExists
$typeExists = Book::typeExists();
索引文档
要索引Eloquent模型中的所有条目,使用addAllToIndex
Book::addAllToIndex();
您也可以索引模型集合
$books = Book::where('id', '<', 200)->get();
$books->addToIndex();
您还可以索引单个条目
$book = Book::find($id);
$book->addToIndex();
您还可以重新索引整个模型
Book::reindex();
搜索
在Elastiquent中有两种搜索方法。第一种是简单的术语搜索,搜索所有字段。
$books = Book::search('Moby Dick');
第二种是基于查询的搜索,用于更复杂的搜索需求
$books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));
两种方法都将返回一个搜索集合。
以下是可用参数的列表
query
- 您的ElasticSearch查询aggregations
- 您希望返回的聚合。 有关聚合的详细信息。sourceFields
- 仅返回所选字段limit
- 要返回的记录数offset
- 设置记录偏移量(用于分页结果)sort
- 您的排序查询
搜索集合
当您在Elastiquent模型上搜索时,您会得到一个具有一些特殊函数的搜索集合。
您可以获取总命中数
$books->totalHits();
访问碎片数组
$books->shards();
访问最大得分
$books->maxScore();
访问超时布尔属性
$books->timedOut();
并访问took属性
$books->took();
并访问搜索聚合 - 有关聚合的详细信息
$books->getAggregations();
搜索集合文档
搜索结果集合中的项将包含来自Elasticsearch的一些额外数据。您可以使用isDocument
函数始终检查模型是否是文档。
$book->isDocument();
您可以使用以下方法检查Elasticsearch分配给该文档的文档得分
$book->documentScore();
在Elastiquent之外使用搜索集合
如果您正在处理Elastiquent之外的原生搜索数据,您可以使用Elastiquent搜索结果集合将数据转换为集合。
$client = new \Elasticsearch\Client(); $params = array( 'index' => 'default', 'type' => 'books' ); $params['body']['query']['match']['title'] = 'Moby Dick'; $collection = new Aloko\Elasticquent\ElasticquentResultCollection($client->search($params), new Book);
更多选项
文档ID
Elastiquent将使用设置为Eloquent模型的primaryKey
的任何内容作为Elasticsearch文档的ID。
文档数据
默认情况下,Elastiquent将使用整个属性数组作为Elasticsearch文档。但是,如果您想自定义搜索文档的结构,可以设置一个返回您自己的自定义文档数组的getIndexDocumentData
函数。
function getIndexDocumentData() { return array( 'id' => $this->id, 'title' => $this->title, 'custom' => 'variable' ); }
请注意这一点,因为Elastiquent在创建搜索结果集合时将文档源读入Eloquent模型属性,因此请确保您索引了足够的数据,以便您的模型功能可以使用。
使用自定义集合与 Elasticquent 结合
如果您在使用自定义集合与Eloquent模型一起使用,您只需将ElasticquentCollectionTrait
添加到您的集合中,这样您就可以使用addToIndex
了。
use Aloko\Elasticquent; class MyCollection extends \Illuminate\Database\Eloquent\Collection { use ElasticquentCollectionTrait; }
许可证
MIT许可证(MIT)
特此授予任何人免费获取本软件及其相关文档副本(以下简称“软件”)的权利,不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许软件提供者进行上述操作,但需遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、特定用途适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任是因合同、侵权或其他原因产生的,也不论该责任与软件或其使用或操作有关。