Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use ElasticSearch to realize search in PHP

Shulou Source: shulou.com Published: 2022-06-02 08:41:49 09月13日 Update

How to use ElasticSearch to achieve search in PHP, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.

Install elasticsearch

Download the source file, extract it, create a new user, change the directory group to this user, because elasticsearch cannot be started as root.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gztar zxvf elasticsearch-6.2.3.tar.gzuseradd elasticsearchpassword elasticsearchchown elasticsearch:elasticsearch elasticsearch-6.2.3cd elasticsearch-6.2.3./ bin/elasticsearch //start

Install PHP extensions

Here I use composer to install elasticsearch-php. Add "elasticsearch/elasticsearch": "~6.0" to the composer. json file and execute composer update.

{ "require": { // ... "elasticsearch/elasticsearch": "~6.0" // ... }}

test example

Create tables and test data

Here I prepared an article table to test, first of all, build the table, then write the test data, after the preparation work is completed, start editing the test cases.

create table articles( id int not null primary key auto_increase, title varchar(200) not null comment 'title', content text comment ' content');

insert into articles(title, content) values ('Laravel test1 ', 'Laravel test1 '),('Laravel test2', 'Laravel test2'),('Laravel test3 ', 'Laravel test3 ');

Read data from Mysql

try { $db = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root'); $sql = 'select * from articles'; $query = $db->prepare($sql); $query->execute(); $lists = $query->fetchAll(); print_r($lists);} catch (Exception $e) { echo $e->getMessage();}

instantiated

require './ vendor/autoload.php';use Elasticsearch\ClientBuilder;$client = ClientBuilder::create()->build();

Noun explanation: index is equivalent to MySQL table, document is equivalent to MySQL row record

The dynamic nature of elasticsearch automatically creates an index and some default settings when the first document is added.

Add documents to index

foreach ($lists as $row) { $params = [ 'body' => [ 'id' => $row['id'], 'title' => $row['title'], 'content' => $row['content'] ], 'id' => 'article_' . $row['id'], 'index' => 'articles_index', 'type' => 'articles_type' ]; $client->index($params);}

Get documents from index

$params = [ 'index' => 'articles_index', 'type' => 'articles_type', 'id' => 'articles_1'];$res = $client->get($params);print_r($res);

Remove documents from index

$params = [ 'index' => 'articles_index', 'type' => 'articles_type', 'id' => 'articles_1'];$res = $client->delete($params);print_r($res);

delete the index

$params = [ 'index' => 'articles_index'];$res = $client->indices()->delete($params);print_r($res);

create an index

$params['index'] = 'articles_index'; $params['body']['settings']['number_of_shards'] = 2; $params['body']['settings']['number_of_replicas'] = 0; $client->indices()->create($params);

search

$params = [ 'index' => 'articles_index', 'type' => 'articles_type',];

$params['body'][' query']['match'][' content']= 'Laravel';$res = $client->search($params);print_r($res); Does it help you to read all of the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Tags: Test index content document article data user search prepare help clear for this example dynamic noun noun interpretation example for this nature belonging Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker Shulou Technology MariaDB Shulou Information