ximil / elasticquent
将 Laravel Eloquent 模型映射到 Elasticsearch 类型。
此包的官方仓库似乎已消失,因此该包已被冻结。
Requires
- php: >=5.4.0
- elasticsearch/elasticsearch: >1.0 <2.1
- illuminate/config: ~4.2|^5
- illuminate/database: ~4.2|^5
- nesbot/carbon: ~1.0
Requires (Dev)
- phpunit/phpunit: ~5.0
This package is not auto-updated.
Last update: 2021-12-17 07:19:06 UTC
README
Elasticsearch for Eloquent Laravel Models
Elasticquent 通过将 Eloquent 模型映射到 Elasticsearch 类型,简化了与 Elasticsearch 和 Eloquent 模型的协作。您可以使用默认设置或直接在模型中定义 Elasticsearch 应如何索引和搜索您的 Eloquent 模型。
Elasticquent 使用 官方 Elasticsearch PHP API。要开始使用,您应该具备对 Elasticsearch 基础知识的了解(索引、类型、映射等)。
Elasticsearch 要求
您必须运行 至少 Elasticsearch 1.0。低于 1.0 的 Elasticsearch 版本(包括 0.9 和以下版本)将无法工作,也不受支持。
内容
报告问题
如果您发现任何问题,请随时通过 GitHub 的错误跟踪器 报告此项目的问题。
或者,克隆项目并提交一个 pull request :)
概述
Elasticquent 允许您轻松地将 Eloquent 模型索引和搜索其内容到 Elasticsearch。
$books = Book::where('id', '<', 200)->get(); $books->addToIndex();
当您搜索时,您将获得一个包含一些特殊 Elasticsearch 功能的 Eloquent 集合,而不是一个简单的搜索结果数组。
$books = Book::search('Moby Dick'); echo $books->totalHits();
此外,您仍然可以使用所有 Eloquent 集合功能
$books = $books->filter(function ($book) { return $book->hasISBN(); });
查看其余文档,了解如何开始使用 Elasticsearch 和 Elasticquent!
如何 Elasticquent 工作
当使用数据库时,Eloquent 模型是从数据库表中读取的数据填充的。使用 Elasticquent 时,模型是通过在 Elasticsearch 中索引的数据填充的。使用 Elasticsearch 进行搜索的整个想法是其速度快且轻量级,因此您的模型功能将由已索引的文档数据决定。
设置
在开始使用 Elasticquent 之前,请确保您已安装 Elasticsearch。
要开始使用,请将 Elasticquent 添加到您的 composer.json 文件中
"elasticquent/elasticquent": "dev-master"
一旦运行了 composer update,您需要在您的 config/app.php 中注册 Laravel 服务提供者
'providers' => [ ... Elasticquent\ElasticquentServiceProvider::class, ],
我们还为 elasticsearch-php 客户端提供了一个外观(使用我们的设置连接),如果需要,请将以下内容添加到您的 config/app.php 中。
'aliases' => [ ... 'Es' => Elasticquent\ElasticquentElasticsearchFacade::class, ],
然后,将 Elasticquent 特性添加到任何您希望能够在 Elasticsearch 中索引的 Eloquent 模型中
use Elasticquent\ElasticquentTrait; class Book extends Eloquent { use ElasticquentTrait; }
现在,您的 Eloquent 模型有一些额外的函数,使得使用 Elasticsearch 索引模型数据更加方便。
Elasticsearch 配置
默认情况下,Elasticquent 将连接到 localhost:9200 并使用 default 作为索引名称,您可以在配置文件中更改这些以及其他设置。对于 Laravel 4,您可以在 /app/config/elasticquent.php 添加 elasticquent.php 配置文件,或者使用以下 Artisan 命令将配置文件发布到您的配置目录中(适用于 Laravel 5)
$ php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider"
<?php return array( /* |-------------------------------------------------------------------------- | Custom Elasticsearch Client Configuration |-------------------------------------------------------------------------- | | This array will be passed to the Elasticsearch client. | See configuration options here: | | http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_configuration.html */ 'config' => [ 'hosts' => ['localhost:9200'], 'retries' => 1, ], /* |-------------------------------------------------------------------------- | Default Index Name |-------------------------------------------------------------------------- | | This is the index name that Elastiquent will use for all | Elastiquent models. */ 'default_index' => 'my_custom_index_name', );
索引和映射
虽然您当然可以通过 Elasticsearch API 构建索引和映射,但您也可以使用一些辅助函数直接从您的模型中构建索引和类型。
如果您想以简单的方式创建索引,Elasticquent 模型有一个这样的函数
Book::createIndex($shards = null, $replicas = null);
对于自定义分析器,您可以在模型中设置 indexSettings 属性并从那里定义分析器
/** * The elasticsearch settings. * * @var array */ protected $indexSettings = [ 'analysis' => [ 'char_filter' => [ 'replace' => [ 'type' => 'mapping', 'mappings' => [ '&=> and ' ], ], ], 'filter' => [ 'word_delimiter' => [ 'type' => 'word_delimiter', 'split_on_numerics' => false, 'split_on_case_change' => true, 'generate_word_parts' => true, 'generate_number_parts' => true, 'catenate_all' => true, 'preserve_original' => true, 'catenate_numbers' => true, ] ], 'analyzer' => [ 'default' => [ 'type' => 'custom', 'char_filter' => [ 'html_strip', 'replace', ], 'tokenizer' => 'whitespace', 'filter' => [ 'lowercase', 'word_delimiter', ], ], ], ], ];
对于映射,您可以在模型中设置 mappingProperties 属性并使用一些映射函数
protected $mappingProperties = array( 'title' => array( 'type' => 'string', 'analyzer' => 'standard' ) );
如果您想根据映射属性设置模型类型的映射,可以使用以下方法
Book::putMapping($ignoreConflicts = true);
删除映射
Book::deleteMapping();
重新构建映射(删除并重新添加,当您对映射进行重要更改时很有用)
Book::rebuildMapping();
您还可以获取类型映射并检查其是否存在。
Book::mappingExists(); Book::getMapping();
设置自定义索引名称
默认情况下,Elasticquent 将在您的配置文件(config/elasticquent.php)中查找 default_index 键。要设置索引使用的默认值,您可以编辑此文件并设置 default_index 键
return array( // Other configuration keys ... /* |-------------------------------------------------------------------------- | Default Index Name |-------------------------------------------------------------------------- | | This is the index name that Elastiquent will use for all | Elastiquent models. */ 'default_index' => 'my_custom_index_name', );
如果您想拥有更动态的索引,您还可以通过在 Eloquent 模型内部覆盖默认配置的 getIndexName 方法来实现
function getIndexName() { return 'custom_index_name'; }
注意:如果没有指定索引,Elasticquent 将使用默认字符串 default。
设置自定义类型名称
默认情况下,Elasticquent 将使用模型表名作为索引的类型名称。如果您想覆盖它,可以使用 getTypeName 函数。
function getTypeName() { return 'custom_type_name'; }
要检查 Elasticquent 模型的类型是否存在,使用 typeExists
$typeExists = Book::typeExists();
索引文档
要索引 Eloquent 模型中的所有条目,使用 addAllToIndex
Book::addAllToIndex();
您还可以索引模型集合
$books = Book::where('id', '<', 200)->get(); $books->addToIndex();
您还可以索引单个条目
$book = Book::find($id); $book->addToIndex();
您还可以重新索引整个模型
Book::reindex();
搜索
Elasticquent 中有三种搜索方式。这三种方法都返回一个搜索集合。
简单术语搜索
第一种方法是简单术语搜索,搜索所有字段。
$books = Book::search('Moby Dick');
基于查询的搜索
第二种是基于查询的搜索,用于更复杂的搜索需求
public static function searchByQuery($query = null, $aggregations = null, $sourceFields = null, $limit = null, $offset = null, $sort = null)
示例
$books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));
以下是可用的参数列表
query- 您的 ElasticSearch 查询aggregations- 您希望返回的聚合。 查看聚合的详细信息。sourceFields- 限制返回的集合仅包含所选字段limit- 返回的记录数offset- 设置记录偏移量(用于分页结果)sort- 您的排序查询
原始查询
最后一种方法是将原始查询发送到Elasticsearch。这种方法在搜索Elasticsearch中的记录时提供了最大的灵活性
$books = Book::complexSearch(array( 'body' => array( 'query' => array( 'match' => array( 'title' => 'Moby Dick' ) ) ) ));
这相当于
$books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));
搜索集合
当您在Elasticquent模型上搜索时,您会获得一个具有一些特殊功能的搜索集合。
您可以获得总命中数
$books->totalHits();
访问分片数组
$books->shards();
访问最大得分
$books->maxScore();
访问超时布尔属性
$books->timedOut();
并且访问 took 属性
$books->took();
并且访问搜索聚合 - 查看聚合的详细信息
$books->getAggregations();
搜索集合文档
搜索结果集合中的项将包含来自Elasticsearch的一些额外数据。您可以使用 isDocument 函数始终检查和查看模型是否为文档
$book->isDocument();
您可以使用它来检查Elasticsearch为该文档分配的文档得分
$book->documentScore();
从Elastiquent分块结果
与 Illuminate\Support\Collection 类似,chunk 方法将Elastiquent集合分割成多个给定大小的较小集合
$all_books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick'))); $books = $all_books->chunk(10);
在Elastiquent之外使用搜索集合
如果您正在处理Elastiquent之外的原始搜索数据,则可以使用Elastiquent搜索结果集合将那些数据转换为集合。
$client = new \Elasticsearch\Client(); $params = array( 'index' => 'default', 'type' => 'books' ); $params['body']['query']['match']['title'] = 'Moby Dick'; $collection = Book::hydrateElasticsearchResult($client->search($params));
更多选项
文档ID
Elasticquent将使用您为Eloquent模型设置的 primaryKey 作为Elasticsearch文档的id。
文档数据
默认情况下,Elasticquent将使用整个属性数组作为Elasticsearch文档。但是,如果您想自定义搜索文档的结构,可以设置一个返回您自己的自定义文档数组的 getIndexDocumentData 函数。
function getIndexDocumentData() { return array( 'id' => $this->id, 'title' => $this->title, 'custom' => 'variable' ); }
请注意这一点,因为Elastiquent在创建搜索结果集合时将文档源读取到Eloquent模型属性中,因此请确保您正在索引足够的数据以用于您想使用的模型功能。
使用自定义集合与Elasticquent一起使用
如果您正在使用与Eloquent模型一起的自定义集合,只需将 ElasticquentCollectionTrait 添加到您的集合中,然后您就可以使用 addToIndex。
class MyCollection extends \Illuminate\Database\Eloquent\Collection { use ElasticquentCollectionTrait; }
路线图
Elasticquent目前需要
- 模拟ES API调用的测试。
- 对路由的支持