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
- {
- "Require": {
- "Elasticsearch/elasticsearch": "~1.2"
- }
- }
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
- <?php
- Require_once (' vendor/autoload.php ');
- function Get_conn () {
- $host = ' IP ';
- $dbname = ' dbname ';
- $user = ' user ';
- $passwd = ' passwd ';
- $conn = new PDO ("pgsql:dbname= $dbname; host= $host",$user,$passwd);
- return $conn;
- }
- function Create_index () {
- //elastic Search PHP client
- $client = new Elasticsearch\client ();
- $sql = "SELECT * from Log";
- $conn = Get_conn ();
- $stmt = $conn->query ($sql);
- $rtn = $stmt->fetchall ();
- //delete index which already created
- $params = Array ();
- $params [' index '] = ' Log_index ';
- $client->indices (),Delete ($params);
- //create index on LOG_DATE,SRC_IP,DEST_IP
- $rtnCount = count ($rtn);
- For ($i =0; $i <$rtnCount; $i + +) {
- $params = Array ();
- $params [' body '] = Array (
- ' log_date ' = $rtn [$i] [' log_date '],
- ' src_ip ' = $rtn [$i] [' src_ip '],
- ' dest_ip ' = $rtn [$i] [' dest_ip ']
- );
- $params [' index '] = ' Log_index ';
- $params [' type '] = ' Log_type ';
- //document 'll be indexed to log_index/log_type/autogenerate_id
- $client->index ($params);
- }
- echo ' create index done! ';
- }
- function Search () {
- //elastic Search PHP client
- $client = new Elasticsearch\client ();
- $params = Array ();
- $params [' index '] = ' Log_index ';
- $params [' type '] = ' Log_type ';
- $params [' body '] [' query '] [' match '] [' src_ip '] = ' 1.122.33.141 ';
- $rtn = $client->search ($params);
- Var_dump ($rtn);
- }
- Set_time_limit (0);
- Create_index ();
- Search ();
- ?>
Successful indexing, you can see "CREATE INDEX done!"
The query succeeds and you can see the returned array of results.
Use of PHP Elasticsearch