ahmedkhan847 / mysqlwithelasticsearch
一个用于连接MySQL和Elasticsearch的小型包
Requires
This package is auto-updated.
Last update: 2024-09-27 21:53:23 UTC
README
一个小型库,用于连接MySQL与Elasticsearch。可用于同步数据以及全文搜索。
点击此处查找v2版本的API文档
下载最新版本
克隆库
git clone -b release2 https://github.com/ahmedkhan847/mysqlwithelasticsearch
现在,运行composer install
来安装所需的依赖。
或使用composer安装完整包。
composer require ahmedkhan847/mysqlwithelasticsearch:2.*
release2中包含什么?
release2包已完全重设计。现在您不需要将$config文件传递给构造函数。您可以动态设置索引、类型、SQL查询、SQL连接。现在您甚至可以创建自己的函数以在Elasticsearch中进行搜索。让我们看看您如何实现以下功能:
- Elasticsearch中的映射
- 将所有MySQL数据索引到Elasticsearch中
- 使用MySqli连接将所有MySQL数据索引到Elasticsearch中
- 将单个数据索引到Elasticsearch中
- 在Elasticsearch中进行更新
- 在Elasticsearch中进行删除
- 在Elasticsearch中进行搜索
- 为Elasticsearch创建自己的搜索类
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);
将所有MySQL数据索引到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>';
默认情况下,它使用"SELECT * FROM tablename",将'id'作为表和Elasticsearch的默认id列。它默认使用PDO连接来获取数据。如果您想使用mysqli连接来获取Elasticsearch的数据,也可以使用该连接,只需将连接设置为SyncMySql\Connection\MySQLiConnection
或通过实现SyncMySql\Connection
来编写自己的。您还可以更改选择查询,但不要忘记在其中定义id列。让我们看看您如何做到这一点。
使用MySqli连接将所有MySQL数据索引到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()); $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>';
将单个数据索引到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>';
如果您想定义自己的查询,则
<?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>';
在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>';
使用与插入相同的技巧,您可以使用setSqlQuery()
添加自己的选择查询。
从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>';
在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>';
为Elasticsearch创建自己的搜索类
要编写自己的搜索,您应该从SearchAbstract
类扩展它,并在其中完成public function search($query)
。
<?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); } }
如果您想贡献,请从master分支进行分支。