Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use ElasticSearch to realize search in PHP

Shulou Source: shulou.com Published: 2022-05-31 20:53:25 10月04日 Update

Most people do not understand the knowledge points of this article "how to use ElasticSearch to search in PHP", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use ElasticSearch to search in PHP" article.

Environment

Php 7.2

Elasticsearch 6.2 download

Elasticsearch-php 6 download

Install elasticsearch

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

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 the PHP extension

I am using 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 here for testing, first, to build the table, then to write the test data, and then to edit the test case after the preparation is done.

Create table articles (id int not null primary key auto_increment, title varchar) not null comment 'title', content text comment 'content'); insert into articles (title, content) values ('Laravel test 1,' Laravel 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 ()

Noun interpretation: an index is equivalent to a table in MySQL, and a document is equivalent to a row record in MySQL

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

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

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

Delete a document from the 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 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) The above is about the content of this article on "how to use ElasticSearch to search in PHP". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Test content index article document search data user knowledge article preparation for this value example dynamic noun noun interpretation most examples is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno Shulou Tech Info Linux Huawei