日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP ElasticSearch的使用

發布時間:2025/3/20 php 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP ElasticSearch的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

在Windows上安裝Elasticsearch v5.4.2

ElasticSearch 索引查詢使用指南——詳細版

ElasticSearch是一個基于Lucene的穩定的、分布式、RESTFul的搜索引擎。其實所謂的RestFul就是它提供URL供你調用(建立索引和進行檢索),不過直接這樣使用實在是太兇殘了。所以,它也提供了一系列client包,相當于將curl請求封裝了,client包支持的語言包括Java、PHP、Python、Ruby和Perl等等。

PHP版的client包叫做elasticsearch-php,可以在Git_hub上下載。地址如下:https://github.com/elasticsearch/elasticsearch

要使用elasticsearch-php有如下三個要求:

1.PHP的版本在5.3.9以上,我用的是PHP5.3.23

2.在項目中使用Composor來管理包,下載地址如下:https://getcomposer.org/

3.在php.ini中開啟curl和openssl

要使用elasticsearch,需要JDK的版本大于6,最好選擇8吧,因為7有漏洞....

截一張需要的包圖:

啟動elasticsearch很簡單,直接進入解壓目錄,運行elasticsearch.bat就可以了,看到最后console輸出start,就啟動成功了。

接下來介紹如何使用elasticsearch-php:

1.新建一個文件夾取名為test,此為項目文件夾

2.在里面放入一個命名為composer.json的文件,文件內容為:

{"require":{"elasticsearch/elasticsearch":"~1.2"}}

3.將composer.phar拷貝到test文件夾中,cd 到test文件夾,輸入命令:php composer.phar install --no-dev 等待安裝成功

這個時候test文件夾下面應該會出現vendor文件夾,里面有elasticsearch、composer、guzzle等文件夾,很多內容

4.這個時候,就可以使用elasticsearch進行建立索引和進行檢索了

<?php require_once('vendor/autoload.php');function get_conn() {$host = 'ip';$dbname = 'dbname';$user = 'user';$passwd = 'passwd';$conn = newPDO("pgsql:dbname=$dbname;host=$host", $user, $passwd);return $conn; } function create_index() {// Elasticsearchphpclient$client = new Elasticsearch/Client();$sql = "SELECT * FROM log";$conn = get_conn();$stmt = $conn -> query($sql);$rtn = $stmt -> fetchAll();// deleteindexwhichalreadycreated$params = array();$params['index'] = 'log_index';$client -> indices() -> delete($params);// createindexonlog_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';// Documentwillbeindexedtolog_index/log_type/autogenerate_id$client -> index($params);} echo'create index done!'; } function search() {// Elasticsearchphpclient$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();

建立索引成功,可以看到“create index done!”

查詢成功,可以看到返回的結果數組。

轉載于:https://my.oschina.net/mickelfeng/blog/914903

總結

以上是生活随笔為你收集整理的PHP ElasticSearch的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。