PHP elasticsearch to explain search examples


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.


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.


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=;dbname=test', 'root', 'root');

 $sql = 'select * from articles';

 $query = $db->prepare($sql);


 $lists = $query->fetchAll();


} catch (Exception $e) {

 echo $e->getMessage();



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'




Get document from index

$params = [

 'index' => 'articles_index',

 'type' => 'articles_type',

 'id' => 'articles_1'


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


Delete document from index

$params = [

 'index' => 'articles_index',

 'type' => 'articles_type',

 'id' => 'articles_1'


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


Delete index

$params = [

  'index' => 'articles_index'


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


Create index

$params['index'] = 'articles_index'; 

$params['body']['settings']['number_of_shards'] = 2; 

$params['body']['settings']['number_of_replicas'] = 0; 



$params = [ 

 'index' => 'articles_index',

 'type' => 'articles_type',


$params['body']['query']['match']['content'] = 'Laravel';

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


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