Use of PHP Elasticsearch

Source: Internet
Author: User
Tags vars elastic search

The system is Windows Server 2003.

Elasticsearch is a stable, distributed, restful search engine based on Lucene. In fact, the so-called restful is that it provides a URL for you to call (indexing and retrieval), but it is too brutal to use it directly. Therefore, it also provides a series of client packages, which are equivalent to encapsulating the curl request, the languages supported by the client package include Java, PHP, Python, Ruby and Perl, and so on.

The PHP version of the client package is called elasticsearch-php and can be downloaded from the Git_hub. The address is as follows: Https://github.com/elasticsearch/elasticsearch


There are three requirements to use elasticsearch-php:

The 1.PHP version is at 5.3.9 or more, I'm using PHP5.3.23.

2. Use Composor in the project to manage the package as follows: https://getcomposer.org/

3. Turn on curl and OpenSSL in php.ini

To use Elasticsearch, you need a JDK version greater than 6, preferably 8, because 7 has a vulnerability ....

Cut a picture of the package you need:

Start Elasticsearch very simple, directly into the decompression directory, run Elasticsearch.bat on it, see the last console output start, it started successfully.

Next, we'll show you how to use elasticsearch-php:

1. Create a new folder named Test, which is the project folder

2. Put a file named Composer.json in it, the contents of the file are:

[HTML]View Plaincopy
    1. {
    2. "Require": {
    3. "Elasticsearch/elasticsearch": "~1.2"
    4. }
    5. }

3. Copy Composer.phar to test folder, CD to test folder, enter command: PHP composer.phar install--no-dev wait for installation to succeed

This time the test folder should appear under the Vendor folder, there are Elasticsearch, composer, guzzle and other folders, a lot of content

4. At this point, you can use Elasticsearch to index and retrieve the

[PHP]View Plaincopy
  1. <?php
  2. Require_once (' vendor/autoload.php ');
  3. function Get_conn () {
  4. $host = ' IP ';
  5. $dbname = ' dbname ';
  6. $user = ' user ';
  7. $passwd = ' passwd ';
  8. $conn = new PDO ("pgsql:dbname= $dbname; host= $host",$user,$passwd);
  9. return $conn;
  10. }
  11. function Create_index () {
  12. //elastic Search PHP client
  13. $client = new Elasticsearch\client ();
  14. $sql = "SELECT * from Log";
  15. $conn = Get_conn ();
  16. $stmt = $conn->query ($sql);
  17. $rtn = $stmt->fetchall ();
  18. //delete index which already created
  19. $params = Array ();
  20. $params [' index '] = ' Log_index ';
  21. $client->indices (),Delete ($params);
  22. //create index on LOG_DATE,SRC_IP,DEST_IP
  23. $rtnCount = count ($rtn);
  24. For ($i =0; $i <$rtnCount; $i + +) {
  25. $params = Array ();
  26. $params [' body '] = Array (
  27. ' log_date ' = $rtn [$i] [' log_date '],
  28. ' src_ip ' = $rtn [$i] [' src_ip '],
  29. ' dest_ip ' = $rtn [$i] [' dest_ip ']
  30. );
  31. $params [' index '] = ' Log_index ';
  32. $params [' type '] = ' Log_type ';
  33. //document 'll be indexed to log_index/log_type/autogenerate_id
  34. $client->index ($params);
  35. }
  36. echo ' create index done! ';
  37. }
  38. function Search () {
  39. //elastic Search PHP client
  40. $client = new Elasticsearch\client ();
  41. $params = Array ();
  42. $params [' index '] = ' Log_index ';
  43. $params [' type '] = ' Log_type ';
  44. $params [' body '] [' query '] [' match '] [' src_ip '] = ' 1.122.33.141 ';
  45. $rtn = $client->search ($params);
  46. Var_dump ($rtn);
  47. }
  48. Set_time_limit (0);
  49. Create_index ();
  50. Search ();
  51. ?>

Successful indexing, you can see "CREATE INDEX done!"

The query succeeds and you can see the returned array of results.

Use of PHP Elasticsearch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.