承接 aloko/elasticquent5 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aloko/elasticquent5

Composer 安装命令:

composer require aloko/elasticquent5

包简介

Access Elasticsearch types through Laravel 5 eloquently

README 文档

README

Elasticsearch for Eloquent Laravel 5 Models

Note: Aloko\Elasticquent5 is a built on top of adamfairholm/Elasticquent which was built for Laravel 4.x. As the author of that package, didn't merge the pull requests for changes to be compatible with Laravel 5. I decided to publish this one.

Elasticquent makes working with Elasticsearch and Eloquent models easier by mapping them to Elasticsearch types. You can use the default settings or define how Elasticsearch should index and search your Eloquent models right in the model.

Elasticquent uses the official Elasticsearch PHP API. To get started, you should have a basic knowledge of how Elasticsearch works (indexes, types, mappings, etc). This is meant for use with Elasticsearch 1.x.

Contents

Overview

Elasticquent allows you take an Eloquent model and easily index and search its contents in Elasticsearch.

$books = Book::where('id', '<', 200)->get();
$books->addToIndex();

When you search, instead of getting a plain array of search results, you instead get an Eloquent collection with some special Elasticsearch functionality.

$books = Book::search('Moby Dick')->get();
echo $books->totalHits();

Plus, you can still use all the Eloquent collection functionality:

$books = $books->filter(function($book)
{
    return $book->hasISBN();
});

Check out the rest of the documentation for how to get started using Elasticsearch and Elasticquent!

How Elasticquent Works

When using a database, Eloquent models are populated from data read from a database table. With Elasticquent, models are populated by data indexed in Elasticsearch. The whole idea behind using Elasticsearch for search is that its fast and light, so you model functionality will be dictated by what data has been indexed for your document.

Setup

Before you start using Elasticquent, make sure you've installed Elasticsearch.

Step 1 - Installation

composer require aloko/elasticquent5

Step 2 - Adding the Service Provider

Add the following line into your config/app.php file's providers array:

Aloko\Elasticquent\ElasticquentServiceProvider::class

Step 3 - Publishing the Config File

From your command line, enter this line:

php artisan vendor:publish --provider="Aloko\Elasticquent\ElasticquentServiceProvider" --tag="config"

This file will contain different configurations for your Elasticsearch instance (e.g. hosts, log files, default index, etc), file will reside in your config/elasticquent.php. You can change it for your preferred ones.

Final Step - Adding it to models

use Aloko\Elasticquent\ElasticquentTrait;

class Book extends Eloquent {

    use ElasticquentTrait;

}

Now your Eloquent model has some extra methods that make it easier to index your model's data using Elasticsearch.

Next Steps

Indexes and Mapping

While you can definitely build your indexes and mapping through the Elasticsearch API, you can also use some helper methods to build indexes and types right from your models.

If you want a simple way to create indexes, Elasticquent models have a function for that:

Book::createIndex($shards = null, $replicas = null);

For mapping, you can set a mappingProperties property in your model and use some mapping functions from there:

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

If you'd like to setup a model's type mapping based on your mapping properties, you can use:

Book::putMapping($ignoreConflicts = true);

To delete a mapping:

Book::deleteMapping();

To rebuild (delete and re-add, useful when you make important changes to your mapping) a mapping:

Book::rebuildMapping();

You can also get the type mapping and check if it exists.

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

Setting a Custom Index Name

Elastiquent will use default as your index name, but you can set a custom index name by editing config/elasticquent.php config file::

<?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',

);

Setting a Custom Type Name

By default, Elasticquent will use the table name of your models as the type name for indexing. If you'd like to override it, you can with the getTypeName function. Add this function into your models (e.g. Book).

function getTypeName()
{
    return 'my_books';
}

To check if the type for the Elasticquent model exists yet, use typeExists:

$typeExists = Book::typeExists();

Indexing Documents

To index all the entries in an Eloquent model, use addAllToIndex:

Book::addAllToIndex();

You can also index a collection of models:

$books = Book::where('id', '<', 200)->get();
$books->addToIndex();

You can index individual entries as well:

$book = Book::find($id);
$book->addToIndex();

You can also reindex an entire model:

Book::reindex();

Searching

There are two ways to search in Elasticquent. The first is a simple term search that searches all fields.

$books = Book::search('Moby Dick');

The second is a query based search for more complex searching needs:

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

Both methods will return a search collection.

Here's the list of available paramers:

  • query - Your ElasticSearch Query
  • aggregations - The Aggregations you wish to return. See Aggregations for details.
  • sourceFields - Limits returned set to the selected fields only
  • limit - Number of records to return
  • offset - Sets the record offset (use for paging results)
  • sort - Your sort query

Search Collections

When you search on an Elasticquent model, you get a search collection with some special functions.

You can get total hits:

$books->totalHits();

Access the shards array:

$books->shards();

Access the max score:

$books->maxScore();

Access the timed out boolean property:

$books->timedOut();

And access the took property:

$books->took();

And access search aggregations - See Aggregations for details:

$books->getAggregations();

Search Collection Documents

Items in a search result collection will have some extra data that comes from Elasticsearch. You can always check and see if a model is a document or not by using the isDocument function:

$book->isDocument();

You can check the document score that Elasticsearch assigned to this document with:

$book->documentScore();

Using the Search Collection Outside of Elasticquent

If you're dealing with raw search data from outside of Elasticquent, you can use the Elasticquent search results collection to turn that data into a collection.

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

More Options

Document IDs

Elasticquent will use whatever is set as the primaryKey for your Eloquent models as the id for your Elasticsearch documents.

Document Data

By default, Elasticquent will use the entire attribute array for your Elasticsearch documents. However, if you want to customize how your search documents are structured, you can set a getIndexDocumentData function that returns you own custom document array.

function getIndexDocumentData()
{
    return array(
        'id'      => $this->id,
        'title'   => $this->title,
        'custom'  => 'variable'
    );
}

Be careful with this, as Elasticquent reads the document source into the Eloquent model attributes when creating a search result collection, so make sure you are indexing enough data for your the model functionality you want to use.

Using Elasticquent With Custom Collections

If you are using a custom collection with your Eloquent models, you just need to add the ElasticquentCollectionTrait to your collection so you can use addToIndex.

use Aloko\Elasticquent;

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

    use ElasticquentCollectionTrait;
}

License

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

aloko/elasticquent5 适用场景与选型建议

aloko/elasticquent5 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.26k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2015 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「elasticsearch」 「laravel」 「eloquent」 「laravel 5」 「elasticquent」 「elasticquent5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aloko/elasticquent5 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aloko/elasticquent5 我们能提供哪些服务?
定制开发 / 二次开发

基于 aloko/elasticquent5 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 20.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 21
  • Watchers: 6
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-11-01