madewithlove/elasticsearcher 问题修复 & 功能扩展

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

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

madewithlove/elasticsearcher

Composer 安装命令:

composer require madewithlove/elasticsearcher

包简介

Wrapper on top of the ElasticSearch PHP SDK which allows easier index/document/query management.

README 文档

README

checks

This agnostic package is a lightweight wrapper on top of the Elasticsearch PHP client. Its main goal is to allow for easier structuring of queries and indices in your application. It does not want to hide or replace functionality of the Elasticsearch PHP client.

Installation

Installation of the latest version is easy via composer:

composer require madewithlove/elasticsearcher

Versions

Elasticsearch Elasticsearcher
>= 7.0 >= 0.7
>= 5.0 >= 0.5
>= 2.0 >= 0.4
>= 1.0, < 2.0 0.3

Features

Query class

Structure queries inside a class for clearer oversight in your application.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		// Full notation
		$body = [
			'query' => [
				'bool' => [
					'filter' => [
						'term' => ['year' => 2014]
					]
				]
			]
		];
		$this->setBody($body);

		// Short (dotted) notation
		$this->set('query.bool.filter.term.year', 2014);

		$this->paginate(2, 20);
		$this->sortBy('name', 'asc');
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$query->run();

Query with custom/re-usable fragments

Move re-occuring or complex fragments of your query or index to a separate class.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');

		$this->set('query.bool.filter', [new YearFilter(2014)]);
	}
}

Query with custom result parsing

Perform actions on the response from Elasticsearch before the Query returns the results. It can be used for converting the Elasticsearch documents into models/entities from your ORM. Re-use it in multiple queries.

class MoviesFrom2014Query extends AbstractQuery
{
	public function setup()
	{
		$this->searchIn('movies', 'movies');
		$this->parseResultsWith(new MoviesResultParser());

		$body = array(...);

		$this->setBody($body);
	}
}

// Usage
$query = new MoviesFrom2014Query($this->getElasticSearcher());
$result = $query->run();
foreach ($result->getResults() as $movie) {
	var_dump($movie->title, $movie->id, $movie->year);
}

Indices management

$searcher->indicesManager()->exists('listings');
$searcher->indicesManager()->create('suggestions');
$searcher->indicesManager()->update('suggestions');
$searcher->indicesManager()->delete('suggestions');

Documents management

$manager->index('suggestions', $data);
$manager->bulkIndex('suggestions', [$data, $data, $data]);
$manager->update('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->updateOrIndex('suggestions', 123, ['name' => 'Fight Club 2014']);
$manager->delete('suggestions', 123);
$manager->exists('suggestions', 123);
$manager->get('suggestions', 123);

Cluster Health

Sometimes when you're re-indexing your ES data, you might have some issues between your index recreation and indexing your data. That's because ES can take a bit longer to recreate your indexes, causing your reindex task to fail - we are talking about microseconds here. You can find some references here and here.

In order to avoid this, we built this helper in the ElasticSearcher class to check the cluster health. You can use like this:

<?php
/** @var \ElasticSearcher\ElasticSearcher $searcher */
while (!$searcher->isHealthy()) { sleep(1); }

Place this in your reindex command between your index creation and the data indexing.

Attention: If you're running a single ES node, you'll need to configure your elasticsearch number_of_replicas setting to 0. However, we highly recommend you to use at least 2 nodes.

Access to Elasticsearch client

The package does not and will not try to implement everything from the Elasticsearch client. Access to the client is always possible.

$client = $searcher->getClient();

Usage

use ElasticSearcher\Environment;
use ElasticSearcher\ElasticSearcher;

$env = new Environment(
  ['hosts' => ['localhost:9200']]
);
$searcher = new ElasticSearcher($env);

More usage in the examples and documentation.

Documentation

madewithlove/elasticsearcher 适用场景与选型建议

madewithlove/elasticsearcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134.45k 次下载、GitHub Stars 达 264, 最近一次更新时间为 2014 年 10 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 134.45k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 265
  • 点击次数: 27
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 264
  • Watchers: 24
  • Forks: 29
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-24