PHP elasticsearch to explain search examples

Time:2021-7-30

Elasticsearch is a Lucene based search server. It provides a distributed multi-user full-text search engine based on restful web interface. Elasticsearch is developed in Java and released as an open source under the Apache license terms. It is a popular enterprise search engine. Designed for cloud computing, it can achieve real-time search, stable, reliable, fast, easy to install and use.

PHP searches based on elasticsearch

I thought of elasticsearch when doing the search, and it also supports PHP, so I made a simple example to test it. I feel it’s good. Make a record.

environment

php 7.2

Elasticsearch 6.2 download

Elasticsearch PHP 6 download

Install elasticsearch

Download the source file, unzip it, re create a user, and change the group of the directory to this user, because elasticsearch cannot be started by root.

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz

tar zxvf elasticsearch-6.2.3.tar.gz

useradd elasticsearch

password elasticsearch

chown elasticsearch:elasticsearch elasticsearch-6.2.3

cd elasticsearch-6.2.3

. / bin / elasticsearch // start

Install PHP extensions

I use composer to install elasticsearch PHP here. 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

I have prepared an article table for testing. First, create the table, and then write the test data. After the preparation, I start editing the test cases.

create table articles(

 id int not null primary key auto_increment,

 Title varchar (200) not null comment 'title',

 Content text comment '

);

Insert into articles (title, content) values ('lavel test 1 ',' lavel test article content 1 '),

('laravel test 2 ',' laravel test article content 2 '),

('laravel test 3 ',' laravel test article content 3 ');

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();

}

instantiation


require './vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

Term explanation: an index is equivalent to a table in mysql, and a document is equivalent to a row record in MySQL

The dynamic nature of elastic search automatically creates the index and some default settings when adding the first document.

Add document 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 document from index


$params = [

 'index' => 'articles_index',

 'type' => 'articles_type',

 'id' => 'articles_1'

];

$res = $client->get($params);

print_r($res);

Delete document from index


$params = [

 'index' => 'articles_index',

 'type' => 'articles_type',

 'id' => 'articles_1'

];

$res = $client->delete($params);

print_r($res);

Delete index


$params = [

  'index' => 'articles_index'

];

$res = $client->indices()->delete($params);

print_r($res);

Create 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);

The above is the detailed content of php search based on elasticsearch. I hope the content sorted out by developeppaer can help you.