Winse Blog

走走停停, 熙熙攘攘, 忙忙碌碌, 不知何畏.

Elasticsearch Startguide

如果有Lucene的使用经历,elasticsearch的入门还是比较简单的。直接解压启动命令就安装好了,然后就是添加一些plugins就OK了。

安装

从官网下载 TAR包 ,解压后,运行 elasticsearch 脚本启动服务。

1
2
# -d 表示 daemonize 后台运行
[hadoop@cu2 elasticsearch-2.2.0]$ bin/elasticsearch -d

插件

大部分插件都是ajax方式的静态页面,可以通过plugin脚本安装,或者直接解压文件到plugins目录下面。

安装已经下载到本地的插件需要加file协议,不然程序会从官网下载。或者直接解压到plugins目录下:

1
2
3
4
5
6
7
[hadoop@cu2 elasticsearch-2.2.0]$ bin/plugin install file:///home/hadoop/elasticsearch-head-master.zip 
-> Installing from file:/home/hadoop/elasticsearch-head-master.zip...
Trying file:/home/hadoop/elasticsearch-head-master.zip ...
Downloading .........DONE
Verifying file:/home/hadoop/elasticsearch-head-master.zip checksums if available ...
NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)
Installed head into /home/hadoop/elasticsearch-2.2.0/plugins/head

windows

1
E:\local\usr\share\elasticsearch-2.3.3\bin>plugin.bat install file:///D:/SOFTWARE/elasticsearch/elasticsearch-plugin/elasticsearch-head-master.zip

安装好plugin后,打开浏览器查看索引情况: http://localhost:9200/_plugin/head/

插件高阶

有些插件版本比较旧需要改一改,需要了解新版本的 elasticsearch-plugin 的规范:

https://www.elastic.co/guide/en/elasticsearch/plugins/2.3/installation.html

新版本插件主要是需要增加一个描述文件:

Plugins require descriptor file

遇到想安装的旧版本的plugin,描述文件写法可以参考 elasticsearch-head

可选插件:

常用URL请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
# 创建
$ curl -XPUT 'http://localhost:9200/t_ods_idc_isp_log2/' -d '{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 0
        }
    }
}'
{"acknowledged":true}

# https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
# 更新
# mapping.json
{
  "properties": {
      "author": {
          "type": "string"
      },
...
      "year": {
          "type": "long",
          "ignore_malformed": false,
          "index": "analyzed"
      },
      "avaiable": {
          "type": "boolean"
      }
  }
}
$ curl -XPUT 'localhost:9200/t_ods_idc_isp_log2/_mapping/default' -d @mapping.json

$ curl -XPUT 'localhost:9200/t_ods_idc_isp_log2/_mapping/default' -d '
{
  "properties": {
    "fDIID": {
      "type": "string"
    },
...
    "gatherTime": {
      "type": "long"
    }
  }
}
'

# https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
# 索引
# documents.json
{ "index": {      "_index": "library",        "_type": "book",        "_id": "1"  } }
{     "title": "All Quiet on the Western Front",  "otitle": "Im Westen nichts Neues",     "author": "Erich Maria Remarque",   "year": 1929,   "characters": ["Paul Baumer",   "Albert Kropp",     "Haie Westhus",     "Fredrich Muller",  "Stanislaus Katczinsky",    "Tjaden"],  "tags": ["novel"],  "copies": 1,    "available": true,  "section": 3 }
{     "index": {      "_index": "library",        "_type": "book",        "_id": "2"  } }
{     "title": "Catch-22",    "author": "Joseph Heller",  "year": 1961,   "characters": ["John Yossarian",    "Captain Aardvark",     "Chaplain Tappman",     "Colonel Cathcart",     "Doctor Daneeka"],  "tags": ["novel"],  "copies": 6,    "available": false,     "section": 1 }

$ curl -s -XPOST localhost:9200/_bulk --data-binary @documents.json

# 删除
$ curl -XDELETE 'http://localhost:9200/t_ods_idc_isp_log2/'
{"acknowledged":true}

# https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html
# https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-field-mapping.html
# 状态查看
http://localhost:9200/_cat/health?v
http://localhost:9200/_cat/nodes?v
http://localhost:9200/_cat/indices?v

curl -XGET 'http://localhost:9200/_all/_mapping/book/field/author'
curl -XHEAD -i 'http://localhost:9200/twitter/tweet'
curl localhost:9200/_stats
curl -XGET 'http://localhost:9200/_all/_mapping/[type]'

–END

Comments