Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use elasticsearch in PHP

Shulou Source: shulou.com Published: 2022-06-03 05:31:33 09月29日 Update

How to apply elasticsearch to PHP, I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Environmental requirements: PHP_VERSION > = 5.3.9 dint composer tool

two。 Create a new folder on disk E named elastic, copy composer.phar to the E:/elastic directory

3. Open a command line window and enter E:/elastic

4. Run on the command line: phpcomposer.pharrequireelasticsearch/elasticsearch

5. A vendor directory will appear under the E:/elastic directory and the installation will be successful

6. How to use: require'vendor/autoload.php';$client=newElasticsearch\ Client ()

Create an index

Include ('. / vendor/autoload.php'); $elastic=newElasticsearch\ Client (); $index ['index'] =' log';// index name $index ['type'] =' ems_run_log';// type name $data ['body'] [' settings'] ['number_of_shards'] = 5 data / number of main shards $data [' body'] ['settings'] [' number_of_replicas'] = 0 Landscape / number of slave shards $elastic- > indices ()-> create ($index)

Insert index data

Include ('. / vendor/autoload.php'); $elastic=newElasticsearch\ Client (); $index ['index'] =' log';// index name $index ['type'] =' ems_run_log';// type name $index ['id'] = 1 id / no id, the system will automatically generate a unique id$index [' body'] = array ('mac'= >' fcd5d900beca','customer_id'= > 3 theatrical products = > 5 cinematic products = > 2); $elastic- > index ($index)

Query

Include ('. / vendor/autoload.php'); $elastic=newElasticsearch\ Client (); $index ['index'] =' log';// index name $index ['type'] =' ems_run_log';// type name $index ['body'] [' query'] ['match'] [' mac'] = 'fcd5d900beca';$index [' size'] = 10 leading index ['from'] = 200 leading index Elastic-> search ($index)

Delete document

Include ('. / vendor/autoload.php'); $elastic=newElasticsearch\ Client (); $index ['index'] =' test';// index name $index ['type'] =' ems_test';// type name $index ['id'] = 2 ems_test';// Elastic-> delete ($index)

After reading the above, have you mastered how elasticsearch is used in PHP? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Name index type method directory content command quantity more problem success helpless for this use method reason for this tool skill copy data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info macOS Redmi MariaDB