Elasticsearch 是一款基于 Lucene 构建的开源分布式检索和分析引擎,通过 RESTful API 提供近实时的索引、搜索功能。
新浪云 Elasticsearch 是基于开源的 Elasticsearch 2.1 版本搭建的一个集群,运行在新浪云的内网环境。对外开放 https API, 通过 Basic Authentication 使用。在新浪云上,您不需要管理 Elasticsearch 集群,只需要创建自己的索引就可以通过 API 索引、搜索您的数据,免去您运维 Elasticsearch 集群的苦恼。
有大量的日志文件需要分析
当前的全文检索依赖MySQL完成,且数据量比较大
anything else
第一步,进入应用总览页面,在“数据库与缓存服务”中,选择“Elasticsearch”,在Elasticsearch列表页面中,点击“创建索引”按钮
第二步,填写索引的基本信息,填写“索引名”,选择“容量”。
这样就好啦~
如果需要查看用于认证的账号和密码,点击“查看账号”,输入安全密码之后,就可以清晰的看到您的账号和密码。
推荐使用composer管理依赖,如何使用composer请参考 https://getcomposer.org/
{ "require": { "elasticsearch/elasticsearch": "1.1" } }
注意:必须使用1.1版本,1.0版本的https有问题
{ "require":{"elasticsearch/elasticsearch":"~2.0"} }
如果你不想安装composer客户端,我也准备了本地composer install之后的包,直接下载就能直接运行了:
PHP5.3: http://opensource.changes.com.cn/sinacloud_es_demo/Elasticsearch5.3.zip
PHP5.6: http://opensource.changes.com.cn/sinacloud_es_demo/Elasticsearch5.6.zip
一切就绪,需要写一个php脚本将文档写入了,写入脚本如下(以PHP5.6版本为例):
<?php // 增加文档 require 'vendor/autoload.php'; $config = require('config.php'); $hosts = ['https://'.$config['ak'].':'.$config['sk'].'@es.sinacloud.com:443']; $client = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build(); $index = $config['index']; $type = 'yuantan_news'; $title = $_REQUEST['title']; $time = $_REQUEST['timeline']; $content = $_REQUEST['content']; $id = $_REQUEST['id']; if (!$type || !$title || !$content || !$id) { die('params error'); } $params = [ 'index' => $index, 'type' => $type, 'id' => $id, 'body' => ['title' => $title, 'content' => $content, 'timeline' => $time] ]; $response = $client->index($params); var_dump($response);
实例写入一个新闻的实际样例,写入的字段有标题、发布时间、正文内容、还有新闻本身的ID(这个是关系型数据库中的ID值,用于关联)。实际体检一条文档:
调用curl添加一个测试文档:
curl 'http://essearch.applinzi.com/add.php' -d 'title=源潭镇的公交通车啦&content=这是测试正文&timline=2016-09-05 12:00:00&id=9999'
可以看到返回结果为:
array(6) { ["_index"]=> string(17) "ak1m33w4jmo2_lazy" ["_type"]=> string(12) "yuantan_news" ["_id"]=> string(4) "9999" ["_version"]=> int(1) ["_shards"]=> array(3) { ["total"]=> int(2) ["successful"]=> int(2) ["failed"]=> int(0) } ["created"]=> bool(true) }
已经添加完了文档了,接下来就到了查找文档的部分了,我们以公交这个关键字来查找所有在文章的标题中出现的文档,示例代码如下:
<?php // 查找文档 require 'vendor/autoload.php'; $config = require('config.php'); $hosts = ['https://'$config['ak'].':'.$config['sk'].'@es.sinacloud.com:443']; $client = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build(); $index = $config['index']; $type = 'yuantan_news'; $params = [ 'index' => $index, 'type' => $type, 'body' => [ 'query' => [ 'match_phrase' => ['title' => '公交'], ] ], 'size' => 10, 'from' => 0, ]; $response = $client->search($params); var_dump($response);
直接访问这个页面http://essearch.applinzi.com/,就看到查找的结果了:
[1]=> array(5) { ["_index"]=> string(17) "ak1m33w4jmo2_lazy" ["_type"]=> string(12) "yuantan_news" ["_id"]=> string(4) "9999" ["_score"]=> float(2.922714) ["_source"]=> array(3) { ["title"]=> string(27) "源潭镇的公交通车啦" ["content"]=> string(18) "这是测试正文" ["timeline"]=> NULL } }
这里还有其他的文档,是之前添加的,我们在这里忽略了。
潮人必备,时尚迷你型无线路由器