承接 nqxcode/laravel-lucene-search 相关项目开发

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

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

nqxcode/laravel-lucene-search

Composer 安装命令:

composer require nqxcode/laravel-lucene-search

包简介

Laravel 5.5 package for full-text search over Eloquent models based on ZendSearch Lucene.

README 文档

README

Latest Stable Version Latest Unstable Version License Build Status Coverage Status

Laravel 5.5 package for full-text search over Eloquent models based on ZendSearch Lucene.

Installation

Require this package in your composer.json and run composer update:

{
	"require": {
        "nqxcode/laravel-lucene-search": "2.4.*"
	}
}

After updating composer, add the ServiceProvider to the providers array in config/app.php

'providers' => [
	Nqxcode\LuceneSearch\ServiceProvider::class,
],

If you want to use the facade to search, add this to your facades in config/app.php:

'aliases' => [
	'Search' => Nqxcode\LuceneSearch\Facade::class,
],

Configuration

Publish the config file into your project by running:

php artisan vendor:publish --provider="Nqxcode\LuceneSearch\ServiceProvider"

Basic

In published config file add descriptions for models which need to be indexed, for example:

'index' => [

	// ...

	namespace\FirstModel::class => [
		'fields' => [
			'name', 'full_description', // fields for indexing
		]
	],

	namespace\SecondModel::class => [
		'fields' => [
			'name', 'short_description', // fields for indexing
		]
	],

	namespace\ModelWithCustomPrimaryKey::class => [
		// You can also define your primary key (if you use something else than "id")
		'primary_key' => 'my_custom_field_name',
		'fields' => [
			'username', 'short_description', // fields for indexing
		]
	],

	// ...

],

Indexing of dynamic fields

You can also index values of optional fields (dynamic fields). For enable indexing for optional fields:

  • In config for each necessary model add following option:
        'optional_attributes' => true

        // or

        'optional_attributes' => [
                'accessor' => 'custom_name' // with specifying of accessor name
        ]
  • In model add special accessor, that returns list of field-name => field-value. By default getOptionalAttributesAttribute accessor will be used. In case accessor name specified in config getCustomNameAttribute accessor will be used.

Example:

In config file:

        namespace\FirstModel::class => [
                'fields' => [
                    'name', 'full_description', // fixed fields for indexing
                ],

                'optional_attributes' => true //  enable indexing for dynamic fields
        ],

In model add following accessor:

        public function getOptionalAttributesAttribute()
        {
                return [
                        'optional_attribute1' => 'value1',
                        'optional_attribute2' => 'value2',
                ];
        }

Score Boosting

See details on Apache Lucene - Scoring.

Model level boosting

This is Document level boosting in terminology of Apache Lucene. By default all models have boost value equal to 1. For change of this behavior customize boost for necessary models as in the following examples.

  • In config for each necessary model add following option:
        'boost' => true

        // or

        'boost' => [
                'accessor' => 'custom_name' // with specifying of accessor name
        ]

In model add following accessor:

        public function getBoostAttribute()
        {
                return 0.5; // customize boost value for model
        }
  • In model add special accessor, that returns boost value. By default getBoostAttribute accessor will be used. In case accessor name specified in config getCustomNameAttribute accessor will be used.

Example:

In config file:

        namespace\FirstModel::class => [
                'fields' => [
                    'name', 'full_description',
                ],

                'boost' => true // enable boosting for model
        ],

In model add following accessor:

        public function getBoostAttribute()
        {
                return 0.5; // customize boost value for model
        }

Model's field level boosting

This is Document's Field level boosting in terminology of Apache Lucene. By default boost is set in 1 for each field. For change of this behavior set boost for necessary fields as in the following examples.

In config file:

        namespace\FirstModel::class => [
                'fields' => [
                    'name', // field with default boost
                    'full_description' => ['boost' => 0.2], // customize boost value
                ],
        ],

Or/and in model accessor:

        public function getOptionalAttributesAttribute()
        {
                return [
                        'optional_attribute1' => 'value1', // field with default boost
                        'optional_attribute2' => ['boost' => 0.5, 'value' => 'value2'], // customize boost value
                ];
        }

Stemming and stopwords

By default the following filters are used in search:

  • Stemming filter for english/russian words (for reducing words to their root form),
  • Stopword filters for english/russian words (for exclude some words from search index).

This filters can be deleted or replaced with others.

'analyzer' => [
    'filters' => [
    	// Default stemming filter.
    	Nqxcode\Stemming\TokenFilterEnRu::class,
    ],

    // List of paths to files with stopwords.
    'stopwords' => Nqxcode\LuceneSearch\Analyzer\Stopwords\Files::get(),
],

Usage

Artisan commands

Initialize or rebuild search index

For building of search index run:

php artisan search:rebuild --verbose

Clear search index

For clearing of search index run:

php artisan search:clear

Filtering of models in search results

For filtering of models in search results each model's class can implements SearchableInterface. For example:

use Illuminate\Database\Eloquent\Model;
use Nqxcode\LuceneSearch\Model\SearchableInterface;

class Dummy extends Model implements SearchableInterface
{
        // ...

        /**
         * Get id list for all searchable models.
         */
        public static function searchableIds()
        {
            return self::wherePublish(true)->pluck('id');
        }

        // ...
}

Partial updating of search index

For register of necessary events (save/update/delete) use Nqxcode\LuceneSearch\Model\SearchTrait in target model:

    use Illuminate\Database\Eloquent\Model;
    use Nqxcode\LuceneSearch\Model\SearchableInterface;
    use Nqxcode\LuceneSearch\Model\SearchTrait;

    class Dummy extends Model implements SearchableInterface
    {
        use SearchTrait;

        // ...
    }

Perform operations without indexing

If you want to avoid triggering the indexing, wrap necessary operations in the withoutSyncingToSearch() method on your model:

Product::withoutSyncingToSearch(function () {
    // mass update position for product, e.g.
    foreach (Product::all() as $i => $product) {
        $product->update(['position' => $i)]);
    }    
});

Query building

Build query in several ways:

Using constructor:

By default, queries which will execute search in the phrase entirely are created.

Simple queries
$query = Search::query('clock'); // search by all fields.
// or
$query = Search::where('name', 'clock'); // search by 'name' field.
// or
$query = Search::query('clock')              // search by all fields with
	->where('short_description', 'analog'); // filter by 'short_description' field.
// or
$query = Product::search('clock'); // search only in `Product` model by all fields in case when `Product` use `SearchableTrait`.
Advanced queries

For query and where methods it is possible to set the following options:

  • phrase - phrase match (boolean, true by default)
  • proximity - value of distance between words (unsigned integer)
  • fuzzy - value of fuzzy (float, 0 ... 1)
  • required - should match (boolean, true by default)
  • prohibited - should not match (boolean, false by default)
Examples:

Find all models in which any field contains phrase like 'composite one two phrase':

$query = Search::query('composite phrase', '*', ['proximity' => 2]);

Search by each word in query:

$query = Search::query('composite phrase', '*', ['phrase' => false]);

Using Lucene raw queries:

$query = Search::rawQuery('short_description:"analog"');
// or
$rawQuery = QueryParser::parse('short_description:"analog"');
$query = Search::rawQuery($rawQuery);

Getting of results

For built query are available following actions:

Get all found models

$models = $query->get();

Get count of results

$count = $query->count();

Get limit results with offset

$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10

Paginate the found models

$paginator = $query->paginate(50);

Highlighting of matches

Highlighting of matches is available for any html fragment encoded in utf-8 and is executed only for the last executed request.

Search::find('nearly all words must be highlighted')->get();
$highlighted = Search::highlight('all words');

// highlighted html:
// '<span class="highlight">all</span> <span class="highlight">words</span>'

License

Package licenced under the MIT license.

nqxcode/laravel-lucene-search 适用场景与选型建议

nqxcode/laravel-lucene-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.53k 次下载、GitHub Stars 达 72, 最近一次更新时间为 2014 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nqxcode/laravel-lucene-search 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 16.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 77
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 72
  • Watchers: 3
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-08-28