drauta/elasticquent

将 Laravel Eloquent 模型映射到 Elasticsearch 类型。

v1.0.4 2016-01-11 22:20 UTC

This package is not auto-updated.

Last update: 2024-09-20 09:23:26 UTC


README

Elasticsearch for Eloquent Laravel Models

Elasticquent 通过将 Eloquent 模型映射到 Elasticsearch 类型,使与 Elasticsearch 和 Eloquent 模型一起工作变得更加容易。您可以使用默认设置或在模型中定义 Elasticsearch 如何索引和搜索您的 Eloquent 模型。

Elasticquent 使用 官方 Elasticsearch PHP API。要开始,您应该具备基本的 Elasticsearch 工作原理知识(索引、类型、映射等)。

Elasticsearch 要求

您必须运行至少 Elasticsearch 1.0。Elasticsearch 0.9 及以下版本将无法工作,也不受支持。

内容

报告问题

如果您发现任何问题,请随时通过 GitHub 的错误跟踪器 报告此项目的问题。

或者,分叉项目并提交一个拉取请求 :)

概述

Elasticquent 允许您轻松地将 Eloquent 模型索引和搜索其内容到 Elasticsearch。

    $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

要开始,将 Elasticquent 添加到您的 composer.json 文件中

"elasticquent/elasticquent": "dev-master"

运行一次 composer update 后,将 Elasticquent 特性添加到任何您想要能够索引到 Elasticsearch 的 Eloquent 模型中

use Elasticquent\ElasticquentTrait;

class Book extends Eloquent {

    use ElasticquentTrait;

}

现在,您的 Eloquent 模型有一些额外的功能,可以更轻松地使用 Elasticsearch 索引模型的数据。

Elasticsearch 配置

如果您需要传递特殊的配置数组给 Elasticsearch,您可以在 /app/config/elasticquent.php(Laravel 4)或 /config/elasticquent.php(Laravel 5)中的 elasticquent.php 配置文件中添加它

<?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);

对于映射,您可以在模型中设置 mappingProperties 属性并使用一些映射函数

protected $mappingProperties = array(
   'title' => array(
        'type' => 'string',
        'analyzer' => 'standard'
    )
);

如果您想根据映射属性设置模型类型的映射,可以使用

    Book::putMapping($ignoreConflicts = true);

删除映射

    Book::deleteMapping();

为了重新构建(删除并重新添加,当您对映射进行重要更改时非常有用)一个映射

    Book::rebuildMapping();

您还可以获取类型映射并检查其是否存在。

    Book::mappingExists();
    Book::getMapping();

设置自定义索引名称

Elastiquent 将使用 default 作为您的索引名称,但您可以通过在 /app/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 函数。

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\Collectionchunk 方法将 Elasticquent 集合分割成多个大小为给定值的较小集合

    $all_books = Book::searchByQuery(array('match' => array('title' => 'Moby Dick')));
    $books = $all_books->chunk(10);

在 Elasticquent 外部使用搜索集合

如果您正在处理来自 Elasticquent 外部的原始搜索数据,您可以使用 Elasticquent 搜索结果集合将那些数据转换为集合。

$client = new \Elasticsearch\Client();

$params = array(
    'index' => 'default',
    'type'  => 'books'
);

$params['body']['query']['match']['title'] = 'Moby Dick';

$collection = new \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

class MyCollection extends \Illuminate\Database\Eloquent\Collection {

    use ElasticquentCollectionTrait;
}

路线图

Elastiquent 当前需要

  • 模拟 ES API 调用的测试。
  • 对路由的支持。