Running as a plugin of Elasticsearch
- Install elasticsearch-head:
– for Elasticsearch 5.x:
site plugins are not supported. Run elasticsearch-head as a standalone server
Running with built in server
- enable cors by adding
http.cors.enabled: true
in elasticsearch configuration. Don’t forget to also sethttp.cors.allow-origin
because no origin allowed by default.http.cors.allow-origin: "*"
is valid value, however it’s considered as a security risk as your cluster is open to cross origin from anywhere. Check Elasticsearch documentation on this parameter:https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-http.html#modules-http git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
grunt server
-
open
http://localhost:9100/
https://github.com/mobz/elasticsearch-head#running-with-built-in-server
1、windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL”按钮,直接点击就会自动下载安装了。
2、安装过程基本直接“NEXT”就可以了。(windows的安装msi文件在过程中会直接添加path的系统变量,变量值是你的安装路径,例如“C:\Program Files\nodejs”)。
3、安装完成后可以使用cmd(win+r然后输入cmd进入)测试下是否安装成功。方法:在cmd下输入node -v,出现下图版本提示就是完成了NodeJS的安装。
5、常规NodeJS的搭建到现在为止已经完成了,急不及待的话你可以在”cmd“输入”node“进入node开发模式下,输入你的NodeJS第一句:”hello world“ - 输入:console.log('hello world')。
步骤3:
MINGW64 /e/elasticsearch-head (master)
$ npm install
npm WARN deprecated [email protected]: use uuid module instead
> [email protected] install E:\workspace-demo\elasticsearch-head\node_modules\phantomjs-prebuilt
> node install.js
PhantomJS not found on PATH
Downloading https://github.com/Medium/phantomjs/releases/download/v2.1.1/phantomjs-2.1.1-windows.zip
Saving to C:\Users\TANG~1.CHE\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip
Receiving...
Received 17767K total.
Extracting zip contents
Removing E:\workspace-demo\elasticsearch-head\node_modules\phantomjs-prebuilt\lib\phantom
Copying extracted folder C:\Users\TANG~1.CHE\AppData\Local\Temp\phantomjs\phantomjs-2.1.1-windows.zip-extract-1479521265399\phantomjs-2.1.1-windows -> E:\workspace-demo\elasticsearch-head\node_modules\phantomjs-prebuilt\lib\phantom
Writing location.js file
Done. Phantomjs binary available at E:\workspace-demo\elasticsearch-head\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe
[email protected] E:\workspace-demo\elasticsearch-head
`-- [email protected]
+-- [email protected]
| +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | `-- [email protected]
| | | `-- [email protected]
| | | `-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | `-- [email protected]
| | +-- [email protected]
| | | `-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | | `-- [email protected]
| | | | +-- [email protected]
| | | | +-- [email protected]
| | | | `-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | +-- [email protected]
| | | | `-- [email protected]
| | | `-- [email protected]
| | `-- [email protected]
| | `-- [email protected]
| `-- [email protected]
`-- [email protected]
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN [email protected] license should be a valid SPDX license expression
grunt
是一套前端自动化工具,一个基于nodeJs的命令行工具,一般用于:
① 压缩文件
② 合并文件
③ 简单语法检查
对于其他用法,我还不太清楚,我们这里简单介绍下grunt的压缩、合并文件,初学,有误请包涵
准备阶段
1、nodeJs环境
因为grunt是基于nodeJs的,所以首先各位需要安装nodeJS环境,这块我们便不管了
2、安装grunt
有了nodeJs环境后,我们便可以开始搞grunt了,因为我们可能在任何目录下运行打包程序,所以我们需要安装CLI
官方推荐在全局安装CLI(grunt的命令行接口)
npm install -g grunt-cli
这条命令将会把grunt命令植入系统路径,这样就能在任意目录运行他,原因是
每次运行grunt时,它都会使用node的require查找本地是否安装grunt,如果找到CLI便加载这个本地grunt库
然后应用我们项目中的GruntFile配置,并执行任务
步骤4:
/e/elasticsearch-head (master)
$ npm install -g grunt-cli
C:\Users\admin\AppData\Roaming\npm\grunt -> C:\Users\admin\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
C:\Users\admin\AppData\Roaming\npm
`-- [email protected]
+-- [email protected]
| `-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| +-- [email protected]
| | `-- [email protected]
| | +-- [email protected]
| | `-- [email protected]
| +-- [email protected]
| `-- [email protected]
+-- [email protected]
+-- [email protected]
| `-- [email protected]
`-- [email protected]
ANTS-SH-0063 MINGW64 /e/elasticsearch-head (master)
$ grunt server
Running "connect:server" (connect) task
Waiting forever...
Started connect web server on http://localhost:9100
IK Analysis for Elasticsearch
The IK Analysis plugin integrates Lucene IK analyzer (http://code.google.com/p/ik-analyzer/) into elasticsearch, support customized dictionary.
Analyzer: ik_smart
, ik_max_word
, Tokenizer: ik_smart
, ik_max_word
Versions
IK version | ES version |
master | 5.x -> master |
5.0.1 | 5.0.1 |
1.10.1 | 2.4.1 |
1.9.5 | 2.3.5 |
1.8.1 | 2.2.1 |
1.7.0 | 2.1.1 |
1.5.0 | 2.0.0 |
1.2.6 | 1.0.0 |
1.2.5 | 0.90.x |
1.1.3 | 0.20.x |
1.0.0 | 0.16.2 -> 0.19.0 |
Install
1.compile
checkout ik version respective to your elasticsearch version
git checkout tags/{version}
mvn package
copy and unzip target/releases/elasticsearch-analysis-ik-{version}.zip
to your-es-root/plugins/ik
2.restart elasticsearch
Tips:
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
Quick Example
1.create a index
curl -XPUT http://localhost:9200/index
2.create a mapping
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
"fulltext": {
"_all": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"term_vector": "no",
"store": "false"
},
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
3.index some docs
curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
4.query with highlighting
curl -XPOST http://localhost:9200/index/fulltext/_search -d'
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
Result
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 2,
"hits": [
{
"_index": "index",
"_type": "fulltext",
"_id": "4",
"_score": 2,
"_source": {
"content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
},
"highlight": {
"content": [
"<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
]
}
},
{
"_index": "index",
"_type": "fulltext",
"_id": "3",
"_score": 2,
"_source": {
"content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight": {
"content": [
"均每天扣1艘<tag1>中国</tag1>渔船 "
]
}
}
]
}
}
https://github.com/medcl/elasticsearch-analysis-ik
配置 JDBC 数据源
1. 安装 openjdk(本人用的ubuntu)
sudo apt-get install openjdk-7-jre
2.下载安装 elasticsearch
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.deb
sudo dpkg -i elasticsearch-1.4.4.deb
/etc/init.d/elasticsearch start
3.安装JDBC 插件
1
./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-river-jdbc/1.4.0.10/elasticsearch-river-jdbc-1.4.0.10.zip
4.安装MySQL JDBC 驱动
curl -o mysql-connector-java-5.1.33.zip -L 'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.33.zip/from/http://cdn.mysql.com/'
unzip mysql-connector-java-5.1.33.zip
cp mysql-connector-java-5.1.33-bin.jar $ES_HOME/plugins/jdbc/
chmod 644 $ES_HOME/plugins/jdbc/*
/etc/init.d/elasticsearch restart
http://localhost:9200/_nodes?settings=true&pretty=true 配置信息(安装路径、插件...)
更多参数见:https://github.com/jprante/elasticsearch-river-jdbc
5.测试mysql (用户root 密码passwd 库jdbctest 表名posts)
curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"url" : "jdbc:mysql://localhost:3306/jdbctest",
"user" : "root",
"password" : "passwd",
"sql" : "select * from posts"
}
}'
curl 'localhost:9200/jdbc/_search'
npm install pkg - 本地安装
运行如下命令,就会在当前目录下安装grunt-cli
(grunt命令行工具)
npm install grunt-cli
安装结束后,当前目录下回多出一个node_modules
目录,grunt-cli就安装在里面。同时注意控制台输出的信息:
[email protected] node_modules/grunt-cli
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
简单说明一下:
- [email protected]:当前安装的package为grunt-cli,版本为0.19
- node_modules/grunt-cli:安装目录
- [email protected]:依赖的包有resolve、nopt、findup-sync,它们各自的版本、依赖在后面的括号里列出来
npm install -g pkg- 全局安装
上面已经安装了grunt-cli,然后你跑到其他目录下面运行如下命令
grunt
果断提示你grunt命令不存在,为什么呢?因为上面只是进行了本地安装,grunt命令只能在对应安装目录下使用。
-bash: grunt: command not found
如果为了使用grunt命令,每到一个目录下都得重新安装一次,那不抓狂才怪。肿么办呢?
很简单,采用全局安装就行了,很简单,加上参数-g
就可以了
npm install -g grunt-cli
于是,在所有目录下都可以无压力使用grunt
命令了。这个时候,你会注意到控制台输入的信息有点不同。主要的区别在于安装目录,现在变成了/usr/local/lib/node_modules/grunt-cli
,/usr/local/lib/node_modules/
也就是之前所说的全局安装目录啦。
[email protected] /usr/local/lib/node_modules/grunt-cli
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
npm包管理
npm的包管理命令是使用频率最高的,所以也是我们需要牢牢记住并熟练使用的。其实无非也就是几个动作:安装、卸载、更新、查看、搜索、发布等。
安装最新版本的grunt-cli
npm install grunt-cli
安装0.1.9版本的grunt-cli
npm install grunt-cli@"0.1.9"
通过package.json进行安装
如果我们的项目依赖了很多package,一个一个地安装那将是个体力活。我们可以将项目依赖的包都在package.json这个文件里声明,然后一行命令搞定
npm install
其他package安装命令
运行如下命令,列出所有npm install
可能的参数形式
npm install --help
输出如下,有兴趣的童鞋可以了解下
npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <pkg>
npm install <pkg>@<tag>
npm install <pkg>@<version>
npm install <pkg>@<version range>
卸载grunt-cli
比如卸载grunt-cli
npm uninstall grunt-cli
卸载0.1.9版本的grunt-cli
npm uninstall grunt-cli@"0.1.9"
npm ls:查看安装了哪些包
运行如下命令,就可以查看当前目录安装了哪些package
npm ls
输出如下
/private/tmp/npm
└─┬ [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected]
└── [email protected]
输出如下,同样,如果是要查看package的全局安装信息,加上-g
就可以
npm ls pkg:查看特定package的信息
运行如下命令,输出grunt-cli的信息
npm ls grunt-cli
输出的信息比较有限,只有安装目录、版本,如下:
/private/tmp/npm
└── [email protected]
如果要查看更详细信息,可以通过npm info pkg
,输出的信息非常详尽,包括作者、版本、依赖等。
npm info grunt-cli
npm update pkg:package更新
npm update grunt-cli
npm search pgk:搜索
输入如下命令
npm search grunt-cli
返回结果如下
npm http GET http://registry.npmjs.org/-/all/since?stale=update_after&startkey=1375519407838
npm http 200 http://registry.npmjs.org/-/all/since?stale=update_after&startkey=1375519407838
NAME DESCRIPTION AUTHOR DATE KEYWORDS
grunt-cli The grunt command line interface. =cowboy =tkellen 2013-07-27 02:24
grunt-cli-dev-exitprocess The grunt command line interface. =dnevnik 2013-03-11 16:19
grunt-client-compiler Grunt wrapper for client-compiler. =rubenv 2013-03-26 09:15 gruntplugin
grunt-clientside Generate clientside js code from CommonJS modules =jga 2012-11-07 01:20 gruntplugin
npm发布
这个命令我自己也还没实际用过,不误导大家,语法如下,也可参考官方对于package发布的说明https://npmjs.org/doc/developers.html:
npm publish <tarball>
npm publish <folder>
NPM配置
npm的配置工作主要是通过npm config
命令,主要包含增、删、改、查几个步骤,下面就以最为常用的proxy配置为例。
设置proxy
内网使用npm很头痛的一个问题就是代理,假设我们的代理是 http://proxy.example.com:8080,那么命令如下:
npm config set proxy http://proxy.example.com:8080
由于npm config set
命令比较常用,于是可以如下简写
npm set proxy http://proxy.example.com:8080
查看proxy
设置完,我们查看下当前代理设置
npm config get proxy
输出如下:
http://proxy.example.com:8080/
同样可如下简写:
npm get proxy
删除proxy
代理不需要用到了,那删了吧
npm delete proxy
查看所有配置
npm config list
直接修改配置文件
有时候觉得一条配置一条配置地修改有些麻烦,就直接进配置文件修改了
npm config edit
关于package.json
这货在官网似乎没有详细的描述,其实就是包的描述信息啦。假设当我们下载了node应用,这个node应用依赖于A、B、C三个包,如果没有package.json,我们需要人肉安装这个三个包(如果对版本有特定要求就更悲剧了):
npm install A
npm install B
npm install C
有了package.json,一行命令安装所有依赖。
npm install
package.json字段简介
字段相当多,但最重要的的是下面几个
- name: package的名字(由于他会成为url的一部分,所以 non-url-safe 的字母不会通过,也不允许出现"."、"_"),最好先在http://registry.npmjs.org/上搜下你取的名字是否已经存在
- version: package的版本,当package发生变化时,version也应该跟着一起变化,同时,你声明的版本需要通过semver的校验(semver可自行谷歌)
- dependencies: package的应用依赖模块,即别人要使用这个package,至少需要安装哪些东东。应用依赖模块会安装到当前模块的node_modules目录下。
- devDependencies:package的开发依赖模块,即别人要在这个package上进行开发
- 其他:参见官网
package版本
在package.json里,你经常会在包名后看到类似"~0.1.0"这样的字符串,这就是包的版本啦。下面会列举最常见的版本声明形式,以及版本书写的要求:
常见版本声明形式
a、"~1.2.3" 是神马意思呢,看下面领悟
"~1.2.3" = ">=1.2.3 <1.3.0"
"~1.2" = ">=1.2.0 <1.3.0"
"~1" = ">=1.0.0 <1.1.0"
b、"1.x.x"是什么意思呢,继续自行领悟
"1.2.x" = ">=1.2.0 <1.3.0"
"1.x.x" = ">=1.0.0 <2.0.0"
"1.2" = "1.2.x"
"1.x" = "1.x.x"
"1" = "1.x.x"
版本书写要求
- 版本可以v开头,比如 v1.0.1(v只是可选)
- 1.0.1-7,这里的7是所谓的“构建版本号”,不理是神马,反正版本大于1.0.1
- 1.0.1beta,或者1.0.1-beta,如果1.0.1后面不是 “连字符加数字” 这种形式,那么它是pre release 版本,即版本小于 1.0.1
- 根据b、c,有:0.1.2-7 > 0.1.2-7-beta > 0.1.2-6 > 0.1.2 > 0.1.2beta
写在后面
内容只是简单地把最常见的命令,以及一些需要了解的内容列了出来。如要进一步了解,可参考官网说明。此外,npm help
是我们最好的朋友,如果忘了有哪些命令,命令下有哪些参数,可通过help进行查看。
最关键的:如果文章内容有误,请指出!!!
github博客:https://github.com/chyingp/blog
新浪微博:http://weibo.com/chyingp
站酷主页:http://www.zcool.com.cn/u/346408/