windows 下使用 elasticsearch 扩展 mongodb 检索能力

共有五个步骤:

1. 第一步,搭建 mongodb 单机 replicSet

  本次安装是在 mongodb 2.0.0 下进行,将原来的服务命令行从
     "X:\mongodb\bin\mongod" --service  --logpath  "X:\mongodb\log.txt" --bind_ip 127.0.0.1 --dbpath  "x:\data"
  更改为 

     "X:\mongodb\bin\mongod" --service  --logpath  "X:\mongodb\log.txt" --bind_ip x.x.x.x --dbpath  "x:\data"  --replSet rs0 --oplogSize 100

  需要注意的是绑定IP不能指定为 localhost 或 127.0.0.1 了

  重启服务后,用 mongo shell 进入:

MongoDB shell version: 2.2.1

connecting to: x.x.x.x/test

Welcome to the MongoDB shell.

> use local

switched to db local

> rs.initiate()
{
        "info2" : "no configuration explicitly specified -- making one",
        "me" : "x.x.x.x:27017",
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
>

这个步骤在原来的 2.0.0 版本下未成功,升级到 2.2.1 后进行初始化成功,重新进入 shell,提示符已变为 rs0:PRIMARY>

目前MongoDB的版本已升级到3.0

2. 第二步 安装 elasticsearch 

进入 http://www.elasticsearch.org/overview/elkdownloads/ 下载相应版本,解压缩到 x:\elasticsearch 目录下

进入 https://jdk7.java.net/download.html 下载相应 jdk,本次下载的是 jdk 7 32位版本

打开windows命令行进入 x:\elasticsearch\bin 目录,执行 service -install elasticsearch,请注意 JAVA_HOME 的路径不包含 bin 目录,安装后启动 elasticsearch 服务

3. 第三步 安装 elasticsearch 的 mongodb-river 插件

命令为:bin/plugin --install com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb/2.0.0,

完毕后重新启动 elasticsearch

4. 第四步 创建 meta 信息

需要用到 http 的 put 方法,这里用 node.js 写了一个脚本

var elasticsearch = require('elasticsearch');  // elasticsearch js client

var client = new elasticsearch.Client({

  host: 'localhost:9200',

  log: 'trace'

});

client.ping({

  requestTimeout: 1000,

  // undocumented params are appended to the query string

  hello: "elasticsearch!"

}, function (error) {

  if (error) {

    console.error('elasticsearch cluster is down!');

  } else {

    console.log('All is well');

  }

});

var http = require('http');

var options = {

  host: 'localhost',

  port: 9200,

  path: '/_river/yourdbname/_meta',

  method: 'PUT'

};

var data = { 

    "type": "mongodb", 

    "mongodb": {

      "host": "x.x.x.x",  

      "port": "27017",

        "db": "yourdbname", 

        "collection": "page"

    }, 

    "index": {

        "name": "yourdbname", 

        "type": "page" 

    }

};

var req = http.request(options, function(res) {

  console.log('STATUS: ' + res.statusCode);

  console.log('HEADERS: ' + JSON.stringify(res.headers));

  res.setEncoding('utf8');

  res.on('data', function (chunk) {

    console.log('BODY: ' + chunk);

  });

});

req.on('error', function(e) {

  console.log('problem with request: ' + e.message);

});

// write data to request body

req.write(JSON.stringify(data));

 

req.end();

从返回的body可以看到 {"_index":"_river","_type":"yourdbname","_id":"_meta","_version":1,"created":true}

执行完后将创建 yourdbname.page 的索引,这里的mongodb-river插件已经能自动从以前的数据开始检索了,少了更新 oplog 的步骤。

5. 第五步 安装 elasticsearch 的管理工具

测试了 https://github.com/mobz/elasticsearch-head,可用

官方的 kibana 也很不错,解压到 web 目录下零配置可用

 

简单查询:http://localhost:9200/DBNAME/COLLECTION/_search?q=中文

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值