ES搜索引擎入门笔记

一:安装
参考文档:http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
Elastic 需要 Java 8 环境。如果你的机器还没安装 Java,可以参考这篇文章,注意要保证环境变量JAVA_HOME正确设置。

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5.1.zip
$ cd elasticsearch-5.5.1/

进入解压后的目录,运行下面的命令,启动 Elastic。

$ ./bin/elasticsearch 【./bin/elasticsearch -d 是后台开启】
ps -ef | grep elsearch 查看是哪个端口
关闭: kill -9 端口号

如果这时报错"max virtual memory areas vm.maxmapcount [65530] is too low",要运行下面的命令。

$ sudo sysctl -w vm.max_map_count=262144

如果一切正常,Elastic 就会在默认的9200端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息

$ curl localhost:9200

{
“name” : “atntrTf”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “tf9250XhQ6ee4h7YI11anA”,
“version” : {
“number” : “5.5.1”,
“build_hash” : “19c13d0”,
“build_date” : “2017-07-18T20:44:24.823Z”,
“build_snapshot” : false,
“lucene_version” : “6.6.0”
},
“tagline” : “You Know, for Search”
}
默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elastic。
network.host: 0.0.0.0
上面代码中,设成0.0.0.0让任何人都可以访问。线上服务不要这样设置,要设成具体的 IP

二:PHP中引入ElasticSearch
参考文档:https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html
composer安装
在系统中的composer.json 文件中增加 elasticsearch 和 monolog 两个插件,并下载
{
“require”: {
“elasticsearch/elasticsearch”: “~6.0”,
“monolog/monolog”: “~1.0”
}
}
composer require elasticsearch/elasticsearch
composer require monolog/monolog

如果没有指定主机名,客户端会连接 localhost:9200 。利用 ClientBuilder 的 setHosts() 方法可以改变客户端的默认连接方式。 setHosts() 方法接收一个一维数组,数组里面每个值都代表集群里面的一个节点信息。值的格式多种多样,主要看你的需求:
use Elasticsearch\ClientBuilder;

$hosts = [
‘192.168.1.1:9200’, // IP + Port
‘192.168.1.2’, // Just IP
mydomain.server.com:9201’, // Domain + Port
mydomain2.server.com’, // Just Domain
https://localhost’, // SSL to localhost
https://192.168.1.3:9200’ // SSL to IP + Port
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build(); // Build the client object

注意 ClientBuilder 对象允许链式操作。当然也可以分别调用上述的方法:

$clientBuilder = ClientBuilder::create(); // Instantiate a new ClientBuilder
$clientBuilder->setHosts($hosts); // Set the hosts
$client = $clientBuilder->build(); // Build the client object
$client = ClientBuilder::create()->setHosts([‘192.168.8.107:9200’])->build();
$params = [
‘index’ => ‘my_index’,
‘type’ => ‘my_type’,
‘id’ => ‘my_id’,
‘body’ => [‘testField’ => ‘abc’]
];
$response = c l i e n t − > i n d e x ( client->index( client>index(params);
print_r($response);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值