cover_image

Elasticsearch轻松入门

新浪云计算 2016年09月06日 11:39

Elasticsearch简介


Elasticsearch 是一款基于 Lucene 构建的开源分布式检索和分析引擎,通过 RESTful API 提供近实时的索引、搜索功能。

新浪云 Elasticsearch 是基于开源的 Elasticsearch 2.1 版本搭建的一个集群,运行在新浪云的内网环境。对外开放 https API, 通过 Basic Authentication 使用。在新浪云上,您不需要管理 Elasticsearch 集群,只需要创建自己的索引就可以通过 API 索引、搜索您的数据,免去您运维 Elasticsearch 集群的苦恼。



1
适合的业务场景
  • 有大量的日志文件需要分析

  • 当前的全文检索依赖MySQL完成,且数据量比较大

  • anything else


2
从管理平台创建索引


第一步,进入应用总览页面,在“数据库与缓存服务”中,选择“Elasticsearch”,在Elasticsearch列表页面中,点击“创建索引”按钮


图片


第二步,填写索引的基本信息,填写“索引名”,选择“容量”。


图片


这样就好啦~


如果需要查看用于认证的账号和密码,点击“查看账号”,输入安全密码之后,就可以清晰的看到您的账号和密码。


图片


3
下载Elasticsearch PHP SDK


推荐使用composer管理依赖,如何使用composer请参考 https://getcomposer.org/


PHP5.3 composer.json


{
        "require": {
            "elasticsearch/elasticsearch": "1.1"
        }   
} 


注意:必须使用1.1版本,1.0版本的https有问题


PHP5.6 composer.json


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


如果你不想安装composer客户端,我也准备了本地composer install之后的包,直接下载就能直接运行了:



4
写入文档


一切就绪,需要写一个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)
}


5
查找文档


已经添加完了文档了,接下来就到了查找文档的部分了,我们以公交这个关键字来查找所有在文章的标题中出现的文档,示例代码如下:



<?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
        }
      }


这里还有其他的文档,是之前添加的,我们在这里忽略了。


图片


图片

潮人必备,时尚迷你型无线路由器



图片

继续滑动看下一个
新浪云计算
向上滑动看下一个