Elasticsearh是个搜索引擎,基于lucene和solr,类似个数据库,往里面导入json格式的数据,然后可以做检索。和mongo db不同,它可以全文搜索,分词。它的查询和管理是基于http的json请求。

安装十分简单,直接下载运行即可。

下载:

wget  https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.3/elasticsearch-2.3.3.tar.gz  

改一下配置,本身是只监听localhost的,调试不便,增加个IP:

vi elasticsearch-2.3.3/config/elasticsearch.yml  
...
network.host: 172.16.11.2,127.0.0.1  
http.port: 9200  
...

运行即可:

cd elasticsearch-2.3.3/bin  
./elasticsearch -Des.insecure.allow.root=true -d

注意上面,我们开放了远程端口,而且是以root身份启动的,这是基于我们的身份是devops,便于调试,如果是正式环境,就要仔细斟酌这两个参数。

我们有个oracle数据库,里面有表collections,每条记录是一个collection,有键值id,_source等字段,用select把数据取出来,转变成json格式:

定义一下从数据库中取到的数据

  • index是collections,type是collection,id指定,注意elastic中类似数据库,每条数据都得有index,type,和id:
{
  "_index" : "collections",
  "_type" : "collection",
  "_id" : "52788d06971c4128ce000026",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "count" : 1,
    "name" : "General",
    "created_at" : "2013-11-05T06:15:34.281Z",
    "updated_at" : "2013-11-05T06:15:34.758Z",
    "latest_pics" : [ "https://s3.amazonaws.com/savvy_products/52788d06971c4128ce000027_1383632134" ],
    "user" : {
      "fb_id" : "",
      "name" : "Anonymous Aficionado",
      "id" : "5270fdb4971c41802e0000f4"
    }
  }
}

注意上面的index,type,id三个字段,然后是内容source,这是从数据库中导出的实际数据。

我们定义一下数据文件:

cat collections-anon.json  
{ "index" : { "_index" : "products", "_type" : "product", "_id": "5246b43d1fb2e6529b000094" } }
{"category": "", "image_url": "https://s3.amazonaws.com/savvy_products/5246b43d1fb2e6529b000094_1380365373", "original": 1, "description": "Green Aviator", "likers": [], "country": "in", "price": "Rs.699", "hashtags": [], "image_attrs": {"width": 0, "height": 0}, "updated_at": "2013-10-24T07:11:25.136Z", "source": "http://www.jabong.com/mtv-Green-Aviator-244429.html", "featured": 0, "liker_ids": [], "comments_count": 0, "likes": 0, "urls": [], "image_s3_id": "5246b43d1fb2e6529b000094_1380365373", "collection_id": "5246b43cee910bc745000057", "mentions": [], "created_at": "2013-09-28T10:49:33.599Z", "user": {"fb_id": "", "name": "Anonymous Aficionado", "id": "523dc317ee910baeb20000ef"}}

注意上面,两行,第一行是定义了三要素。

第二行是我们数据库里导出的内容,当然,变成了json格式。

导入Elasticsearch的方式:

curl -s -XPOST http://localhost:9200/_bulk --data-binary @collections-anon.json  

用curl查一下:

curl -XGET "http://localhost:9200/collections/collection/52788d06971c4128ce000026?pretty=true"  
{
  "_index" : "collections",
  "_type" : "collection",
  "_id" : "52788d06971c4128ce000026",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "count" : 1,
    "name" : "General",
    "created_at" : "2013-11-05T06:15:34.281Z",
    "updated_at" : "2013-11-05T06:15:34.758Z",
    "latest_pics" : [ "https://s3.amazonaws.com/savvy_products/52788d06971c4128ce000027_1383632134" ],
    "user" : {
      "fb_id" : "",
      "name" : "Anonymous Aficionado",
      "id" : "5270fdb4971c41802e0000f4"
    }
  }
}

下一步安装插件,便于调试

cd elasticsearch-2.3.3/bin  
./plugin install mobz/elasticsearch-head

然后打开:http://172.16.11.2/_plugin/head/

ok,基本就安装完毕了,也能做基本的查询,下一步我们要进一步来改造它。

comments powered by Disqus