ahmedkhan847/mysqlwithelasticsearch
Composer 安装命令:
composer require ahmedkhan847/mysqlwithelasticsearch
包简介
A small package to connect MySQL with Elasticsearch
关键字:
README 文档
README
A small library to connect MySQL with Elasticsearch. Use it to sync data and do full text search.
Click here to find the API documentation for v2
Downloading the latest release
Clone the library
git clone -b release2 https://github.com/ahmedkhan847/mysqlwithelasticsearch
Now, run composer install to install the required dependencies.
Or use composer to install complete package.
composer require ahmedkhan847/mysqlwithelasticsearch:2.*
What's in release2?
In release2 package is fully redesign. Now you don't need to pass $config file to constructor. You can set index, type, sql query, sql connection dyamically. Even now you can create your own function for searching in Elasticsearch. Let's see how you can achieve the following:
- Mapping in Elasticsearch
- Indexing All MySQL data in Elasticsearch
- Indexing All MySQL data in Elasticsearch using MySqli Connection
- Indexing Single Data in Elasticsearch
- Updating in Elasticsearch
- Deleting in Elasticsearch
- Searching in Elasticsearch
- Creating your own search class for Elasticsearch
Mapping in Elasticsearch
<?php require "vendor/autoload.php"; use ElasticSearchClient\Mapping; $mapping = new Mapping(); $map = ['index' => 'blog', 'body' => [ 'mappings' => [ 'article' => [ 'properties' => [ 'id' => [ 'type' => 'integer' ], 'article_name' => [ 'type' => 'string' ], 'article_content' => [ 'type' => 'string' ], 'article_url' => [ 'type' => 'string' ], 'category_name' => [ 'type' => 'string' ], 'username' => [ 'type' => 'string' ], 'date' => [ 'type' => 'date', 'format' => 'dd-MM-yyyy' ], 'article_img' => [ 'type' => 'string' ], ] ] ] ] ]; $mapping->createMapping($map);
Indexing All MySQL data in Elasticsearch
<?php require "vendor/autoload.php"; include "config.php"; use SyncMySql\SyncMySql; $elastic = new SyncMySql(); $connection = new \PDO('mysql:host=localhost;dbname=laravel;', 'root', ''); $sync->setIndex("blog"); $sync->setType("users"); //Where 1st param is the database connection and 2nd param is tableName $sync->insertAllData($connection, "users"); echo '<pre>'; print_r($result); echo '</pre>';
By default it is using "SELECT * FROM tablename", use 'id' as a default id column for table and elasticsearch. It is using PDO connection to fetch the data by default. If you want elasticsearch to fetch data using mysqli connection you can also use that you just need to set the connection to SyncMySql\Connection\MySQLiConnection or write your own by implementing SyncMySql\Connection. Also you can change the select query but don't forget to define an id column in it. Let's see how you can do it.
Indexing All MySQL data in Elasticsearch using MySqli Connection
<?php require "vendor/autoload.php"; use SyncMySql\SyncMySql; use SyncMySql\Connection\MySQLiConnection; $sync = new SyncMySql(); $connection = new \mysqli('localhost', 'root', '', 'laravel'); $sync->setIndex("blog"); $sync->setType("article"); $sync->setConnection(new MySQLiConnection()); $sync->setSqlQuery("SELECT id,title,body FROM posts"); //Now you don't need to pass the tablename' $sync->insertAllData($connection); echo '<pre>'; print_r($result); echo '</pre>';
Indexing Single Data in Elasticsearch
<?php require "vendor/autoload.php"; use SyncMySql\SyncMySql; use SyncMySql\Connection\MySQLiConnection; $sync = new SyncMySql(); $connection = new \mysqli('localhost', 'root', '', 'laravel'); $sync->setIndex("blog"); $sync->setType("article"); $sync->setConnection(new MySQLiConnection()); $result = $sync->insertNode($connection,21,"users"); echo '<pre>'; print_r($result); echo '</pre>';
If you have want to define you own query then:
<?php require "vendor/autoload.php"; use SyncMySql\SyncMySql; $sync = new SyncMySql(); $connection = new \PDO('mysql:host=localhost;dbname=laravel;', 'root', ''); $sync->setIndex("blog"); $sync->setType("article"); $sync->setSqlQuery("SELECT id,title,body FROM posts"); //Now you don't need to pass the tablename' $result = $sync->insertNode($connection,21); echo '<pre>'; print_r($result); echo '</pre>';
Updating in Elasticsearch
<?php require "vendor/autoload.php"; use SyncMySql\SyncMySql; $sync = new SyncMySql(); $connection = new \PDO('mysql:host=localhost;dbname=laravel;', 'root', ''); $sync->setIndex("blog"); $sync->setType("article"); $result = $sync->updateNode($connection,21,"users"); echo '<pre>'; print_r($result); echo '</pre>';
Using same technique as we do for insert you can add your own select query using setSqlQuery().
Deleting data from Elasticsearch
<?php require "vendor/autoload.php"; use SyncMySql\SyncMySql; $sync = new SyncMySql(); $sync->setIndex("blog"); $sync->setType("article"); $result = $sync->deleteNode(21); echo '<pre>'; print_r($result); echo '</pre>';
Searching in Elasticsearch
<?php require "vendor/autoload.php"; use SearchElastic\Search; $search = new Search(); $search->setIndex("blog"); $search->setType("user"); $search->setSearchColumn("name"); $result = $search->search("ahmed khan"); echo '<pre>'; print_r($result); echo '</pre>';
Creating your own search class for Elasticsearch
In order to write your own search you should extends it from SearchAbstract class and complete the public function search($query) in it.
<?php namespace SearchElastic; //Extends your class from SearchAbstract use SearchElastic\SearchAbstract\SearchAbstract; class CustomPostSearch extends SearchAbstract { /** * Write your own search method * * @param string $query * @return Result from elasticsearch */ public function search($query) { $this->validate($query); //get the elasticsearch client from the base class $client = $this->client->getClient(); $result = array(); /* Write your own query*/ $params = [ //you can add your index directly here or use this function if you are planning to set it on runtime $search->setIndex("blog") and then use $this->client->getIndex() to get index 'index' => "blog", //you can add your type directly here or use this function if you are planning to set it on runtime using $search->setType("post") and then use $this->client->getIndex() 'type' => "post", 'body' => [ 'query' => [ 'match' => [ "post" => $query], ], ], ]; $query = $client->search($params); // you can use the base method to extract result from the search or return the result directly return $this->extractResult($query); } }
If you want contribute, fork master branch.
ahmedkhan847/mysqlwithelasticsearch 适用场景与选型建议
ahmedkhan847/mysqlwithelasticsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.08k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2017 年 01 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mysql with elasticsearch」 「search in mysql with elasticsearch」 「connect mysql with elasticsearch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ahmedkhan847/mysqlwithelasticsearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ahmedkhan847/mysqlwithelasticsearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ahmedkhan847/mysqlwithelasticsearch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Ajax Box widget for Yii 2 Framework.
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
simple api library.
C3JS By phpanonymous (Mahmoud Ibrahim)
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 2.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 40
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-24