跳到正文
W Winse Blog
dev datastore 3 min read

elasticsearch startguide

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

# 安装

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

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

# 插件

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

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

[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

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请求

# 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

在 GitHub 上讨论

欢迎通过 GitHub Issue 留言或反馈。每条讨论都会关联到对应文章的源文件路径。

2016-06-15-elasticsearch-startguide.md

Related posts