php curl 调用 elasticsearch 怎么传参

php curl 调用 elasticsearch 怎么传参,第1张

ElasticSearch是一个基于Lucene的稳定的、分布式、RESTFul的搜索引擎。其实所谓的RestFul就是它提供URL供你调用(建立索引和进行检索),不过直接这样使用实在是太凶残了。所以,它也提供了一系列client包,相当于将curl请求封装了,client包支持的语言包括Java、PHP、Python、Ruby和Perl等等。

PHP版的client包叫做elasticsearch-php,可以在Git_hub上下载。

要使用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的文件,文件内容为:

[html] view plaincopy

{

"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] view plaincopy

<?php

require_once('vendor/autoload.php')

function get_conn(){

$host = 'ip'

$dbname = 'dbname'

$user = 'user'

$passwd = 'passwd'

$conn = new PDO("pgsql:dbname=$dbnamehost=$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 will 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()

?>

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

查询成功,可以看到返回的结果数组。

我Google了下,大致给出的答案如下:

1、使用bulk API

2、初次索引的时候,把 replica 设置为 0

3、增大 threadpool.index.queue_size

4、增大 indices.memory.index_buffer_size

5、增大 index.translog.flush_threshold_ops

6、增大 index.translog.sync_interval

7、增大 index.engine.robin.refresh_interval

其中 5,6 属于 TransLog 相关。

4 则和Lucene相关

3 则因为ES里大量采用线程池,构建索引的时候,是有单独的线程池做处理的

7 的话个人认为影响不大

2 的话,能够使用上的场景有限。个人认为Replica这块可以使用Kafka的ISR机制。所有数据还是都从Primary写和读。Replica尽量只作为备份数据。

不过我希望大家知其然,并且根据原理,可以根据实际业务场景,做出相应的改动,而不仅仅是会配置上面几个参数。

可能匹配到跨时间的情况,正则再改一下:

(\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\][^[]*?cURL[^[]*?)(?=\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\])


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11725749.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存