In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.