michelmelo/laravel-search 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

michelmelo/laravel-search

Composer 安装命令:

composer require michelmelo/laravel-search

包简介

A search package for Laravel 5|6|7

README 文档

README

This package provides a unified API across a variety of different full text search services. It currently supports drivers for Elasticsearch, Algolia, and ZendSearch (good for local use).

Installation Via Composer

Add this to you composer.json file, in the require object:

"michelmelo/laravel-search": "0.0.8"

After that, run composer install to install the package.

Add the service provider to app/config/app.php, within the providers array.

'providers' => array(
	// ...
	'MichelMelo\Search\SearchServiceProvider',
)

OR

'providers' => array(
	// ...
	MichelMelo\Search\SearchServiceProvider::class,
)

Add a class alias to app/config/app.php, within the aliases array.

'aliases' => array(
	// ...
	'Search' => 'MichelMelo\Search\Facade',
)

Configuration

Publish the default config file to your application so you can make modifications.

$ php artisan vendor:publish

OR

$ php artisan vendor:publish  --provider=MichelMelo\Search\SearchServiceProvider

Dependencies

The following dependencies are needed for the listed search drivers:

  • ZendSearch: zendframework/zendsearch
  • Elasticsearch: elasticsearch/elasticsearch
  • Algolia: algolia/algoliasearch-client-php

Default Index

This package provides a convenient syntax for working with a "default" index. Edit the default_index field in the config file to change this value. If you need to work with more than one index, see Working With Multiple Indicies below.

Indexing Operations

Indexing is very easy with this package. Simply provide a unique identifier for the document and an associative array of fields to index.

The index will be created automatically if it does not exist the first time you access it.

Index A Document

Add a document to the "default" index with an id of "1".

Search::insert(1, array(
	'title' => 'My title',
	'content' => 'The quick brown fox...',
	'status' => 'published',
));

Note: id may be a string or an integer. This id is used to delete records and is also returned in search results.

Store Extra Parameters With A Document

You may store extra parameters with a document so they can be retrieved at a later point from search results. This can be useful for referencing timestamps or other record identifiers.

Search::insert(
	"post-1",
	array(
		'title' => 'My title',
		'content' => 'The quick brown fox...',
		'status' => 'published',
	),
	array(
		'created_at' => time(),
		'creator_id' => 5,
	)
);

Note: Extra parameters are not indexed but are stored in the index for future retrieval.

Delete A Document

Delete a document from the "default" index with an id of "1":

Search::delete(1);

Delete An Index

Search::deleteIndex();

Search Operations

Search For A Document

Search the "default" index for documents who's content field contains the word "fox":

$results = Search::search('content', 'fox')->get();

Search More Than One Field

$results = Search::search(array('title', 'content'), 'fox')->get();

Search All Fields

$results = Search::search(null, 'fox')->get();

Perform A Fuzzy Search

Perform a fuzzy search to find results with similar, but not exact, spelling. For example, you want to return documents containing the word "updates" by searching for the word "update":

$results = Search::search('content', 'update', array('fuzzy'=>true))->get();

Note: You may also pass a numeric value between 0 and 1 for the fuzzy parameter, where a value closer to 1 requires a higher similarity. Defaults to 0.5.

Apply A Filter To Your Query

You can apply filters to your search queries as well. Filters attempt to match the value you specify as an entire "phrase".

$results = Search::search('content', 'fox')
	->where('status', 'published')
	->get();

Note: Filters do not guarantee an exact match of the entire field value if the value contains multiple words.

Geo-Search

Some drivers support location-aware searching:

$results = Search::search('content', 'fox')
	->whereLocation(36.16781, -96.023561, 10000)
	->get();

Where the parameters are latitude, longitude, and distance (in meters).

Note: Currently, only the algolia driver supports geo-searching. Ensure each indexed record contains the location information: _geoloc => ['lat' => 1.23, 'lng' => 1.23].

Limit Your Result Set

$results = Search::search('content', 'fox')
	->where('status', 'published')
	->limit(10) // Limit 10
	->get();

$results = Search::search('content', 'fox')
	->where('status', 'published')
	->limit(10, 30) // Limit 10, offset 30
	->get();

Paginate Your Result Set

You can also paginate your result set using a Laravel paginator instance.

$paginator = Search::search('content', 'fox')->paginate(15);

Limit The Fields You Want Back From The Response

$results = Search::select('id', 'created_at')
	->search('content', 'fox')
	->get();

Chain Multiple Searches And Filters

$results = Search::select('id', 'created_at')
	->where('title', 'My title')
	->where('status', 'published')
	->search('content', 'fox')
	->search('content', 'quick')
	->limit(10)
	->get();

Note: Chained filters/searches are constructed as boolean queries where each must provide a match.

Delete All Documents That Match A Query

Search::search('content', 'fox')->delete();

Working With Multiple Indicies

If you need to work with more than one index, you may access all of the same methods mentioned above after you specify the index name.

Add a document to an index called "posts":

Search::index('posts')->insert(1, array(
	'title' => 'My title',
	'content' => 'The quick brown fox...',
	'status' => 'published',
));

Search the "posts" index for documents who's content field contains the word "fox" and who's status is "published":

$results = Search::index('posts')->search('content', 'fox')
	->where('status', 'published')
	->get();

Delete a document from the "posts" index with an id of "1":

Search::index('posts')->delete(1);

Delete the entire "posts" index:

Search::index('posts')->deleteIndex();

Advanced Query Callbacks

If you need more control over a search query you may add a callback function which will be called after all conditions have been added to the query but before the query has been executed. You can then make changes to the native query instance and return it to be executed.

$results = Search::index('posts')->select('id', 'created_at')
	->search('content', 'fox')
	->addCallback(function ($query) {
		// Make changes to $query...
		return $query;
	})
	->get();

Since each driver has it's own native $query object/array, you may only want to execute your callback for one of the drivers:

$results = Search::index('posts')->select('id', 'created_at')
	->search('content', 'fox')
	->addCallback(function ($query) {
		// Adjust pagination for an elasticsearch query array.
		$query['from'] = 0;
		$query['size'] = 20;
		return $query;
	}, 'elasticsearch')
	->get();

Note: You may also pass an array of drivers as the second parameter.

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

michelmelo/laravel-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 153 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 153
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-30